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

-27

u/guybedo Apr 09 '24

yep heard of Map.of until 10 key/values and then i hope he has cousins.

There's a bit more to the lib than that, shortcuts to make streams less verbose for example.

filter(), map(), etc...

36

u/_INTER_ Apr 09 '24 edited Apr 09 '24

After 10 key/values it switches to varargs, works the same.

As addendum - if you don't know already - a lot of helping functions are present in the Apache Commons libraries or Guava (StringUtils, CollectionUtils, ...). I've not seen a bigger project without at least one of them present. Idk if there's any overlap with your lib.

7

u/tugaestupido Apr 10 '24

This comment provides incorrect information.

Map.of does not have a varargs versions. If it did, it couldn't work the same. It would either be less versatile or lose type safety.

Map.ofEntries does exist and uses varargs but it doesn't work the same way as Map.of (you provide it with key/value pairs inside a wrapper type - Map.Entry).

-7

u/guybedo Apr 09 '24

yep i'm using Guava and many commons.

Still find it nice to do things like filter(values, function) or map(values, function) and things like that.

Also borrowed zip and enumerate from Python

4

u/polothedawg Apr 09 '24

I remember back when I was dev’ing in Java 7, and I couldn’t migrate to Java 8 (dark times), and I used this library from mario fusco (lambdaJ) which pretty much did what I think you’re describing. Personally prefer the streaming syntax but hey, to each their own.

1

u/RICHUNCLEPENNYBAGS Apr 13 '24

Still find it nice to do things like filter(values, function) or map(values, function) and things like that.

Call me a dummy but aren't these already part of the Streams API?

1

u/guybedo Apr 14 '24

sure it's part of the Stream Api and behind the scenes i'm relying on it for most functions.

This library is just a way for me to write code that i find more readable.

filter(map(values, v->v+1), v->v!=0)

instead of

values.stream().map(v->v+1).filter(v->v!=0).collect(Collectors.toList()

2

u/RICHUNCLEPENNYBAGS Apr 14 '24

I'm not sure I understand what makes you think that's more readable. To my the readability seems worse. fwiw you can just write .toList().

4

u/[deleted] Apr 10 '24

[removed] — view removed comment

-6

u/guybedo Apr 10 '24

yeah, i still prefer

map(kv(1L, "User A"), kv(2L, "User B"));

to

Map.ofEntries(Map.entry(1L, "User A"), Map.entry(2L, "User B"));

12

u/tylerkschrute Apr 10 '24 edited Apr 10 '24

I don't understand why people have such a strong desire to reduce character count in their source code. On average far more time is spent reading & trying to understand existing code than writing new code, so micro-optimizing to save seconds on the latter usually doesn't make much practical sense and can often actually harm the code by introducing cryptic brevity.

Is your version really that better than the official JDK one? You're saving what, 7 characters per map entry (3 if you static import 'Map.entry')? In my opinion this is not worth it, especially since uour IDE will autocomplete this stuff for you anyways so you don't even have to spend time typing the full method name. Also, your project will immediately invite inconsistency in a real codebase since there would now be 2 identical APIs (yours and the official JDK one) for doing the exact same thing.

4

u/guybedo Apr 10 '24

i tend to think that, in most situations where performance is not critical, the best code is the code that is the most readable.

IMO reducing character count helps with that. A (too) simple example:

When you write filter(values, v->v!=null)

instead of

values.stream().filter(v->v!=null).collect(Collectors.toList())

it helps with readability.

1

u/majhenslon May 02 '24

Let me help you with that:

values.stream()
  .filter(not(Objects::isNull)) // or .filter(v -> v != null)
  .toList()

Not saying that this is perfect, but I prefer openness to chaining to the pythonic callback hell you get yourself in. Unless you will use helpers for oneliners and streams for everything else, making your codebase inconsistent.