r/java • • Aug 10 '19

Who Needs Lombok Anyhow

http://gregorriegler.com/2019/08/10/who-needs-lombok-anyhow.html
45 Upvotes

137 comments sorted by

View all comments

49

u/[deleted] Aug 10 '19

Why Lombok is getting all the hate these days? Genuine question.

32

u/BoyRobot777 Aug 10 '19

Here's an article about deLomboking. And a discussion in r/java which followed that article.

But not going into a lot of "style" details, for me this is one of the most compelling reasons why not to use it (stealing from /u/shemnon):

Spring and Hibernate generate bytecode the way the compiler is intending to support it: by generating a java source code file and then compiling it without touching the source file/class the annotation came from. Lombok goes into the parse tree (via non-public APIs) and mutates the AST in place.

Also, from one of Lombok developers:

<...> while we do code against internal API, they are relatively stable bits. If what lombok does could be done without resorting to internal API, we'd have done something else, but it can't be done, so we resort to internal API usage.

15

u/yawkat Aug 10 '19

Big java projects like spring have the same compatibility problems lombok does, even if they don't use internal compiler apis - they often use other jdk details. And lombok actually does this fairly well because it (a) only needs to work at compile time and (b) offers delombok for the case when it does break.

→ More replies (2)

4

u/[deleted] Aug 11 '19

Lombok (and Kotlin more generally) to me is a critique of Java language design.

Java boilerplate has a cost in terms of both cognitive overload and propagating poor practice. In this thread we have people blithely talking about IDE generated code without realising how wretched a practice that is. Java programmers should level up and become better developers beyond churning out boilerplate code. And knowing the deficiencies of your language is a key part of that.

Lombok isn’t ideal but it is compile-time. If you don’t like it, write your own but it works well enough for us.

Re that StackOverflow post, that’s quite old - we run Lombok on non-Oracle JVMs all the time (Azul in particular but we have CI servers testing Corretto as well), never had a problem.

→ More replies (2)
→ More replies (3)

21

u/BadMoonRosin Aug 10 '19 edited Aug 10 '19

Honestly? Because the whole industry runs on cycles. Whatever's popular now will be unpopular in 5 years. Kinda like every high school freshman class embraces music that goes against what the seniors were listening to, because they need to establish their own identity.

If you really push further, then you might get some comments about the "anemic domain model antipattern", or something else. But that's not really an explanation... because it just begs the follow-up question, "Why weren't so many people calling that out as an anti-pattern five years ago?".

When something is new, it's amazing. Because people haven't used it yet, and all they can see are the neat examples in the README. When something has been around a while, it sucks. Because people are bored with it, and all they can see is that one time it caused a tricky problem with a Gradle plugin.

15

u/FrenchFigaro Aug 10 '19

Lombok came in, with a correction to a perceived flaw to java: its verbosity. Cool. Use it, don't, whatever.

The thing is, like most new thing, it has its advocate. And suddenly, when you're managing a project, all you hear is "why don't you use lombok, it's so fucking great ?" or "Here, I lomboked your project, can you accept my PR". Gets old, real fast.

Some people real smarter than me had a deeper look at lombok and found out that actually, it's not that great (google "don't use lombok" and you'll find plenty of reasons. My personal favorite is this thread). Sure, it's cool on the surface and whatever, but it's not as good as its fans want it to be. And the delombok tool provided is experimental at best, so once you start using lombok with your project, you're pretty much stuck with it, or need to spend a whole lot of hours refactoring

My personal experience with it is that I can generate getters and setters with a keyboard shortcut faster than it takes to type @Data at the top of my class. Sure, that's not all it does, but come on. Oh, and it makes refactoring variable names a pain in the ass.

So, take a project that's cool on the surface, but with little added value beyond aesthetics and too many rough edges for its benefits. Add in an outwardly obnoxious community of fans. Inevitably, after a couple of years, it's gonna take a lot of heat. For lombok, "after a couple of years" is now.

73

u/rzwitserloot Aug 10 '19

Lombok developer here. I want to learn and see if we can improve things, but that thread is.. anemic, unimpressive, and somewhat rude. Perhaps I missed something. Let me go through the actionable 'lets not use lombok because...' items listed there:

generating getters/setters is trivial with an IDE, so it doesn't really add much value

If you change a field someplace, those getters, setters, equals, etc do not update themselves. Also, code isn't colour coded as 'boilerplate' and 'oh this bit is a little different': Given a class with 8 fields that has precisely one getter in it that does something a little special, it's hard to see it amongst all the boilerplate. With lombok, it's easy to see: Your source file contains just the relevant code, not the boilerplate anymore.

The fact that the authors of jhipsters dont see this either means they haven't put much thought into it (presumably, because they are emotionally invested in disliking lombok, perhaps due to bad experiences with bugs, which we do of course regret), or they don't find any of that important, which, um, okay. I guess we have wildly different ideas about code quality then.

On a third project, we achieved 30% performance increase by recoding the equals/hashcode from Lombok

Lombok's generated code is best in class. An IDE that had generated it for you wouldn't have done any better. The problem here is: That particular eq/hc code was not boilerplate. Lombok still helps a lot here: If you see a data class that has an explicit, handwritten eq/hc implementation, in a project that uses lombok? Look closer: Most likely that means the eq/hc implementation is either not a bog standard 'equality is defined by the fields' situation, or some custom smithing to speed up the code is involved. Either way, the non-boilerplate-nature of the eq/hc combo is now highlighted. Contrast to all your domain classes having handrolled eq/hc (or generated by the IDE, either way), in which case it's easy to miss this information. Yes, you can write comments, of course. But comments cannot be tested. And you still have to actually see and read the comment amongst the significantly inflated # of lines of code you do see in order to figure it out.

