Builder Pattern Instead of Error-Prone Constructors
When designing your classes, rather than following the path of the unreadable telescoping constructor, or leaving yourself open for bugs where the caller incorrectly passes a value into the wrong parameter because you have several of the same type, consider the following Builder pattern.
Having a private constructor will prevent anyone but the Person’s Builder from instantiating a Person. The setters in the Builder return the Builder, which allows method chaining, providing a DSL-like self-documenting interface. The Builder’s build() method is responsible for making sure that all properties are set before returning the Person.
Of course, this is a simple example with only two fields, so the alternative isn’t exactly error prone:
But, once you add a few more required fields, the following makes your code easier to follow, while still ensuring all required fields are set.