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
3
u/rzwitserloot Dec 23 '25
Purely anecdotal of course, and I may well be biased, but I replace lombok code with OpenJDK stuff all the time (i.e. I don't think I am biased):
I rarely use records. By which I mean: I have about 1 record for every 5 record-esque classes, give or take. A 20% 'hit rate'. With 'record-esque' I mean: The class is primarily intended as a data vehicle.
4 out of 5 have something that would make using a record impossible or at least annoying:
public. If arecordispublic, then its constructor must also be (it's a compiler error even if you just redefine it solely for the purpose of whacking an access mod on it). I'm not going to ugly up the general API in order to 'save typing', and I don't think a thing being a record is inherently a bonus, i.e. there is no point trying to weigh the 'upside' of "well, its a record!" against the downside of "... it has a constructor that is exposed to code that has absolutely no business calling it".That third point is, in the end, a style consideration. But one I'm pretty set on, and I think most of the community agrees. Essentially ignoring the concept of access modifiers is bad / 'access' goes a little further than 'forbid' - access modifiers also constraint API: If it's hard to imagine how code outside of the package is meant to even use a certain method, then do not expose it. It makes reasoning about your code simpler, and more to the point perhaps, it makes reasoning about your type from the viewpoint of other packages much simpler: That method you don't expose is one you don't even have to know about, after all.
But about half of those '4 out of 5' need a bunch of boilerplate. Lombok therefore remains 100% required for sane java coding. And this will not change anytime soon (SOURCE: Recent OpenJDK panel session with all the movers and shakers (including Goetz and Reinhold), where the question was asked: "So, boilerplate removal" and the answer was a clear: "No!" - they spent more words, though).
[1] I fucking hate pithy programming 'principles' being applied to everything with no room for experience and artistry. YAGNI? If my experience and knowledge of the domain is telling me that I likely will, and soon, then I won't program ourselves into a corner whilst raising the banner of YAGNI, of course.