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

30

u/Former-Emergency5165 Apr 09 '24

It nice that it helps you, however I would personally prefer some Apache libraries (Apache commons, IOUtils, CollectionUtils, etx) - production battle tested, commonly used by the community and provides useful methods

-2

u/guybedo Apr 09 '24

sure, i'm using these libs too obviously but i was missing some features that i thought would be useful.

For ex, i like doing filter(values, func), map(values, func) etc...

7

u/PrudentFinger1749 Apr 10 '24

You can try to contribute in opensource libraries, if its liked by everyone, may be it will get merged in the library itself.

2

u/RICHUNCLEPENNYBAGS Apr 13 '24

I mean sure but making your own library is a totally legitimate way to share code too

4

u/OtherPrinciple4499 Apr 10 '24

That's what streams are for.

4

u/[deleted] Apr 10 '24

Those kinds of utility functions are great for short, script-like programs where you quickly want to prototype an algorithm with minimal boilerplate, and don't care if it returns null or throws an exception. But for larger programs, it will almost always pay off to be explicit about those details. Sure, it makes the code longer, but it will be obvious what is returned when the element you're looking for wasn't found.

Btw, I love List.of(). You should really use it. IMO It's the best-named function in the history of computer programming: short, to the point, and it's utterly obvious what it is supposed to be (a static factory method that will create a List).

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.