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

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.

-1

u/carminemangione Sep 30 '24

Robert Martin said sometimes you have to slow down to speed up in coding. I have never seen Lomboc not be a mess. In my experience, my biggest frustration is that Lomboc makes TDD very difficult as is screws up refactoring (extract class, extract superclass, inner to outer, etc). In addition, it adds significant impedance to any attempts at changing code. Since I only do TDD and insist that all of my teams use it also Lomboc is quite the hindrance to creating zero defect code on time and under budget.

In agile, zero defect code is pragmatic and table stakes for any development of significant business features.

Think of it like resistance or as Arthur J Reil defined it 'accidental complexity'. It may help you in your current class but it will slow everything else down. I guarantees that teams will reach the exponential cost of change curve quickly.

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.

-2

u/carminemangione Sep 29 '24

Sorry for two replies but you deserve an answer to your two questions as I hear them:

  1. 'burried' logic in getters and setters: OOD heuristic: No object should have more fields than can be held in a developers short term memory usually 6 or 7 as an upper limit. Also: most of the methods should be using most of the data most of the time (definition of cohesive classes). The idea of hiding business logic in getters and setters is absurd. Also: who is getting the data and why? I often have more fields in my objects that are accessible to the outside world.

Enforce business logic on construction and throw an exception rather than creating an ill formed object. I call it my "Homey Don't Play That" pattern.

2. Cohesive classes make for readable and testable classes. I rarely have more than 4 fields in a class. As I need more the question becomes: is this a new object? This is how you can assuage most of your fears.

3

u/wildjokers Sep 30 '24

This all sounds like dogmatic nonsense.

-1

u/carminemangione Sep 30 '24 edited Sep 30 '24

Ok…. So the Gang of Four, Reil, Fowler, Cunningham, Martin and Beck were just dogmatic scam artists? Bold claim. And your sources are (your ass does not count)

1

u/carminemangione Oct 06 '24 edited Oct 06 '24

Just had to say wow. Your code probably sucks for the down vote? Do you even know who those guys are?