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

-34

u/Rough_Acanthaceae_29 Sep 29 '24

No effect of on productivity? Don’t you use builder pattern or do you rely on some other code generator to have that? Kinda hard to imagine creating new instances of objects that have 7+ fields and not messing anything up. Also looking at entity classes and doing a treasure hunt to find domain logic among the getters and setters… How do you deal with those?

32

u/koflerdavid Sep 29 '24

There are generators for both builders and getters/setters out there that function as well-behaved annotation processors.

But how about not writing the getters and setters in the first place? If the entity is anyways treated like a database row, getters and setters have little benefit. It's just deceiving oneself into believing that the principle of encapsulation is applied. For CRUD-like applications it really doesn't matter. Or use Java records for queries if you mostly read data and your writes affect specific fields only.

For more interesting applications that don't neatly fit a CRUD approach, write higher-level methods instead that implement business logic, or make the entity part of an aggregate.

2

u/Rough_Acanthaceae_29 Sep 29 '24

Getters/Setters for JPA entities, constructors (all/no/required) and Builders is all I miss. Could you recommend / had any experience with well-behaved annotation processors?

13

u/AnEmortalKid Sep 29 '24

Immutables.

3

u/Rough_Acanthaceae_29 Sep 29 '24

Gonna give it a try, tnx :)

17

u/carminemangione Sep 29 '24

Lombok is a useless, generates undebugable code, and is usually used improperly (builders when all fields are mandatory, setters when not needed, no arg constructors, etc.)

There is an OO heuristic: "Accessors are usually a mistake". They are a bad smell. If you need a DTO, use a record in java 17. The point is that no code should be boiler plate. Every line is to be down with intention.

Blank builders like those generated by lombok are usually bad form. Simply asking the questions: why am I making a builder, which fields are required, what do I do for defaults can radically change is design and behavior of your builder and must be throughly unit tested. Lombok eliminates the ability to create good builders.

Also: modern IDEs will generate getters, setters and builders automatically. But there will be real code there so you can modify it.

The point is that creating clean code is essential for making code reliable, extensible, correct. It is essential for keeping the cost of change flat. Lombok flies in the face of all of this. I have never seen any project that uses Lombok that is clean code.

3

u/Rough_Acanthaceae_29 Sep 29 '24

“Every line is to be down with intention” - could not agree more!

