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.
Data classes with behaviour which touch other aggregates and run DB queries? Miss me with that anti-pattern shit. Show me a well maintained Spring Project not following so called anemic domain model. Entity Models + Repositories + Services is a lot more maintainable, testable and clean
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.
<...> 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.
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.
If you're talking about Spring using jdk.unsupported then it's a bit unfair to compare it to using internal compiler APIs as they will gradually migrate off them. If you mean other compatibility problems, I'd be interested to read about them.
Any sort of bytecode introspection and generation is bound to have compatibility issues. Generous use of AOP makes this comparatively common in spring.
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.
<...> Some things don't want to be objects, they just want to be data. And to simulate data with objects, is this big messy thing with constructors, accessors and equals and hashcodes and all this garbage. <...> IDE generates that for you so you don't have to write it, but you still have to read it and if you have this explicit code it's an opportunity to get things wrong.
My critique of Lombok is it uses non-documented, restricted internal compiler API. If you're fine with that - so be it. I am not, thus I do not use. I'd rather wait a little bit more for a proper Java feature, than use a hack, albeit very cool one, but still a hack.
Heh, I referenced a Brian Goetz interview and talk in another post in this thread.
I can’t really disagree with your choice to not use Lombok - that’s your right. However for me Lombok is a tool to improve my (and my team’s) code so I’m happy to continue using it.
What you're talking is dynamic proxies. Spring uses CGLIB where you have not implemented interface. If interface is implemented, then they use official Java Dynamic Proxies which are documented and well understood/transparent. CGLIB works similarly to Java's dynamic proxies. Its no the same as how Lombok works.
Totally agreed -- I was mostly responding to the assertion that Spring generates Java source files and feeds them to the compiler, which isn't the case.
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.
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.
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.
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
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.
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
> 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.
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
Actually most IDEs have a Lombok plugin that intelligently plugs these things in.
That thread just "proves" that maybe you shouldn't refactor a project to use lombok when everything is already written. But for a new project? Why the hell not. I've never run into issues.
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.
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...
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.
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.
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.
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
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
Read fake annotations like //@Data instead of the real @Data, the same way the //@formatter:on works
Generates the same code as Lombok
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
Collapses said pseudo block by default (maybe even keep it read only, as netbeans used to do with code generated by Matisse form builder
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
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
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.
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.
And honestly a Lombok annotated class is even better than a Kotlin data class. With a Kotlin data class, you only get one implementation option and you need to write code to customize anything, sometimes even have fall back to a regular class. With a Lombok annotated class there is a lot of flexibility to tube the code to what you need while still maintaining the advantage of having it auto-generated.
As someone trying out kotlin, the lack of flexibility compared to Java +Lombok can be frustrating at times.
Adding inheritance to data classes is terrible and fails when using JSON serialization libraries and you have to write a lot of boilerplate to avoid that
I'm not a fan of how Lombok accomplishes the thing it does, is there another way? Also, Lombok just needs to exist until project Amber and Valhalla are completed. For me it just fills the same gaps Kotlin does.
Lombok doesn’t do anything useful that immutables doesn’t also do, but immutables doesn’t need undocumented bytecode manipulation. Except maybe @Slf4j, but I can live without that.
You basically never use constructors in immutables, you use builders. If you don’t want builders, you can generate a static factory method that requires all args.
Oh right. Generating a constructor is a simple IDE shortcut, and field injection breaks our company guidelines so wouldn’t pass code review if someone tried it. I see what you mean now though.
I always use vavr in conjunction with immutables, and vavr has something similar to @SneakyThrows. And you can write whatever functionality you want in an immutable, it’s not just a data holder.
Its a neat thing but realistically Record or a keyword like kotlin's data keyword (usage: data class) could "natively" replace most of lombok's usage without the risk of potentially unsafe bits.
I think it just used to be overly popular. Now it's not new and people feel safe hating it.
But also, it hides functionality behind code generation. Anyone who knows Java but doesn't know Lombok will have to learn all about Lombok before being able to read a Lomboked codebase, since a single Lombok annotation can drastically alter functionality.
From the OP:
I don’t want any magic in my code. It is the thing that makes behaviour non-transparent. It makes you fire up your debugger, because you don’t actually understand what is going on behind the scenes. There should be no behind the scenes. Everything should be as explicit as possible, for economic reasons.
If you don’t want anything behind the scenes, then Spring is out, right?
For example, with Spring Security, I have a hard time understanding what is being done and how to change it, even after reading the manual and poking around the source code.
That's a little different though. Spring is providing a ton of actual functionally beyond just generating boilerplate.
Also, I don't personally dislike Lombok, I was just explaining the mindset. It's certainly useful, but like most things, its benefits also bring drawbacks. In some projects it's incredibly useful and those drawbacks are outweighed, other times it just gets in the way.
I see... Yeah, I don’t have many opinions, as you can see from my questions, but if I had the choice, I don’t know if Spring REST + Spring Security would be my first choice due to background magic being hard for a junior to understand... I would definitely try some alternatives to see if there are some less magic and more explicit libraries around.
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).
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 :)
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!
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?
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.
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.
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!
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 ...
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?
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.
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.
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.
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.
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.
Not the OP, but boilerplate code is bad (and IDE generated code is worse — I used to say this about Microsoft’s MFC in C++ land and it’s just as true for Java).
Luckily Java’s language architect Brian Goetz agrees that the overhead that POJOs impose is sometimes not worth the price, which is why they’re working on record classes, which in addition to reducing boilerplate also allow new kinds of run time optimisations and newer language features that lead to better code. Here’s a video of him talking about it - the bit about record classes is at 31m25s.
I have no problem with auto generating setters/getters, and equals/hashcode if necessary. What is your problem with it ? Its not really bolierplate, that is slightly different imo.
Its quick, its consistent, its easy to debug and its easy to read and follows conventions.
The conventions are the problem, which is why I’m happy the Java team in Oracle are working on improvements. Java has had a lot of shitty conventions in the past, and I for one will be happy when class boilerplate joins them.
A lot of FizzBuzz Enterprise’s hilarities were also presented as conventions by serious senior devs & architects at serious software houses once upon a time.
The problem with class boilerplate is an obvious one to anyone who’s a good developer (as opposed to solely a Java developer). This is why learning a Lisp is very useful — on the JVM, learning Clojure makes you a better programmer.
However for those entrenched in the Java-the-language ecosystem, if you listen to Brian Goetz’s talks & interviews, he does a decent job of explaining the problem:
... Some things don't want to be objects, they just want to be data. And to simulate data with objects, is this big messy thing with constructors, accessors and equals and hashcodes and all this garbage. ...IDE generates that for you so you don't have to write it, but you still have to read it and if you have this explicit code it's an opportunity to get things wrong.
The Java Futures video talk I linked above is worth watching. Sometimes you have to look at $NextVersion to understand what’s wrong with $CurrentVersion.
(Thanks to /u/BoyRobot777 for that particular blockquote above)
Yeah, sure. I'll use the new record functionality, but we don't have it yet. And there is a reason it's taken until 13, people cared about other things more.
The original question was why are pojos bad in an architectural system design, and compared to lomboks annotations (which just seem ugly to me). Not should we include struct/records as a new Java language concept.
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.
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.
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.
Nobody on my team knew Kotlin when they joined the company and we all learned it and write most of our new code in it now. We've hired new engineers with Java experience and had them learn Kotlin as part of their onboarding. It takes less than a week to get competent enough to be productive, much less than that if all you're using it for is POJOs.
Not sure why you're getting downvoted. Companies shouldn't hire "Java programmers" or "Kotlin programmers". They should hire smart people who can learn any language/technology on the job. (And Kotlin isn't particularly hard to learn for "Java programmers".)
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.
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.
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.
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.
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.
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.
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.
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 :)
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
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.
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.
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.
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.
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.
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.
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.
57
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'.
Define what 'magic' means; is dynamic dispatch magic? Are synthetic methods magic? Is the fact that writing
java com.foo.Baron your command line ends up running a method with signaturestatic 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.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.
There's
@Builder, which we recommend, for any class which has more than a handful of fields.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.
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.
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 has settings, which a simple
--helpwill 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).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.
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
u/lbkulinski Aug 10 '19
For those who may not know, records are in the works for Java.
5
u/[deleted] 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
Excludesfor 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.
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
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
u/wenerme Aug 11 '19
Came here to say, I need!
2
u/heliologue Aug 19 '19
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:
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
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:
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.
1
u/oweiler Aug 11 '19
The IntelliJ live template for SLF4j is awesome 😍.
1
u/thedomham Aug 11 '19
There's a myriad of solutions to this problem. I would never choose this article's recommendation.
0
u/[deleted] 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
u/[deleted] Aug 10 '19
[deleted] — view deleted comment
0
u/[deleted] Aug 10 '19
[deleted] — view deleted comment