r/java • u/danielliuuu • 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:
- 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).
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
View all comments
Show parent comment
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.