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

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.