Is it common to have a constructor with 7 elements for example? or is it better to use many setters instead?
1 Answers
First, I want to point out the excellent back and forth in the comments that shows the issue is nuanced - and often a religious debate.
There are many Java-specific comments about different frameworks, etc. - but I think the real meat of a question like this should stand alone from a specific framework choice and possibly even language.
I think the question you didn't ask was: "Does my object need seven others to work properly?" This may be a design smell - or it might be completely normal at a high-level tier of your complex program. This is the question I would ask first - any weirdness about the number of inputs doesn't change depending on the method of object creation.
Once it's known that all these objects as inputs do make sense, then it comes down to how the object is created. To @Robert Harvey's point, it's important to have a fully-baked object, or else you have to check if the object is good before you do any work anywhere in the object. I have seen that type of code and it tends to be less maintainable - I feel I can say that objectively. All arguments coming in on constructors is a nice clean way to do this, but if your inversion of control framework does it via private properties - does it matter? Protection exists in both cases.

- 1,369
-
I can design it anyway I want, the class simply is the git Commit which will have elements: author, message, sha, date plus 3 extra fields that we add for the commit – nemo Sep 24 '14 at 02:11
final
fields, etc. I recommend the builder pattern. Hide the complexity, and throw an exception if the object cannot be properly constructed. – Sep 23 '14 at 23:19build()
will throw an exception, but once you get a successful object back from the builder, you know it's completely valid because it used the constructor. I don't see any advantage to using it just because you have "too many parameters", but if you have too many possible combinations of included/excluded parameters, then I could maybe see it being handy. – Ben Aaronson Sep 24 '14 at 01:30