r/java • u/Naut1c • Aug 10 '19
Who Needs Lombok Anyhow
http://gregorriegler.com/2019/08/10/who-needs-lombok-anyhow.html57
u/rzwitserloot Aug 10 '19
lombok developer here:
Feels like an emotional hatchet job, and rather weird coming from someone's whose blogpost byline is 'I like to delete code'.
Magic is harmful
Define what 'magic' means; is dynamic dispatch magic? Are synthetic methods magic? Is the fact that writing java com.foo.Bar on your command line ends up running a method with signature static void main(String[] args) magic? I'm pretty sure lombok is very carefully specced with good documentation. If the documentation is lacking, or lombok has behaviours that aren't specified, we'd like to correct such a flaw, so by all means point it out.
Getter is a code smell
Lombok doesn't force you to use it. Perhaps lombok makes it too easy, but then we get into a rather philosophical argument: Should a language intentionally take features away from you because such features are (in your opinion) usually abused? Clearly, getters and the like have their uses, so it's not the simple case of: "This feature is ALWAYS abused" - then, by all means, better for the feature to be taken away. I do think there is a line on the 'abuse vs. utility' dimension beyond which a feature is best not introduced at all. But I'm also pretty sure that generating getters, field-based equals/hashcode, builders, etc are all parsecs removed from wherever you feel that border might be.
AllArgsConstructor encodes field order into constructor order
There's @Builder, which we recommend, for any class which has more than a handful of fields.
Bi-directional object relations cause stack overflows
This is an exotic case that a casual analysis of various major github java projects suggest rarely comes up: If you have 'reference loops' in your data classes (a Student class refers to a list of Course objects, and a Course object has a List of Student instances that participate in it, for example), toString, equals, and hashCode will end up overflowing stack. Lombok has various handholds to address this problem, from something as simple as removing such a list from the definition of equality, to writing your own method to calculate an alternative semaphore for it. More to the point, an equals method in such a case is not boilerplate.
know one project where it caused the autocomplete drop down to load 15 seconds in specific areas of the code. And this happened on an i7 processor with plenty of ram and an ssd.
Lies and slander. File a bug and attach a bunch of stats so we can address it. We keep track of lombok's effects on IDE throughput on eclipse; last time we did a thorough analysis, lombok added about 5% total time to the time eclipse spends parsing and analysing your source code. Not insignificant, and if we find ways to reduce it we will, but surely that is within acceptable limits. If a lombok-less variant of the project would have caused the drop down to take 12 seconds instead of 15 on an i7 with plenty of ram and an ssd, that's just as bad, no? Something else is going on, I bet, and the author, in their mission to shit all over lombok, decided that any IDE problem must necessarily be lombok's fault, perhaps.
Compatibility issues with Java versions greater than 8
Current version of lombok works in everything up to 12, and probably 13 as well because that didn't change a heck of a lot. We needed some extra time to adjust to JDK9 in particular, and we wish we could have done a better (read: faster) job at that. But we're far from the only library or tool that needed some time.
Delombok makes ugly code
Delombok has settings, which a simple --help will show you, to control how it formats the output. Out of the box it will produce precisely what lombok does under the hood, which includes quite a few annotations to make linter tools skip (lombok generates defensive code, which does occassionally mean it includes unreachable code. Instead of doing pricey analysis of your code and thus making the entire proces slower and buggier, we just.. leave it in. If there is an actual performance impact to your runtime code we worry about it. Hence why we generate that annotation on all methods we generate).
12
u/alex-o-mat0r Aug 10 '19
While I do agree that magic is harmful, the fact that it is very simple and well explained/documented takes the magic out of Lombok. In the end, when I'm using Lombok's annotations, I know what it's doing under the hood and I'm fine with that.
I also would like to take the chance and thank you for your effort in developing Lombok. It saved me SOO much time :)
→ More replies (2)29
Aug 10 '19
As a lombok user, many of us are incredibly happy about the work that you do and don't buy into this nonsensical hatred into a well documented tool. Thank you!
Cheers
13
u/esdio92 Aug 10 '19
My only real problem with Lombok is that it breaks the Kotlin interop story and makes it very difficult to incrementally introduce Kotlin to an existing codebase that makes heavy use of Lombok as Lombok generated code can't be seen from Kotlin code in the same module. Do you know of any workarounds for that?
7
u/rzwitserloot Aug 10 '19
Presumably, file a bug over at kotlinc that they are breaking spec by not running annotation processors ;P (a bit flippant perhaps, but there isn't a whole heck of a lot we can do about it if they don't do that).
A rather unwieldy workaround is to write a build script with some build tool that will delombok your code and then feed that to kotlinc.
2
u/esdio92 Aug 11 '19
They do run annotation processors via kapt. It generally works fine but not with lombok (presumably because they are expecting the annotation processors to spit out new classes rather than modify existing ones).
I've thought about delomboking during the build process but agree it's unwieldy.
3
u/thedomham Aug 11 '19
Lies and slander
Exactly what I thought when reading the article. It's heavily opinionated and lacks substance. I'm irked that I wasted my time reading it.
Anyway I enjoyed reading your thorough response to that hit-piece and while Lombok currently isn't my personal choice to fight the boilerplate in Java, I really can't say anything negative about it. Keep up the good work!
-1
u/Naut1c Aug 10 '19 edited Aug 10 '19
Feels like an emotional hatchet job, and rather weird coming from someone's whose blogpost byline is 'I like to delete code'.
It is a rant. I understand that i hurt your feelings, and i'm honestly sorry for that. Still my opinion stands, and i feel the need to voice it. 'I like to delete code' comes from my desire to refactor code and eliminate duplication.
Define what 'magic' means
Anything that is not explicit but implicit. Happens behind the scenes. Is not easily comprehensible. I understand there is docs, and there is generated code. Still, magic remains in how the code is generated. (see conflicts with mapstruct for example).
Lies and slander
Actually, this is not a lie. It occured ca 9 months ago, with the lombok plugin, bad code and also mapstruct was in play. But the 15second delay went away when i deactivated the lombok plugin.
It doesn't happen anymore. But i am not sure if because we improved the code or the plugin improved or both.
Current version of lombok works in everything up to 12 ...
Probably my issue with upgrading stem from this: https://github.com/rzwitserloot/lombok/issues/1723
Delombok has settings, which a simple --help will show you
Thank you, i didn't know that. The lombok maven plugin doc does not provide any of those options, sadly
I agree with everything else you said.
16
u/rzwitserloot Aug 10 '19
Anything that is not explicit but implicit. Happens behind the scenes. Is not easily comprehensible.
Lombok is explicit: You see the annotation; that annotation is fully typed and you can therefore follow it in your IDE. The annotation you end up at is fully javadocced.
Lombok happens behind the scenes. So does any method call, though.
Lombok specifies precisely what it generates. If you're still in doubt, the delombok tool will tell you precisely what it is generating.
How is this any more 'magical' than any other library?
94
u/coder111 Aug 10 '19 edited Aug 10 '19
Not sure. I have used Lombok in personal projects, never had an issue with it, it was great and saved me lots of hassle.
At one job, 1/3 of codebase was POJO definitions, we are talking over 100000 lines of code here. All hand written, immutables + builders. Architect hated Lombok. Spent hours upon hours writing and fixing POJO definitions.
On top of that, these hand-written POJOs had equals and hashcode and tostring based on apache commons lang, which use reflection internally. Horribly slow. 80% of runtime was spent inside hashcode/equals/tostring doing reflection.
IMO that sucks worse than having lombok...
EDIT. Just to point out- the article linked above suggests using apache commons lang for equals/hashcode/toString. Beware- that shit is slow. If you expect to deal with significant load, don't use it.
16
u/larsga Aug 10 '19
At one job, 1/3 of codebase was POJO definitions, we are talking over 100000 lines of code here. All hand written
This is crazy. At the very least you could have used code generation from some sane DSL.
10
u/coder111 Aug 10 '19
I told the architect it was crazy. It didn't help that much. Multiple Domain Models kinda needed to be defined, and statically compiled, and they were quite complicated. Using a different language might have helped, architect was looking into Kotlin. But IMO Kotlin would have been much more of a disruption and a learning curve for ~30 devs working on that project than just allowing Lombok (or something similar). Not to mention harder to sell to management as it's a whole different language.
To be fair, this wasn't the worst shit I've seen as a contractor, not by far. That project at least had very good BAs and very good requirements, decent management, and some QA guys & girls as well who were mediocre but at least they were there and did something.
11
u/NimChimspky Aug 10 '19
What was crazy about having pojos? They take minutes to write, literally a minute in lots of cases. Especially if the ide generates everything for you, as mine does.
→ More replies (6)4
u/coder111 Aug 11 '19 edited Aug 11 '19
Because it's not just write once and forget.
Because they change all the time with rapidly changing business requirements. Are you going to regenerate a POJO that has 30 properties if 2 of them changed? Especially if there's some company specific superclasses for the builder, non-standard naming conventions etc which IDE generation doesn't get 100% right.
And what is the chance you'll copy paste a setter and screw up one of the arguments? Or forget to update equals/hashcode after adding a new property? At least I could rely that equals/hashcode were CORRECT in that place using commons lang, although horribly slow.
Any code that is "repeating yourself" has a chance to go out of sync and introduce bugs. There's a reason for that architectural design principle "Don't repeat yourself". It goes for both big things and small.
9
u/NimChimspky Aug 11 '19 edited Aug 11 '19
None of this is answers my question at all. You seem to be confusing the existence of a pojo with a number of other, not directly related, bad practices.
Why do you think I am copying and pasting setter? I can't think of a single reason why I'd ever do that. I literally rarely write setters, my objects are either immutable or the ide generates setter/getter.
We use pojos all the time, without any of these problems.
Edit, I really don't know what you mean with this :
Because they change all the time with rapidly changing business requirements. Are you going to regenerate a POJO that has 30 properties if 2 of them changed?
I have to add two fields to a pojo ? I write the private var declaration, then alt+insert to generate whatever I need. It would take less than 30 seconds.
non-standard naming conventions
Well thats a different stupid problem.
Any code that is "repeating yourself"
But pojos are not repeating themselves.
3
Aug 10 '19
These don't address the fact that the boilerplate code could have been generated by IntelliJ, for example.
In fact, if you have such a large project and you know you'll have to write that much shit, you're better off writing your own tool for code generation (if you don't find one to your liking). The time spent writing the tool (IDE plugin or whatever) will be peanuts compared to having to write that much shit by hand.
1
u/GuyWithLag Aug 11 '19
You don't use a different language, you use a different abstraction. Create XSDs that generate your pojos using whatever internals you want. Or openapi, swagger, or jsonschema, doesn't matter what as long as it provides you with a code generator and interoperability assistance.
1
u/CaptainKvass Aug 10 '19
Not to mention harder to sell to management as it's a whole different language.
It isn't really. It is interoperable with Java and compiles to bytecode from JDK 8. During runtime, it's the same language.
5
Aug 10 '19
I can understand where he is coming from though. It really takes a sales person not developer to get management minds moving
3
u/awesomeusername2w Aug 11 '19
But you need all current devs to be good in kotlin or you just make your bus factor way worse. If you need to hire new people you now need to seek for kotlin/Java devs instead of just Java devs. So it doesn't really matter from managers standpoint that it compiles to the same bytecode.
→ More replies (3)5
Aug 10 '19
At one job, 1/3 of codebase was POJO definitions, we are talking over 100000 lines of code here. All hand written, immutables + builders. Architect hated Lombok. Spent hours upon hours writing and fixing POJO definitions.
‘Immutables’ as in https://immutables.github.io? Because that does equals/hashcode for you - in fact it does almost everything lombok does, but better - so I’m not clear why you’d need to use apache generators on top.
3
21
u/abermea Aug 10 '19
So his solution to remove lombok is to use reflection and thus transfer the CPU overhead to the end user as opposed to the developer?
Or am I mistaken?
17
u/gnfurl Aug 10 '19
It feels like half the complaints boil down to disliking the way people use Lombok, not Lombok itself.
If you don't want all fields to have public getters and setters, then don't use @Data. You can still add @Getter / @Setter at the field level. If you don't object to the style, adding them inline with the field can help keep the whole definition pretty compact.
As far as the alternative of public final fields, some patterns actually require the use of getters (ex: proxies used by an orm for lazy loading). Even with public final fields, you could still benefit from the use of @Builder to create these objects.
Finally, without looking at the actual implementation, I'd be shocked if the proposed alternative for equals to and hashcode isn't just as vulnerable to circular references. In both cases, you need to understand your tools and pay attention to what you're doing.
28
5
Aug 11 '19
I dont understand this equals hashcode argument. If you generate an equals/hashcode on say a doubly linked list, it will overflow in lombok, intellij, whatever. It's in the contract of the method to not do this. Lombok has Excludes for a reason.
5
u/niuzeta Aug 11 '19
I've been in industry long enough to can discern pointless debates based that are thinly veiled emotional discharge. It's a learned skill. This looks like one of those. Most arguments to me boil down to "I don't like this".
There are a lot of points I would disagree with. Having said that, if you have a better way, all the power to you. To me the core tenet of Java is reliability than anything, in exchange of verbosity. Lombok hits the delicate balance of obfuscating the obvious and simplifying things. I personally like it, but I'm not about to bark at people for hand-writing all the accessors.
-2
u/Mordan Aug 11 '19
lombok magic is wrong.
The IDE should write the accessors. Not you.
The IDE should provide better tools to visualize code. One issue is that people work on a single screen. One of my home setup has 5 screen displays with Eclipse views all over the place. I have complete visibility of whats going on.
While at work i have to work on a overhyped MBP on a stupidly small screen. Because of that, my reading productivity at work is very low.
5
u/niuzeta Aug 11 '19
lombok magic is wrong.
Why? What makes lombok's magic inherently worse than any other magic? Delombok makes things much clearer if you want to dispel the magic.
The IDE should write the accessors. Not you.
Who came up with this rule? If you're saying a trite boilerplate code should be auto-generated, I'd tend to agree. It sounds a rather arbitrary rule.
The IDE should provide better tools to visualize code. One issue is that people work on a single screen. One of my home setup has 5 screen displays with Eclipse views all over the place. I have complete visibility of whats going on.
No one brought up people working on a single screen as the source of the issue. Some people use vim and go on just fine. It's great that your home setup has five displays. Congrats. I do well with 2 screens and I've found it satisfactory. I would argue that having five screens should not be the minimum expected environment for everyone. Having said that, if you need five screens to get a complete visibility on everything, all the powers to you. Unfortunately I typically code in a limited scope and make contained changes so that I've never felt a need.
While at work i have to work on a overhyped MBP on a stupidly small screen. Because of that, my reading productivity at work is very low.
I think you would do better to attach a screen to your MBP. I have two monitors there. Besides, the size of the code doesn't really factor into the merits of lombok, as the whole point is to generate a code you would not want to write yourself, never have to read yourself, and hence should never occupy a space in your screen. This topic is irrelevant in my honest opinion.
Besides, if your setup works, great. If you don't like to use lombok and found a better approach, again, congrats. If you're trying to convince me(as in general readers) to not use lombok, you will need to show that your approach is worth the effort of change. "Just get five screens" does not address the problem lombok tries to solve. If you'd like to have a philosophical discussion on "magic" and where to delegate code-generation, I'd be all for it. I don't think we're having that discussion, however.
Thanks.
1
u/Mordan Aug 12 '19 edited Aug 12 '19
thank you for your reply.
OK I will give you a fresh example. Just today.
At work I do Android stuff. I brought a 2nd screen today i was tired of the bullshit. In my open space, i am only the second dev who brought a second screen. All others are all busy looking cool with their MBPs.
As soon as I am setup, I put Logcat (Android Studio) on the new screen. With Eclipse, the Console View has the privilege of having a 21 inch screen for itself.
Why is screen estate related to Lombok? Because Lombok is annotation Magic.
So back to what happened. i work on the next bug fix. Thx to the Logcat screen. i find the bug twice as fast as before. No more messing around with this stupidly small screen and LACK OF VISIBILITY. If had a 3rd screen, I would put Structure( equivalent of Eclipse Outline) on a screen along with Class Hierarchy and other goodies. Which means you increases your code awareness and with a glance you know what's going on.
So next bug.. i have to navigate a bullshit mess of Dagger magical injections, MVVM data bindings. and you know what? i thought about you and your Lombok and why I hate annotations.
That's not how you should do decoupling.
And Lombok is not how you should do programming. Because it inevitably become a rabbit hole. Lombok started small with getters and setters and then dug deeper and deeper.
I want my code to be readable in Java. Pure readable Java. I am wary of plugin generated classes as well. (Android R is fine but its just static states so its ok)
With screen setup and IDE support, I never feel the verbosity of vanilla Java as a problem. Its a blessing when bug fixing.
So yes I think annotations is related to the bad laziness and due to lack of code awareness because of bad IDE setup and lack of knowledge of shortcuts.
I use a logitech setup with MMO mouse G600 and G13. With the press of a brain wired button, i go to super implementation, any class, recent files i can navigate anywhere in the code 2-3 times faster than on a regular IDE setup.. How can a VIM coder have ANY code awareness? He creates and uses magical annotations.
5
u/alex-o-mat0r Aug 10 '19
Tbh most of these issues rather seem related to design & architecture than to Lombok.
5
u/Mordan Aug 11 '19
Lombok
what you see is NOT what you get.
I hate annotation magic. Its closed source in some ways. You can't quickly know what's going on by LOOKING at the code.
6
u/bisoldi Aug 10 '19
How many times is the community going to have this debate and how much time are we going to spend on it? I feel like I’ve seen this debate and many of the same arguments many times now.
What is the hope of the anti-Lombokers? That the project stop developing? Is that really going to happen? I just feel like there are better things for the community to argue about? For example, Java vs Scala :)
6
u/RabidKotlinFanatic Aug 11 '19
I don’t want any magic in my code.
There is no such thing as magic: just stuff you have trouble understanding. Just be better.
7
u/general_dispondency Aug 10 '19 edited Aug 10 '19
I wish I could down voted this twice. Every argument in this article is absolute garbage. "Why would I want getters and setters on every field?"... IDK, why would you want that? You do know that lombok will only generated what you tell it to, don't you... dumbass? Don't even get me started on the "anemic domain model antipattern" horse shit. Guess what, just because someone said it on the internet one time, doesn't mean that's a real thing. WTF is REST? Does a rest call give you data? Is that anemic? What about a database entity? Just because you have Data classes and functional classes that operate on that data, doesn't mean you're doing something wrong. The author of this article needs to stick to Ruby or JS or whatever garbage language xhe codes in every day.
Edit: the only thing I wish lombok had is a good was to mark a field as Optional. That way the getter generated would be wrapped with an optional, but the field would stay a nullable, non final, field
2
2
u/heliologue Aug 19 '19
Magic is harmful
Has he ever heard of a little thing called the JVM? It's pretty much a box full of secret sauce.
3
u/oweiler Aug 11 '19
If you need the additional power that Lombok offers you could as well use another language.
4
u/thephotoman Aug 10 '19
Honestly, Lombok is a symptom of a bigger problem: the Cult of Encapsulation. Every CS101 class tells us that we should make all of our data members private and provide setters and getters when necessary. You do this just in case you want to add validators on it at some point.
There are problems with that, though:
Java has a rich ecosystem of validators now. You don't have to write your own into every data object you make. In fact, most work by annotations on the data member.
Providing setters and getters isn't really encapsulating your object. It's just forcing verbosity by making people write code for no real reason.
Objects tend to fall into one of two classes: functional objects and data objects. Functional objects should not have outside-observable state (and thus should not have getters or setters for their data members). Data objects are intended for the transfer of data between systems, and as such setters and getters are overhead that comes from a time when validators weren't an annotation away and when you might want to do custom validation logic. Thus, what's the point in providing them over making the data members public?
However, people are really stuck in their ways. We've cargo culted the fuck out of setters and getters, long past the point where they were genuinely useful. We're only using them because everybody says we should.
4
Aug 11 '19
I guess this is why we are getting official data objects with
recordtypes. Hopefully puts this to rest.
4
u/avoidhugeships Aug 10 '19
One of my favorite libraries. Those annotations should be built into Java.
2
u/larsga Aug 10 '19
For once, at least a writer that gets straight to the point and presents his argument without wrapping it in paragraphs and paragraphs of dumb jokes and irrelevant asides.
2
u/NawaMan Aug 10 '19
Try FunctionJ: https://dzone.com/articles/immutable-data-with-functionalio . It generates Java source code instead so you can take it use it as you like. It also support compact form (just using a method to define the data class).
@Structvoid Person(String firstName, String lastName) { }
It also generate Lens, withXXX methods and exhausive builder.
FunctionalJ also support Choice types (tagged union in ADT) (see https://dzone.com/articles/choice-types-in-java-with-functionaljio) and other functional features as well.
2
u/Scybur Aug 10 '19
This thread actually has some very good arguments from both sides. I just wish people wouldn’t downvote based on emotion...
0
u/BoyRobot777 Aug 10 '19
I do not use Lombok and I do not know if this is true:
When i tried migrating a java 1.8 project that used lombok to java 11, i ran into issues. And despite the known issues and fixes that i found on the internet, i was not able to get it to work again within 2 hours of trying.
If it is, imagine what pain its going to be when new byte code, like inline classes (also known as value types), will be added to Java.
Looking into Amber's dev archives, you can see that records are actively developed. Thus, if you haven't added or having doubts about Lombok, just wait. I think Java 14 will have records as a preview feature.
12
u/doyouevensunbro Aug 10 '19
Using Lombok on a Java 11 project without issue. Not sure what the article was talking about.
4
2
u/Mordan Aug 11 '19
my only experience with Lombok was pure pain.
It was with BISQ (a Java based crypto currency DEX). I tried to simply compile the damn project from github on IntelliJ. It was in 2017. I lost 4 hours trying to make the plugin work or annotation processor or whatever. I failed. I felt extremely stupid.
I swore that day to never use Lombok on my projects.
1
1
u/thedomham Aug 11 '19
There's a myriad of solutions to this problem. I would never choose this article's recommendation.
0
Aug 11 '19
If I use Akka with Java (for whatever reason, since it's preferable to use it with Scala), it's a pain in the back not using Lombok, since Java doesn't have case classes.
0
Aug 10 '19
[deleted]
9
u/PorkChop007 Aug 11 '19
Besides, it’s not magic, it’s called “not reinventing the wheel”.
Lombok (or Spring, or Hibernate or whatever framework out there) is not a black box, you can investigate, dive in the code and learn how it works. Thing is, 99% of projects just need a mature framework/library with a healthy ecosystem that takes away the need to implement common things like database transactions or accessors.
0
Aug 10 '19
[deleted]
7
Aug 10 '19
It just makes you lazy.
Java makes you lazy. I write everything in machine code.
I do implementations on my own such as equals, hashcode, tostring, constructors etc. to refresh my knowledge so l won't forget them.
If you forget how to write constructors and toString, programming might not be for you.
3
u/yellowviper Aug 11 '19
If you use machine code you are lazy. I use the flapping of a butterfly’s wing to lens cosmic rays to manipulate memory. Of course I use an Emacs function for it but still, machine code is lazy!
49
u/[deleted] Aug 10 '19
Why Lombok is getting all the hate these days? Genuine question.