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!
82
Upvotes
View all comments
Show parent comment
15
u/rzwitserloot Dec 22 '25
They usually are. If they are not, then.. don't lombok them.
And, even better, if they are today but when updating your code you realize your constructor is no longer boilerplate, you simply remove the lombok annotation and write it out. Your API is 100% backwards compatible in all sense of the word. Callers can't even tell you changed it.
Note that records themselves 'treat it as boilerplate', in the sense that if you don't write a constructor for a record, that's no problem; you get the default one which sets all 'fields' and does no validation whatsoever.