On 17.06.2017 22:07, Radoslaw Grymin wrote:
> Hi Jacek,
>
> in the Episode No. E04 (Function Structure) Uncle Bob told a generic
> rule - he limits number of function arguments in his programs to 3.
> And It is a little bit confusing. It is really difficult to achieve. I
> dont consider particular program, I think about programs overall, about
> this rule of thumb.
>
> Yes, I can achieve it by removing parameters from constructor and create
> setters. But if I set attributes separately it may lead to logical
> inconsistency.
> And if I want to check if the state is proper, I need to know about
> relations between class atributes - as a class user I should not think
> about it.
> Ok, I can make checks in setters and throw exceptions.
> But still, users of this class will have to remember that before
> invoking method they need to call setter.
> I think that TDD is the great invent and I use it when it is possible
> (if test compilation does not last many minutes, in C++ it is a big
> problem, preprocessor has lot to do...),
>
> but still, as a class user I don't want to remember about setting class
> state after construction.
>
> So, for me this rule is not possible to fulfiled everytime.
Exactly, what I've learned so far about software development is, that
it's an art of trade-offs. If having no more than 3 parameters is the
most important rule for you, you might need to sacrifice something else,
like ensuring consistent state of objects. And it's your, usually
difficult, choice to make: which of all the rules you know is more
important. Tricky part is, everyone would probably be doing different
trade-offs. In this case I personally would easily sacrifice "max 3
parameters" rule to get consistent objects.
> I wanted to ask you if you follow this rule and if you have some
> techniques how to achieve it. And what do you think about setters
> (instead of many parameters of constructor),
> proposed by Uncle Bob.
Yes, I'm trying to follow that rule. How do I achieve it? Sometimes I
don't. Sometimes I'm using builders for more complicated objects.
Sometimes I split objects, because I realize they're too big (if I may
notice, I have a feeling you're carefully omitting this advice from Caio
and me, maybe try to see if your object can be broken). As usual: it
depends.