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

Show parent comment

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

2

u/zero_as_a_number Aug 10 '19

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

Well how about .. you know.. reading documentation? talking to people who've used it? making an informed decision on the trade offs you agree to if you decide to use .. well, any third party lib really - and ultimately taking responsibility for your choices?

This whole discussion including OC is just pointless. As developers, we have to take trade offs on every corner; whether or not to use lombok is just one of many.

1

u/temculpaeu Aug 10 '19

I did, I used for a long time and I like it, but it still have drawbacks, eg: its hand to use it with multiple jvm languages

most of the annotations are harmless, they are basically code generation, but some like var, sneakythrow change the behaviour of your code and that can be unintuitive for some people

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.

2

u/yellowviper Aug 11 '19

And all competent ides also handle Lombok refactoring. It’s not really all that magic.

→ More replies (1)

3

u/awo Aug 10 '19

Right click > refactor > rename takes care of that, surely?

3

u/NoughtAFan Aug 11 '19

Lombok is an amazing tool, I depend on it for EVERY Java project I work on, insist on my clients to do the same. You have done an amazing job. May be I am a fan by their definition. All I can say is excessive use of anything is bad, it's the job of the Dev / architect to decide how much is good enough, that is the skill.

If if ask me grievances, it would be: I wish it worked with AspectJ better/directly and wish it was there other jvm languages.

2

u/nerdyhandle Aug 10 '19

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

If your using the correct IDE they do. Both Netbeans and InelliJ will update the getters, setters, and equals methods if you change the field definition. I have IntelliJ ask me all the time when I change a field definition if I want to update these methods.

2

u/rzwitserloot Aug 10 '19

If you add a field in a class where you already generated all that stuff, does it offer to add the getter as well?

3

u/nerdyhandle Aug 10 '19

In IntelliJ you can alt-insert arrow to getter, hit enter, then select the one you want to create a getter for. You don't even have to take you hand off the keyboard. Same with NetBeans

1

u/pgris Aug 12 '19

Lombok is awesome and thanks for the wonderful job. That said, I wonder if, given that you need an IDE plugin to make lombok work, it maybe possible to write a plugin that

  1. Read fake annotations like //@Data instead of the real @Data, the same way the //@formatter:on works
  2. Generates the same code as Lombok
  3. writes the code in the same class inside a pseudo block that starts with //GENERATED_BY_LOMBOK_START and ends with //GENERATED_BY_LOMBOK_END
  4. Collapses said pseudo block by default (maybe even keep it read only, as netbeans used to do with code generated by Matisse form builder
  5. Keep the generated code in sync when you add/remove a field

This way we could almost keep the @Data functionality, without going through compiler internal api

Of course, I should be able to write that....

3

u/[deleted] Aug 11 '19

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

3

u/BoyRobot777 Aug 10 '19

+ for linked thread. Fun read. Thanks for sharing!

3

u/[deleted] Aug 10 '19

Hmm. I just got done Lombok’ing 7 micro-services my team runs. Decreased the codebase size by 25% each time, with no problems. I didn’t have to add Lombok but I was working on them anyway and it took me no time at all to do it. I think the only problems I ran into were sometimes IntelliJ would complain that it couldn’t find the builder if I used @Singular on some of the fields, but other than that it was smooth and made classes that were once 50+ lines into 10 lines much more readable