Andrea Koutifaris
1 min readJun 5, 2018

--

Job.builder()
.account('My Account')
.address('My address')
.product('My product')
.build().save();

Could be considered many lines, or 1 line, depending o the definition of “line”. If you put everything on the constructor, is it a line?

Anyway 4 lines is a bad rule. Single responsibility is a the right rule. Single responsibility often implies fewer lines.

But I replied just to say: do not use setters! Setters are used when a class is mutable by design. It means that you need the class to mutate later in the code or in the time. Do not create setters just to put initial state inside a class!

Use a static factory method, a Builder, a factory, or, if you can pass parameters by name, use a public constructor.

If there are 2 or less parameters of different type, just use a public constructor. Otherwise make the constructor non public and, in this case, it can take as may parameters as needed.

The idea is that after the constructor is executed, the class is consistent.

--

--

Andrea Koutifaris
Andrea Koutifaris

Responses (1)