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

27

u/ingvij Apr 09 '24

I honestly won't complain about the DSL, I think you can create the abstractions you want to make your code look better for your taste and use them in your project if you or your team is happy with that..

A couple of things though:

  • This is the kind of library that I would most likely not want to use in any performance-aware application. Remember that there's always a trade-off. When you add layers on top of your code, it will pay a cost. From a quick glimpse over the KeyValue class and the `map()` static function, I would assume there's some GC overhead on using this over a regular `new Hashmap<>(Map.of(...))`.

  • Intense usage of this library can yield a codebase that is java in theory but not really in practice, increasing the entry barrier for contributors, so that would also be a factor against using this library on a project.

But then, as I said, if it works for you, then I guess this is fine.

12

u/agentoutlier Apr 10 '24

I know /u/guybedo has been dragged over the coals for this lib (perhaps unfairly) but the library is neither a DSL or syntactical sugar.

It is an opinionated utilities library.

I mean you could make an argument I guess for DSL the domain being collections but it is for sure not syntactical sugar like lombok or var args. Adding methods is not syntactical sugar.

I guess I'm being pedantic but the reason is if it were an actual DSL of a unique domain it would probably have better uptake.

-1

u/guybedo Apr 10 '24

Yeah this post has gotten a lot of negative comments and i'm not sure that's really justified.

fwiw although it's not like lombok, for sure, it still qualifies as syntactic sugar

https://en.wikipedia.org/wiki/Syntactic_sugar#:~:text=In%20computer%20science,more%20verbose%2C%20form

5

u/agentoutlier Apr 10 '24

https://en.wikipedia.org/wiki/Syntactic_sugar#:~:text=In%20computer%20science,more%20verbose%2C%20form

I fail to see it in the link.

You are not changing the syntax of the language but just applying functions.

You are not even changing the evaluation order like with hygienic macros (Java does not have macros) which are borderline syntactical sugar adjustments bust just adding shorter methods or changing order is not syntactical sugar. Even if that is stated in a wikipedia (which again I fail to see) I just disagree.

1

u/guybedo Apr 10 '24

In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer. Syntactic sugar is usually a shorthand for a common operation that could also be expressed in an alternate, more verbose, form: The programmer has a choice of whether to use the shorter form or the longer form, but will usually use the shorter form since it is shorter and easier to type and read.

6

u/sabriel330 Apr 10 '24

I think you're completely misunderstanding what syntax is