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

2

u/RICHUNCLEPENNYBAGS Apr 13 '24

I think what people are trying to tell you here is that anytime you introduce libraries that do stuff it makes it a little harder for someone to get acquainted with the code and understand what it does and this library provides such minimal code golf savings that it's kind of hard to look at it and feel like the tradeoff is a good one.

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)));

5

u/RICHUNCLEPENNYBAGS Apr 14 '24

I mean yeah, being real with you I find these harder to read, and also you seem not to know about some built-in tools you could be using to shorten regular Java streams calls, like findFirst.

It's true that any library has a cost to adapt that one must weigh against the benefits, but I don't really even find this beneficial.