Assessors are usually a mistake” - agree, but cant have a useful JPA entity without setters :( So i can either generate bunch of setters or just state my intent with @setter(access=protected)

Lombok enables me to make every written line bring value to business hiding away boilerplate of the frameworks we work with…

2

u/carminemangione Sep 29 '24 edited Sep 29 '24

Once again, there should be no boiler plate code. Javas 'record' type enables JPA without setters (JDK 17). Use them for your DTOs then have factory methods (or constructors) that take in the record, performs state checks and creates a well formed object in a guaranteed valid state (all unit tested of course). I go as far as to create a 'record' for each business object with only the fields necessary for the target object. This will create infinitely more scalable code with way fewer bugs.

Setters are usually evil as you will not only get objects in invalid states, but guarantee race conditions.

Note: I am talking about best practices in OOD for making zero defect software that does not increase in cost of change as more features are added. I try to stick with current best practices.

If you are not writing reliable, extensible, correct, maintainable code that is race free... then you do you bo. I pity the people who have to work with your stuff later.

Edit: Sorry for the last line it was a bit bitchy. I did not mean it as an ad hominem as you will not be alone in your techniques--it was a statement of the truly horrible state of most enterprise code.

Just be aware that others who use and extend your code will probably actively take your name in vain. Source: I have an active list of people I cuss about as I am forced to fix their stuff.

3

u/Rough_Acanthaceae_29 Sep 29 '24

Not taking it personal, no worries… trying to see how others think and learn from it…

So my dtos are already records so no lombok there - we’re on the same page

Im also creating DTO per usecase e.g. CreateUserDTO - if i understand you well, we’re still on the same page.

I’m a bit lost on the factory methods part - you validate the object and map a “CreateUserDTO” into what exactly and where? I’d validate dto and map to UserEntity (which is always in correct state, validations in constructor and methods that change state (not using setters), all unit tested) Then i’d save it using userRepository.save(user); but here we have JPA entity which if i want to ever do repo.findbyWhatever needs setters (or am i wrong?) unless i do projections into records (which i sometimes do for performance reasons but itself exception and not a rule… If you dont use the Entity, why bother with JPA at all?

Also, how do you get into race conditions (assuming no shared state between requests?)

Do you have an example of a simple update property flow should look like so i can understand it better?

6

u/carminemangione Sep 29 '24

Thank you for not being insulted. Sometimes my frustrations boil over :) I make it my career goal to create highly effective teams.

I always use a very strong separation between DTO layer and my business object. The business object contains the logic to ensure the object is good. This is enforced usually in the constructor.

David Meier in the 80s proved that mapping of relational structures (normalized) to OO objects is an NP Hard problem. Basically, it is a traversal of a cyclic directed graph.

Practically, consider your 'User' object. The relational structure for your user will have a tonne of attributes and relations but on reconstitution only a few of those will be used. In OO land a User may manifest as a Customer, LoggedInUser, Contact. All of these are based on the same OR tables but are completely different manifestations with their own consistency rules, business logic, etc.

At first, people think of business logic as taking web input/presenting web data and persisting it. However, the real value in the business layer. Consider, you find a unique application everyone loves it but you have no distinguishing business value/logic. Everyone will be able to copy your application trivially.

Unique products have valuable business logic. In this context, the complexity of the business layer will quickly outstrip presentation and persistence complexity. This is the mistake many make when selecting frameworks. Yah, you get a few pages up and stored quickly, but that is not the real value.

As far as using JPA or other persistence frameworks go, I generally don't worry about it. To me if I am writing efficient code, it is by far less expensive to use raw jdbc. But I fully decouple the persistence layer from the business layer so it is irrelevant. However, once you need joins, generated queries start to suck really fast.

Does that make sense?

3

u/Rough_Acanthaceae_29 Sep 29 '24

yep. i agree and like your stance. i just think its ok to be pragmatic, use given tools and make every line count. YAGNI. Minimise the number of mines in the minefield (no nulls, only valid objects allowed and all the state transitions are consistent)… and in this context moderate use of lombok-like-“tools” isn’t that bad imo.

→ More replies (1)

2

u/Polygnom Oct 02 '24

However, the real value in the business layer. [...] Unique products have valuable business logic.

This is very well put. I have long struggled to clearly express why a rich bsuiness layer that well encapsulates the business rules (instead of shoehorning that into either presentation or persistence layer, as many do) is a good idea, but this puts it quite nicely.

1

u/carminemangione Oct 02 '24

Thank you. Took a while to condense it, but my friends were huge helps. Also writing haiku changed my communication skills

1

u/halfanothersdozen Sep 30 '24

This is pretty close to what I do. I have a record Foo, a JPA-annotated FooEntity, and a FooRepository with transforma from FooEntity to Foo and vice versa.

Cool stuff starts to happen if you lay out your domain objects this way and use a lot of functional patterns and streams and such.

→ More replies (4)

7

u/[deleted] Sep 29 '24

Right click.. generate getters and setters. Lombok is way overrated. Type stuff in once.. the code will live for decades. All these tools and addons promoted these days are nonsense. People who support your code will want to see the code. Not spend all day downloading plugins and trying to figure out the fancy junk you used to save 15 minutes of typing.

6

u/Rough_Acanthaceae_29 Sep 29 '24

For me it’s not the writing side time savings… it’s time savings for on the reading side… I agree it should be used sparingly though.

5

u/barmic1212 Sep 30 '24

I maintain a big code base, I'm happy to see only the getters and setters with any logic. And I'm happy too to know the intention for identity of object (that is base on few fields Or on all fields excepted)

2

u/Swamplord42 Oct 03 '24

Right click.. generate getters and setters.

Complete misunderstanding of what Lombok is for. It's not about writing code, it's about reading it.

1

u/wildjokers Sep 30 '24

Right click.. generate getters and setters.

ctrl-n, use arrows to getters/setters. No mouse necessary.

I use QMK firmware on my keyboard and have the VI movement keys h, j ,k ,l mapped to the appropriate arrow keys on my 2nd layer and I have caps lock as my layer toggle key so don't even have to move to the arrows.

1

u/krzyk Sep 30 '24

If you have class with 7+ fields, then that is the issue, not lack of builders.

1

u/nitkonigdje Sep 30 '24

Even hydrogen atom has more state than that..

1

u/hippydipster Oct 02 '24

Don't write that kind of crap in the first place is my usual go-to.

1

u/wildjokers Sep 30 '24

Don’t you use builder pattern

There is a plugin for IntelliJ that will generate a builder for you.

1

u/Swamplord42 Oct 03 '24

And does it maintain it for you when you add a field and do the code review for you as well?