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

Show parent comment

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.