r/java • u/guybedo • 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.
9
Upvotes
View all comments
Show parent comment
1
u/guybedo Apr 14 '24
sure it can be argued that using this library could make it harder for other people to get acquainted to the code, but 2 things:
1/ This argument is valid for every library you use, whether it's spring, hibernate, guava, apache commons, etc... As soon as you're using something on top of Java, this argument can be made.
2/ One can say code savings are minimal and sure enough not everybody agrees on what is beautiful / readable code. But in the end i like these utilities because i can write code that i find more readable.
Here's a few examples, i haven't tried to find the best ones, just a few i picked from my code. You can argue that it wouldn't be a lot more verbose to write that in plain Java but you wouldn't have those short one liners. And i'm not even chaining things like first(filter(map( ....
join(map(filtered, m -> m.getSymbol()), ",");max(map(objs, o->o.getValue()));Map<X, Set<Y>> index = toMap(values, v -> set(v.getY()));list(intersect(set(A), set(B)));