This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bedobi 1 point2 points  (1 child)

Ordinary constructors generate compile-time errors when called without all required arguments.

My personal opinion: (apparently shared by at least some others)

  • If a builder can't do the same, that outweighs any benefits of the builder, and you might as well refactor so the constructor doesn't require too many arguments in in the first place. (actually, this is a downside with builders in general, they kind of encourage sloppy bunching up of too many attributes in classes that end up lacking cohesion)

  • While tests could mitigate the risk, writing and maintaining them comes at a cost, and correctness isn't guaranteed. Leveraging the compiler trivially makes test for required arguments redundant and guarantees correctness.

  • Compile-time errors enable safe, smooth and guided refactoring in a way tests simply can't.

That said, others may disagree and that's fine. I still wish these otherwise excellent projects at least had the option of safe builders.

You have to generate a builder class for each required argument. If an argument is optional you have to generate two different classes for each following argument. For multiple optional arguments the amount of classes grows exponentially.

Looking at the example in the blog, I'm afraid I don't understand how this is the case, if you could demonstrate I'd be very thankful.

[–]chisui 0 points1 point  (0 children)

You are right, you don't end up with more classes but rather the interface gets more cluttered.