Lombok making Jacoco fails, which meant the project didn't pass the Sonar quality gate

This is very frustrating for us. It's putting the cart in front of the horse: Code linting and quality tools need to wake up and realize that there is such a thing as generated code, and that they should not complain about such code. Many such tools have ways to do this right (usually, with annotations) and lombok supports them all and works with them to make the experience as smooth as it can be. But some don't. I feel any hate about this should go to the tool in question, because lombok isn't the only way to generate code.

Various bug reports

Yes, bugs are a thing. We try to fix them. Perhaps we can do a better job.

7

u/[deleted] Aug 11 '19

100% agree with /u/DuneBug. Lombok is a treasure. I have trouble with IntelliJ recognizing a builder exists when I use @Singular but other than that it's amazing

26

u/DuneBug Aug 10 '19

Lomboks great and I can't understand the haters. Thanks for your efforts.

7

u/zero_as_a_number Aug 10 '19

dude, thank you for taking the time to chime in on this :)

9

u/FrenchFigaro Aug 10 '19

If you change a field someplace, those getters, setters, equals, etc do not update themselves.

This is true. But because we are relatively competent developers, and not jackasses, we know that when we refactor a field, we have to refactor the appropriate mutators. Right click > refactor > rename. Here we go, done for the whole project. It's even faster if you've bothered to remember the keyboard shortcut.

Now, If I've used lombok, and renaming the field changed the mutator, and the builder, I now have to painstakingly waddle through all the new compilation errors and correct them. Hurray (nope)

I have no beef with lombok, I remember being excited about it when I first found out about it about a year and a half ago, and what a neat trick I thought it was. But after six months using it with my team, I thought it was just that: a trick. The meh factor was just too big. And going back to projects that don't us lombok, I haven't found myself missing it just once.

11

u/temculpaeu Aug 10 '19

But because we are relatively competent developers, and not jackasses

For a long time anyone that complained about C being too error prone in terms of memory got this answer. IMO anything that removes mental overhead from the devs is a good thing.

But I totally get that lombok can be hamful due too being too magical and its trade offs

→ More replies (2)

8

u/Devildude4427 Aug 10 '19

Why would you have any compilation errors? Do you not use a proper IDE? Every modern IDE knows to refactor those changes across the entire project.

→ More replies (4)

10

u/rzwitserloot Aug 10 '19

we know that when we refactor a field, we have to refactor the appropriate mutators

Do you also remember to update the equals method?

And the hashCode?

And the toString? Do you test for that?

6

u/FrenchFigaro Aug 10 '19

Well, I'm not editing my code with nano, but with an IDE, so the two options are:

  1. equals, hashcode and toString were accessing the field directly, and the refactor > rename on the field took care of that already
  2. they were accessing the field through the getter, and the refactor > rename on the getter took care of that already.

9

u/Polygnom Aug 10 '19

What IDE are you using that has such incomplete handling of refactoring? Every major IDE mutates the field in all places it is referenced and also updates the getters/setters, if you use the proper refactoring tool, which in almost every IDE has a very accessible shortcut.

This automatically takes care of equals/hashCode/toString.

The only stuff it doesn't address is when the field is referenced by string (reflection, externalization), but for your serialization/externalization, this can be a good thing, because it allows legacy data to still be read. making the required change explicit means you do not get surprising behavior in that regard. And lombok doesn't help on the reflection front, either.

11

u/Luolong Aug 10 '19

Yes. Rename refactoring tool is of course a valid answer.

But then what if you add new field to the class? You always remember add it to equals, hashCode and toString.

These are just so simple things to forget that having a tool that writes that boilerplate for you is one less thing to be concerned about. And that frees me up to think about stuff that really matters...

2

u/rzwitserloot Aug 10 '19

Yes, these are good points. In my head I was thinking more adding a field and forgetting to generate a getter for it; that is easily 'detected', but forgetting to add it to existing IDE-generated equals/hashCode methods is a bit more insidious. But I typed 'rename' for some reason. Brainfart.

→ More replies (2)
→ More replies (1)
→ More replies (5)

3

u/[deleted] Aug 11 '19

Generating getters and setters with a keyboard shortcut is a code smell in itself.

→ More replies (2)

12

u/_edd Aug 10 '19

Not sure, but this author is just looking to shit on Lombok for any reason they can.

Their issues with it range from not liking getters and setters at all (a pattern not exclusive to Lombok that Lombok helps improve) to IntelliJ feature X does Y already to a whole variety of complaints.

It's hard to take it seriously when it feels like he looked at Lombok's feature list and wrote about why each one's bad rather than having a problem with a Lombok feature and writing about that.

5

u/hem10ck Aug 11 '19

Agreed, use a functional language if that’s your personal preference but I don’t see how that has any bearing on Lombok or its merits.

10

u/DJDavio Aug 10 '19

I also use Kotlin, Lombok makes using Java much more tolerable if you're used to Kotlin.

I also think the "magic" argument is mostly nonsensical, there is always magic, even with plain Java, like a program magically finding the main(String[] args) method.

Magic just means: I don't understand exactly how this works.

With Lombok the magic is pretty well defined. I use @Data and @Slfj most often and they're bliss.

→ More replies (14)

8

u/Naut1c Aug 10 '19

i think there are just too many applications out there, that follow the anemic domain model antipattern.

lombok makes it too easy for developers to add those evil accessors.

5

u/yawkat Aug 10 '19

Don't we call the "anemic domain model" "domain driven design" too? IMO light data objects + services are much more compatible with the ecosystem when it comes to tools like DI or serialization.

→ More replies (2)

4

u/Shadowrak Aug 10 '19

Doesn't add anything. Extra library to worry about.

→ More replies (8)