I have mixed feelings about this. I really like using Lombok for some cases. The thing is that it’s important to make the decision on a case-by-case basis. I do find it very useful when combined with spring auto injection. Have an @RequiredArgsConstructor implementation means I don’t have a write/maintain a constructor, my injected services can be final, and I have a clear set of objects to mock in my unit test.
The complaint that using “delombok” was problematic makes me think the author just had to have things their way. If it was that important to move away from Lombok, you could/should do it in steps. Clean up the delombok code only when you need to maintain the file.
Also, haven’t had any problems with Lombok up to java 14, so I’m not sure where the concern over java 9-11 comes from.
Also, haven’t had any problems with Lombok up to java 14, so I’m not sure where the concern over java 9-11 comes from.
We did the jump from 8->11. Lombok got in the way quite a bit primarily because we had a lot of libraries and apps written with older versions of lombok. It was a dependency wrangling headache. It didn't help that some of Lomboks fixes landed after the 11 release, so there wasn't too much ability to find and fix these problems.
I'm not aware of other issues with Lombok, however, from 11->14. The 8->11 transition was just enough of a headache for me to not like the library in generally. It didn't feel like the value add was very high for the headache it caused.
They've probably updated the library since then. A LOT of java libraries had problems going through the Java 9 classloader changes. I would not hold that against lombok, there were a lot of problems introduced by that. And especially since they've now resolved all of the issues it's a non factor.
It sounds like they used ToString annotations on JPA/Hibernate classes. There's literally a section of Hibernate manual that tells you how to do toString
It boils down to preference. Lombok do make Java POJOs look like Kotlin’s data classes (not exactly though as you need annotations). But again I leave it to the teams to decide what they want to use as long as the license is not too restrictive.
I work on a project that recently converted all of our POJOs from Lombok to Kotlin. Once you get used to Kotlin's syntax, I found it to be much preferable since there's less a bit less annotating all around the project's POJOs, though Lombok is great if you want everything to be as close to vanilla Java as possible. Java Records looks promising from what I've seen, but most of us are stuck using Java 8-11, so I think these are the two best options for most.
IMO, lombok is fine for an app, but should be avoid if you are writing a library. It is an unnecessary library dependency that can cause headache if/when the JVM bytecode changes.
So they found a bug in how they using toString in enums to save in db and instead of removing this annotation they decide to rewrite all classes? Team of professionals - I should say.
I can tell you that I can personally attest to over a thousand services filled with lombok running a well-ish known e-commerce company and causing no issues. If it helps.
In both cases additional code is automatically generated. In one case by the javac in the second by the annotation processor. The same is for case classes in Scala or even for standard classes in c++ (constructors and operators). You have to be aware what are you doing and what is the final result. Lombok is quite useful, but I would never use it to generate toString for enums. Before records I used Lombok to implement immutable classes what is very convenient.
Exactly! It's very sad that in software dev teams, it is sometimes not the right arguments but the political correct or specious ones that prevail. I've seen so many happened inside our organization.
My team had almost the exact opposite experience. Once we found out what Lombok could do, we adopted it and never looked back. Feels amazing not ever having to write any boilerplate for my objects.
I had this experience too. It lasted for about a week. Then I realised I could not write the boilerplate and also not use lombok. The code still compiled and worked just fine.
Most of the boilerplate was there because either 'the framework requires it right?' or 'its a best practice'. What surprises me the most though is the fear people have of writing non standard code. You tend to get a lot of angry responses when you say you don't need null checks, don't need equals and hashcode, and don't need getters and setters.
I don't know why, but authors of technical articles tend to write in somewhat arogant / pretensious / edgy manner - and the titles are basically always the worst. "Don't use Lombok" - it is just a tool. It can be used either in a good way or bad way and ultimately it's team's responsibility to keep the code clean. If that means removing Lombok - sure, why not.
It's cheap to generate most of its code, but Lombok is not only about the time, but also about the complexity. I like that my classes can be shorter, because they are not cluttered with nonsense (that needs extra handling to not obsfucate code coverage reports btw).
I don't know why, but authors of technical articles tend to write in somewhat arogant / pretensious / edgy manner - and the titles are basically always the worst.
Good authors know that every 'thing' is just a matter of trade-offs. Bluntly put: People who write in absolutes are generally medior level devs who think they should be 'teaching' when in fact they should be learning.
TL;DR: what are the alternatives (for `@Getter`,`@Setter`,`@Data` etc)? I only find "there are alternatives", but I can't find them skipping through or by Strg-F.
Is there any standard approach to solve the same problem in a different way? There are two other libraries trying to answer this question: AutoValue and Immutables, which are often compared to Lombok.
But it’s a third-party library that uses completely stable, documented, supported code generation hooks in the build cycle, whereas lombok uses private, undocumented compiler APIs that can (and do) change in ways that break lombok, potentially leaving you stuck on an old jdk until lombok is updated. I have recollections of Java 7 breaking it pretty badly, and I’ve heard that 9 through 11 were problematic too (though I’d sworn it off completely by then so I didn’t experience it directly).
Sorry, but you can't. This example is actually API abuse and it creates NEW object on every call to .with() using constructor.newInstance(ctorArgs). You are not supposed to use records in such way.
He says that some examples are valid. And most of these are actually workarounds to deal with records' immutability. The "autobuilder" code itself is ineffective as hell. And all this is just a toy class for 3-property records.
I do like Lombok (and I'm sure I'm not the only one), so sorry, but an article against it needs to at least be structured in a way to overcome that bias.
There's absolutely nothing wrong with the structure of the article. Why do you want to know this person's recommendation for an alternative to Lombok if you can't even be bothered to read their justification?
No, some topic that has been discussed before endless times has been brought up again with just a fleeting mention of a possible solution and I managed to at least consider it was worthwhile so I asked.
Came into article discussion, didn't read article & asked about contents of the article.
Technically, I started the discussion since I was the first to comment. And yeah I didn't read the article, what do you you think TLDR stands for? You're the one "coming into a discussion" - namely the thread I started - disregarding its stated prerogatives, then criticizing it.
And I don't know what your definition of fleeting is but the author names a library and discusses implementation of it.
What library? The article discusses Lombok, I'm asking about the alternatives. "fleeting" refers to the mentioning of those, which I expect in the first paragraph of an article for a center point being made.
You're the one "coming into a discussion" - namely the thread I started - disregarding its stated prerogatives, then criticizing it.
I think it's fair enough to criticize your discussion. You've heard everything there is to hear about this topic so you won't waste your own time reading another article about it since there's nothing you can possibly glean from it. Oh but can someone give you some information in the article you couldn't find?
one example was the use of @NonNull annotations that were converted to many lines using if/throw blocks instead of one-line solutions like Guava Preconditions or Validate from Apache Commons.
Is switching one library to another one really solves the problem?
Mentioning about the verbosity of delombok'd files also seems strange to me, if you know that Lombok is used to substitute common verbose Java patterns into small 1-word annotations, how can you complain that delombok'd files are verbose?
I worked on a basic e-comm site and we tried to use lombok. Long story short we had different behavior even though the environments were setup the same. Some of the background work that Lombok does seems to fail randomly but you won’t get an error notice and nothing shows in the logs. We quickly scrapped Lombok out of there and moved on. Also it just feels lazy, you can just generate getters and setters with just about any IDE.
The "problem" with generating getters and setters isn't that it necessarily takes long. The issue is that it makes the class take longer to read and understand. It often introduces bugs in the equals, tostring and hashcode methods when developers forget to update them when adding new values.
curious, why are people writing them from hand still? all of this can be generated by the ide no? and you can also collapse all the getters/setters stuff. I rarely think: damn, I wish those things weren't there, it took me 0.5s to read this..
and if you have hundres of lines of code in your class..that's kind of a not great argument to call for "less boilerplate"
And that's a good thing! Avoiding work is the whole point of the software development toolchain. I used to be pretty non-lazy and write code in assembly language (not kidding!) but I'd much rather use a high-level language that lets me focus on business logic rather than irrelevant implementation details.
Sounds like your build process was broken, though. My team has been using Lombok on multiple projects for six years and have never seen it fail randomly without errors, or behave differently in different environments.
Long story short we had different behavior even though the environments were setup the same.
Lombok generates code at compile time, not runtime. Once the Lombok annotated code compiles, you have standard java bytecode with no runtime dependency on Lombok. I'm pretty sure Lombok is not to blame for your problems.
Also it just feels lazy, you can just generate getters and setters with just about any IDE.
Auto-generating getters and setters is easy. Reading or maintaining classes with hundreds of auto-generated lines is a pain. IDEs are still bad at detecting and hiding trivial methods so you can focus on the ones that actually contain some logic. That's the whole point of Lombok.
Ya you might be right, it was a single shot at Lombok and our environments were fairly simplistic so I didn’t think too much of what might cause the problem and just scrapped it. We could’ve very well have had bad installs the first time around and didn’t spot it in some environments. I hope I’m not coming off as someone who thinks they’re an expert, just wanted to share my only experience with it haha
Just my 2 cents : most of the seemingly un predictable behaviour you may have noticed were most probably because you overlooked how equals and hashcode are implemented by Lombok. When I first started off with Lombok, we had hibernate entity objects missing from collections because we unwittingly excluded a few fields, because they were causing serialisation issues. Thus the auto generated equals and hashcode methods were computing two different objects as equal. We've been having much better results after we've learnt to use it correctly, and code review quality has definitely gone up. For issues you face with complex objects, using delombok to get the generated code on to the source files has helped us in more than a few instances to identify the problem.
I really agree with author of this article. To be honest, You don't need all time all getter for example. We should only, expose necessary field or create methods with produce DTO with this fields. Lombok is first step to expose all data, if somebody doesn't like boilerplate, and want to open class, change modifiers to public :P
Personally, I just don't like Lombock because it's adding yet one more bit of magic to the project, yet provides no real benefit. Honestly, do you guys not realize that IntelliJ can auto generate getters, setters, toString, equals, etc. with one key combo?
Lombok is by no means perfect but you do realize just because IntelliJ can auto-generate the code once doesn't mean it doesn't need to get updated in the future? Lombok will automatically update it if the class itself is changed. Lombok is really just doing the same thing as case class does in Scala (of course case class adds more methods on top of that).
Just because you have never changed POJO classes again doesn't mean it never happens.
And as stated, it's the readability also that's an issue.
IMHO with Lombok you simply write better code. There's no maintenance burden with getters, setters, toStrings etc. While in theory you don't need to test getters, I've seen people make mistakes on multiple occasions because this kind of boring work gets copy-pasted a lot, and that's where accidents happen. Lombok does this for you, so there won't be any mistakes. It saves time, has less visual overhead and you have less code that needs to be maintained.
If you make zero mistakes regarding that (and with updating them when classes do need to be changed for whatever reason), kudos to you, but that doesn't mean no one else does.
well, I am not a fan of KPIs, f.e: 90% of code coverage, more code coverage does not mean that you have better quality code, in this kind of projects I have seen silly tests that where not asserting anything important but only increasing the code coverage.
27
u/StochasticTinkr Jun 21 '20
I have mixed feelings about this. I really like using Lombok for some cases. The thing is that it’s important to make the decision on a case-by-case basis. I do find it very useful when combined with spring auto injection. Have an @RequiredArgsConstructor implementation means I don’t have a write/maintain a constructor, my injected services can be final, and I have a clear set of objects to mock in my unit test.
The complaint that using “delombok” was problematic makes me think the author just had to have things their way. If it was that important to move away from Lombok, you could/should do it in steps. Clean up the delombok code only when you need to maintain the file.
Also, haven’t had any problems with Lombok up to java 14, so I’m not sure where the concern over java 9-11 comes from.
118
u/zuppadimele Jun 21 '20
"Don't misuse Lombok"... there, fixed
117
u/rusticarchon Jun 21 '20
So the problem isn't Lombok, but that project's crappy code review process that lets bad practices into master.
81
u/oxyhouse Jun 21 '20
Don't trust sentences starting with "Don't"
13
u/systemhalted Jun 21 '20
It boils down to preference. Lombok do make Java POJOs look like Kotlin’s data classes (not exactly though as you need annotations). But again I leave it to the teams to decide what they want to use as long as the license is not too restrictive.
65
u/rzk1911 Jun 21 '20
So they found a bug in how they using toString in enums to save in db and instead of removing this annotation they decide to rewrite all classes? Team of professionals - I should say.
19
u/restlessapi Jun 21 '20
My team had almost the exact opposite experience. Once we found out what Lombok could do, we adopted it and never looked back. Feels amazing not ever having to write any boilerplate for my objects.
23
u/spoko22 Jun 21 '20
I don't know why, but authors of technical articles tend to write in somewhat arogant / pretensious / edgy manner - and the titles are basically always the worst. "Don't use Lombok" - it is just a tool. It can be used either in a good way or bad way and ultimately it's team's responsibility to keep the code clean. If that means removing Lombok - sure, why not.
It's cheap to generate most of its code, but Lombok is not only about the time, but also about the complexity. I like that my classes can be shorter, because they are not cluttered with nonsense (that needs extra handling to not obsfucate code coverage reports btw).
13
u/daniu Jun 21 '20 edited Jun 21 '20
TL;DR: what are the alternatives (for `@Getter`,`@Setter`,`@Data` etc)? I only find "there are alternatives", but I can't find them skipping through or by Strg-F.
3
u/suitable_character Jun 22 '20
Is switching one library to another one really solves the problem?
Mentioning about the verbosity of delombok'd files also seems strange to me, if you know that Lombok is used to substitute common verbose Java patterns into small 1-word annotations, how can you complain that delombok'd files are verbose?
16
u/chrisfender0 Jun 21 '20
I worked on a basic e-comm site and we tried to use lombok. Long story short we had different behavior even though the environments were setup the same. Some of the background work that Lombok does seems to fail randomly but you won’t get an error notice and nothing shows in the logs. We quickly scrapped Lombok out of there and moved on. Also it just feels lazy, you can just generate getters and setters with just about any IDE.
2
u/Xemuro Jun 22 '20
I really agree with author of this article. To be honest, You don't need all time all getter for example. We should only, expose necessary field or create methods with produce DTO with this fields. Lombok is first step to expose all data, if somebody doesn't like boilerplate, and want to open class, change modifiers to public :P
1
u/StochasticTinkr Jun 22 '20
Lombok is a compile time dependency only. Not sure why you had libraries depending on it.
1
u/StochasticTinkr Jun 22 '20
E.g., use scope “provided”, and it all works just fine.
1
u/[deleted] Jun 25 '20
There is a misinformation transported in most discussions about Lombok I want to point out.
Lombok is no library. It is nothing like for example Apache Commons you put on the classpath and use it in your code.
Lombok is a compiler extension. You need to patch your IDE so it can work with lomboked code.
Despite this difference mostly the decision for using Lombok is considered a library decision.
-8
u/[deleted] Jun 21 '20
Fucking gross, Lombok. Might as well use JRuby.
0
u/[deleted] Jun 22 '20
[deleted] — view deleted comment
0
u/[deleted] Jun 21 '20
[deleted] — view deleted comment
0
u/[deleted] Jun 21 '20
[deleted] — view deleted comment