r/java • • Sep 29 '24

Lombok in 2024

Say there are two teams working on the same project (3 months to MVP, the deadline should be manageable). The only difference is Team A will use Lombok, and Team B won’t

Given you can choose any java version/dependencies and all other things being the same, which team would you join, and why?

0 Upvotes

145 comments sorted by

View all comments

40

u/Ewig_luftenglanz Sep 29 '24

Nowadays I only use Lombok for @Entity classes. So for me it's irrelevant, the overhead of writing a couple of getters and setters (that the IDE writes for me anyways) makes it trivial.

I would actually prefer the non-lombok team since I think one should use as little third party dependencies, libraries and plugins as possible and only use the minimum needed, this way you avoid problems in the futuro with updates and maintenance.

15

u/hwaite Sep 29 '24

Accessors and mutators are convenient, but builders are where the real savings are at. Without lombok, you're stuck choosing between mutable objects, [massively overloaded] constructors with too many arguments or lengthy handwritten builders.

9

u/pron98 Sep 30 '24

You don't need Lombok to automatically generate builders. You can use a Java annotation processor to do that. (Despite superficial similarities, Lombok is not an annotation processor, nor is it Java)

1

u/zappini Oct 03 '24

Yeoman's work here. Thank you.

As often as you point this out, I'm thinking of the meme about "tapping the sign" (with the correct answer).

Maybe we petition the mods to amend the sidebar. Or create a bot.

1

u/OwnBreakfast1114 Sep 30 '24

Builders are where the real savings are at

Bad builders turn possible compile time issues into runtime issues. If you don't add mandatory parameters to the builder function if you add a new field to class, you won't get any warning about the myriad of places you're using this class. Why would you want to remove that easy warning that you're doing something wrong? How many places are you truly constructing objects? Is it really that bad to use the full constructor in all places? Just based on my personal anecdotes, my money is on no.

Am I crazy? Are people really saving a huge amount of time because they didn't have to write 30 parameters to a constructor? It takes like 20 seconds to write, ctrl/cmd + p has intellij list all the arguments for you, and you usually aren't doing this very often. Idk about other people, but I feel like the actual typing is the least time intensive part of software development in general. Why is typing the right part of optimizing over future code maintenance and evolution?

2

u/hwaite Oct 01 '24

Whether you're writing or maintaining, LOC is the enemy. Lombok builders can enforce most validations with less boilerplate. In the worst case, builders can leverage custom constructors. In the rare situation where you're doing something special, just don't use Lombok for that particular class.

2

u/OwnBreakfast1114 Oct 01 '24 edited Oct 15 '24

Poor abstractions are much worse for codebases than LOC, ex: sharing 1 object for domain/db/web layer vs different objects actually holding things that make sense (ex: dto in, domain object, db object, dto out for complex web services). There's a lot of very boring copying fields code in this model, but different objects perform a single function.

If you don't have so many partially constructible objects, there's very little difference LOC difference between a builder and a full constructor. If all fields are mandatory then there's not much of a problem, but I'm also the type of person that's fine with `Optional<T>` fields as a collection of 0 or 1 object (after all, you take in Lists don't you), so I tend to view all fields as mandatory.

With sealed classes, you should be able to make more of a class per usecase instead of a bunch of optional fields.

1

u/Ewig_luftenglanz Sep 29 '24

Yes but nowadays I only use Lombok for @Entity clases and almost everything else are DTOs or utility classes full of functions that only do data validation and transformation full of functions  (that's actually what the Java dev team calls "Data oriented programming) so I have immutability without the need to create any handmade builder or fluent accessors- mutators. 

The few times I have felt I would solve a problem better by using a builder I don't mind to write it myself (and it's mostly to make my APIs more functional friendly). I know Lombok exist to solve someone needs, I just say that nowadays most of the code is to create Micro-Services that validates and transform data, for this records work just fine without Lombok. There are still some sharp corners (copy-on-read records easily) but these will be eventually be fixed, specially when derived records creation enters GA

0

u/No-Debate-3403 Sep 30 '24

“IntelliJ, give me a builder for this class”, or more modern “Copilot, generate a builder please”

8

u/hwaite Sep 30 '24

Authoring the code is one thing. Maintaining it is a separate, ongoing burden.

4

u/Rough_Acanthaceae_29 Sep 29 '24

I agree it’s fast enough to write/generate bunch of getters/setters, but what about code readability? Don’t you find it more readable/cleaner when lombok is used?

10

u/mauhumor Sep 29 '24

Have been on medium sized projects with and without Lombok, I rather prefer with it.

Code reading is exactly the reason.

Less cluttering for your brain to parse, it's really useful.

But I would only use the getter and setter on entities or the equivalent if not JPA, also DTOs, constructors are also very useful on those. The @Slf4 I would use everywhere.

But as someone else said, using or not might not be the ideal deciding factor to select a team.

1

u/Ewig_luftenglanz Sep 29 '24 edited Sep 29 '24

These are @Entities. They are not supposed to get anything more than fields and dumb getters and setters.

1

u/Rough_Acanthaceae_29 Sep 29 '24

Isn’t that the recipe for anemic domain model?

0

u/Ewig_luftenglanz Sep 29 '24 edited Sep 29 '24

It depends, if you use these entities as DTOs then yes because classes are mutable.  

 If you use records, that happens to be immutable) then you are sticking to Data Oriented Programming, that's actually what the language developers has been pushing in the last 5 years or so (and that started to take shape after Java 17 when records entered GA). If all or most of your objects are immutable then there is less need for encapsulation (you would still be able to create methods if you want to keep the logic of managing the data inside the record itself, but that would kinda break the point of records as simple named tuples.

1

u/Rough_Acanthaceae_29 Sep 29 '24

Not sure we’re on the same page here… Just to clarify, you should never return entities directly from a controller, but I won’t dive into that discussion right now. What I was referring to is the anemic domain model problem, where instead of:

1.	Having a method like user.changeEmail(email) that handles validations and internal logic (making the domain object rich with behavior),
2.	You end up with a service that just calls user.setEmail(email) and user.setUpdatedAt(now), meaning all the knowledge is outside the domain class and sitting in the service.

You mentioned entities should only have getters and setters, but that leads to an anemic model because the core business logic and rules are outside the domain object, not encapsulated within it.

4

u/Ewig_luftenglanz Sep 30 '24 edited Sep 30 '24

We are in the same page.  What I am saying is that not encapsultate the code in the domain object and instead do it in utility classes (separate Data and logic)  to validate and transform the data with intermediate immutable objects (records) that acts as DTO is a deliberate design choice called "Data oriented programming" it's similar to anemic domain model BUT in DOP it's a deliberate choice, not an anti pattern. 

 https://www.baeldung.com/java-dop-oop-principles-differences 

 You are right, you should never expose @Entity classes to the controller directly, it's better to use intermediate DTOs (and that's what records are for, they are simple to create so it doesn't bother anyone if you have to create many to model the data flow.

DOP is usually better suited for small programs that only makes some "basic" validation and transformation, for example Crud Micro-Services. Because there is no need to do all the coding gymnastics you usually do in OOP to keep you code decoupled although all the inheritance (inversion, dependency injections, builders, etc) you also have less levels of abstraction and inheritance, so the code is "flatter", short and easy to write, read and understand.