Number of arguments according to Uncle Bob

195 views
Skip to first unread message

Rudi Hammad

unread,
Jun 4, 2020, 4:28:26 PM6/4/20
to Clean Code Discussion
Hi everyone.
I finished ready uncle bob's clean code book. I wanted to know your opinion about the section where he talks about the number of arguments.
Basically he says that monadic are better that diadic functions and diadic better than triadic. And that 4 arguments need special justification.
I wanted to share my opinion on this and see what other developers think:
I do agree that methods need to have few arguments. I would not say that 1 is "better" than 2. I think from 1 to 3 they are all equal as longo as the SRP is respected.
Also you can kind of cheat saying that if two arguments are cohesive they can be put together inside a tuple, and that count as a single argument. For instance instead of having
someMethod(source, target) you do someMethod(inputs=(None, None)) . I personally rather the first one, so does that make my method worst? I don't think so. So that's why I
believe that we need to be carefull talking too literally that 1 is better than 2.

Anyway, my real question is the following. I come from the CGI industry, and I'd say that 90% of my methods have between none or 2 arguments, and 3 sometimes. But there
are situations where you need 5 or even six. I'll give you an example. I have a method that makes an object look at another. This is how the method looks:

def aimAt(source, target, aimAxis, upAxis, Upvector, aimDirection)

So I think that it is impossible to know if this is correct unless you see the code inside the method. This is my take one it:
1-it is wrong if you open the method and see that has a huge body. For instance 100 lines of code, since that is a fairly complex method. It still does only one thing, but all the source code is in that huge method.
2-it is perfectly fine if that method is acting as a wrapper. So what I did here is create a class called Aim that have very simple method with one argument each. So if you look inside the aimAt method you will see this:

aim = Aim()
aim
.inputs(source, target)
aim
.setAimAxis(aimAxis)
aim
.setDirection("positive")
etc
...
aim
.doIt()

So as you can see, even if the method has 6 arguments, the code inside is actually wrapping an object were most methods are monadic.

To sum up, my take on that topic is that to judge if a code is clean we need to see what is inside of the method, and if it is a wrapper as shown above it is totally fine.
You are providing the user 6 arguments at a high level, but that is fine because your low level code is respecting the clean code rules.

what do you think?

Cheers

Rudi Hammad

unread,
Jun 4, 2020, 5:17:41 PM6/4/20
to Clean Code Discussion
Hi
this is a duplicate, please feel free to delete my threads in the forum in you want. All this was answered in the clack chat
Reply all
Reply to author
Forward
0 new messages