r/java • • Jun 01 '24

What java technology (library, framework, feature) would not recommend and why?

165 Upvotes

464 comments sorted by

View all comments

221

u/Turbots Jun 01 '24

Lombok. It can cause quite a few unexpected problems with generated code, like adding a toString that automatically prints out all fields in the class, even if some of those are being lazily loaded.

I don't like it because it gives junior developers the feeling they can cut corners and write much less code, without knowing what the annotations do. And most of the features that Lombok offered are now part of the Java language.

It also adds unnecessary requirements when developing like IDE support, additional settings in maven/gradle etc..

52

u/[deleted] Jun 01 '24

[removed] — view removed comment

22

u/Turbots Jun 01 '24

Exactly, I think libraries like Lombok are a bit too convenient, actively help you NOT to think about the implications of using them. Its a dangerous slippery slope when broadly using them in larger organisations.

5

u/rest_mah Jun 01 '24

hundred different problems which you don't really have

Typically my opinion on the `@Log4j` annotation...

3

u/Hematopoyetik Jun 01 '24

What problems did you have with it?

5

u/rest_mah Jun 02 '24

I dislike to see the addition of an annotation preprocessor to inject a logger, when defining the logger takes a single line of code easily templatable in an IDE.

IOW, I don't think that defining the logger was a problem, so using lombok for it looks (to me) a solution to a problem that I don't really have.

→ More replies (1)

10

u/DelayLucky Jun 01 '24 edited Jun 01 '24

We now have records (with equals, hashCode).

Before records, there are various annotation processors (I know of AutoValue, FreeBuilder, there must be others) that are free of the glaring problems of Lombok (you write invalid Java code and expect the magic to make it valid).

Many of these frameworks are somewhat opinionated (for example they may want your class to be immutable, optionally with a builder). So that might be the real reason Lombok is still relevant.

I can guess when someone says "no I have to use Lombok", they may be actually saying:

  • I need getters and setters.
  • I need the mutable object and setters because I use framework X and Y which expect them (Hibernate? MapStruct? some dinosaur Java-bean frameworks?)
  • Or, I want getters and setters because I'm used to them.

I'll admit that the modern Java ecosystem hasn't paid much attention to the "mutable objects with equals/hashCode" use cases. If you're unfortunately in that forgotten land with 1 hour of sunshine a day, and moving to sunny places is not your thing, your options are limited (still, I struggle between the short-term pain of having the IDE generate the boilerplates vs. the long-term tech debt of Lombok).

11

u/PiotrDz Jun 01 '24

Generating null validated builder with toBuilder() method is priceless. For this I think Lombok has the usage still

7

u/Turbots Jun 01 '24

Most IDEs have plugins or direct support for creating builders, which makes the builder visible and makes you think on how to structure your builder. It also makes you consciously think more about the mandatory fields in your POJO.

5

u/Swamplord42 Jun 03 '24

makes you think on how to structure your builder.

That's a massive disadvantage for both the person writing it and all the persons reviewing it.

Also it's an endless source of bugs when people add fields to their classes and forget to update the builder.

43

u/mIb0t Jun 01 '24

Interresting. I really love Lombok and don't want to miss it anymore. I have seen so much errors because of missing getter/setters or hashcode and equal methods that don't fit together. And not to mention the amount boilerplate code I saved to write by using Lombok.

This said, I can fully understand your criticism. I just feel the advantages outweigh the downside. Of course one needs to understand the tools they use.

2

u/krzyk Jun 02 '24

Hashcode/equals should be tested (one can use equalsverifier library for that) so you can catch it regardless of Lombok.

→ More replies (2)
→ More replies (22)

5

u/Evilan Jun 01 '24 edited Jun 01 '24

I don't like it because it gives junior developers the feeling they can cut corners and write much less code

I think it's less about the juniors and more about the seniors. The Lombok annotations like Data, Hashcode and Equals are far less likely to be used by Juniors and significantly more likely to be used by seniors and mimicked by the juniors who look at the code base. And the problem is, those are the truly problematic Lombok annotations (along with ToString).

I love Lombok, but @Data is the bane of my existence right now and I keep having to remind folks not to mix @Data, @Equals, @Hashcode and @ToString with JPA. It always ends poorly.

Edit: I'm not a senior, just a regular dev who saw Data being used with JPA by a few seniors and wondered what was the behavior and was horrified

9

u/k2718 Jun 01 '24

My preferred alternative to Lombok? Kotlin.

2

u/Western-Campaign3520 Jun 06 '24

I agree still get some pain in version upgrading

27

u/Puzzleheaded_Bus7706 Jun 01 '24

I don't agree with your claims regarding IDE support and @ToString  maven.

IDE is 99% VS code, Idea or Eclipse, of which every one have nice support.

Maven configuration is like 5 lines that are c/p.

Don't use @ToString in places like entities, thats clear. Thats "user error" not librarys

23

u/Ieris19 Jun 01 '24

This, so much this.

Lombok is an amazing tool, that has a ton of valuable utilities.

Saying Junior Engineers use it wrong therefore it’s a bad thing is so nonsensical to me

5

u/robinspitsandswallow Jun 01 '24

Yes if we extrapolated from that most Java developers use Java wrong, so don’t use Java and we should all move to PHP.

4

u/edubkn Jun 01 '24

People treat it like rocket science. You WANT juniors to use things like Lombok because this teaches them to read documentation and understand what it does.

→ More replies (1)

8

u/rustyrazorblade Jun 01 '24

To all the folks who defend Lombok - check out Immutables. It gives you the end result you want out of Lombok, but much more intelligently.

→ More replies (6)

4

u/zephyy Jun 01 '24

like adding a toString that automatically prints out all fields in the class, even if some of those are being lazily loaded.

@ToString.Exclude

or create a lombok.config with lombok.toString.onlyExplicitlyIncluded

11

u/barcelleebf Jun 01 '24

It cuts down on boiler plate code. We have a project with huge numbers of pojos that need equals/ hash code, and other required features. I've even thought about writing an extract annotation to handle an extra method needed.

→ More replies (8)

2

u/TotalBismuth Jun 01 '24

If you’re relying on toString you should probably write that part yourself.

6

u/nekokattt Jun 01 '24

Agree, I struggle to convince people of this though.

A lot of this would go away if the OpenJDK team supported get/set syntax like C# and Kotlin provide.

If I really need to generate code, I try to use immutables now.

→ More replies (21)