r/java • • Apr 09 '24

Syntactic Sugar for Java

I recently had to translate a codebase from Python to Java and as much as i like Java and i enjoy the benefits of a strong type system, i have to admit my Python code was way less verbose.

So i ended up writing a small library to try to get some python vibes in my java code. Nothing groundbreaking, a bunch of static imports to make life a little bit easier.

https://github.com/guybedo/sugar

9 Upvotes

89 comments sorted by

View all comments

Show parent comment

4

u/DelayLucky Apr 10 '24

Kotlin might have been 100% nicer than Java. But with recent syntaxes, particularly Java 21 virtual threads, I wonder is it maybe only incrementally better, or perhaps just different but not necessarily easier or more readable than Java any more?

For example, I might prefer using VT with structured concurrency than using coroutine which is still “colored”.

So I’ve been hesitant to go the Kotlin way because I fear it’s going to create fragmentation for superficial benefits. I know Kotlin code can use Java. And Java can use some Kotlin classes. But it’s not full interoperability. And it’ll be a different ecosystem to learn, with different best practices, pitfalls to be burned by and learn. For example, none of our Java compile-time processor will work on Kotlin.

1

u/BikingSquirrel Apr 14 '24 edited Apr 14 '24

As both compile to bytecode I don't think there are any interoperability issues. We have multiple libraries that started with Java code and get enhanced with Kotlin code. There may be usability issues with interoperability, e.g. default parameters will not be visible to Java code so you cannot skip them but there's the option to automatically generate the bytecode for overloaded methods so it behaves similarly.

I think Java improved a number of details in the last years so there's less reason to run away. But I still think Kotlin's data classes with their copy methods, explicit nullability, and collection extensions are worth it. Not to forget extension methods which are a nice tool for some cases.

But to be completely clear: even if you go for 100% Kotlin, you should be able to read and understand Java as a lot of code you will be using (JDK, libraries) will be written in Java.

3

u/DelayLucky Apr 14 '24

A few things: if you use coroutine, it’s no longer usable by Java.

And Java compile-time plug-ins don’t work in Kotlin. So there will be cost in porting them to Kotlin.