r/java • • Dec 21 '25

After writing millions of lines of code, I created another record builder.

Background

After writing millions of lines of Java code, here are my findings:

  1. Record can replace part of Lombok's capabilities, but before Java has named parameter constructors with default values, the Builder pattern remains the best solution for object construction (although it still has boilerplate code).
  2. Protobuf made many correct API design decisions:

    • One single way to build objects (builder)
    • Not null by default (does not accept or return null)
    • Builder class has getter/has/clear methods

Based on this, I created another record builder inspired by Protobuf, which provides no custom capabilities, does not accept null (unless explicitly declared as Nullable), and simply offers one way to do one thing well.

// Source code

import recordbuilder.RecordBuilder;
import org.jspecify.annotations.Nullable;


public record User(
    String name,
    Integer age,
    @Nullable String email
) {}

// Generated code

public final class UserBuilder {
    private String _name;
    private Integer _age;
    private @Nullable String _email;

    private UserBuilder() {}

    // Factory methods
    public static UserBuilder builder() { ... }
    public static UserBuilder builder(User prototype) { ... }

    // Merge method
    public UserBuilder merge(User other) { ... }

    // Setter methods (fluent API)
    public UserBuilder setName(String name) { ... }
    public UserBuilder setAge(Integer age) { ... }
    public UserBuilder setEmail(@Nullable String email) { ... }

    // Has methods (check if field was set)
    public boolean hasName() { ... }
    public boolean hasAge() { ... }
    public boolean hasEmail() { ... }

    // Getter methods
    public String getName() { ... }
    public Integer getAge() { ... }
    public @Nullable String getEmail() { ... }

    // Clear methods
    public UserBuilder clearName() { ... }
    public UserBuilder clearAge() { ... }
    public UserBuilder clearEmail() { ... }

    // Build method
    public User build() { ... }

    // toString
    u/Override
    public String toString() { ... }
}

GitHub: https://github.com/DanielLiu1123/recordbuilder

Feedback welcome!

83 Upvotes

53 comments sorted by

View all comments

Show parent comment

-2

u/foreveratom Dec 22 '25

How often do you really need to implement equals and hashcode? And what IDE are you using that doesn't generate getters and setters with a shortcut?

14

u/rzwitserloot Dec 22 '25

The problem with letting your IDE generate them, is that you have to maintain that and look at it. Code isn't carved out of marble. It tends to be modified.

If you have a boatload of IDE-generated boilerplate, and there's one getter in there that does something a little wonky, that's hard to spot amongst all the noise. Not so with lombok. Or with most other generators.

If you have a boatload of IDE generated boilerplate and you add a field, will you remember to wipe out the equals and hashCode and ask your IDE to generate them anew? You sure those methods didn't contain modifications you just accidentally deleted? If you forget, it won't really be obvious. (no compiler errors, for example). Likely that will be a bug that goes undiscovered for quite a while and can cause hours of damage, if not more.

Excuse the directness, but that sentiment strikes me as someone who doesn't maintain any projects larger than ~1 personyear of effort. Perhaps you just code differently, but I find it hard to imagine how you end up with heaps of code that you pretty much never touch again.

7

u/j4ckbauer Dec 22 '25

The problem with letting your IDE generate them, is that you have to maintain that and look at it. Code isn't carved out of marble. It tends to be modified.

If you have a boatload of IDE-generated boilerplate, and there's one getter in there that does something a little wonky, that's hard to spot amongst all the noise. Not so with lombok. Or with most other generators.

Thank you for pointing this out. When pages-and-pages of boilerplate are generated, code maintainers have to read those pages in order to confirm it is just boilerplate that does what you'd expect boilerplate to do.

The better answer is for the 'code' to not exist as something the developer has to read and modify. Lombok is one way to do that, but I'm not saying it has to be Lombok.

People who argue against this, unfortunately, tend to reach for the most low effort, least-effective, and most-insulting reply "Oh, you are just too lazy to type out all that code, eh?".

I find it hard to imagine how you end up with heaps of code that you pretty much never touch again.

Not only 'never touch again', but 'never even look at again'. The fantasy of every manager who was a terrible coder. "Well, if you just did it perfectly the first time, you wouldn't have to re-read, much less rewrite your code. Next time use at least 10 classes instead of 3 and sprinkle this code over as many files as possible... <speculative generality>"

1

u/foreveratom Dec 24 '25

I didn't mean to let an IDE automatically generate everything but to take the conscious decision to generate code using it. In all cases changing something also means taking care of the side effects of those changes, not assuming some magic wand will do it for you.

And not to boast, but your sentiment is definitely wrong. Having 30 years in this field, chances are you are using some of the code I wrote in your daily internet life...

1

u/rzwitserloot Dec 25 '25

didn't mean to let an IDE automatically generate everything but to take the conscious decision to generate code using it.

Right, so, take responsibility and maintain the code.

Which is probably the right move, but mentioning 'just let your IDE generate this code, there is no effort involved' is misleading at that point. There is a rather obvious difference in effort between 'maintain these 2 pagefuls of code' and 'maintain this one line of code', even if the actual writing of those 2 pages were free.