Clean Code : Keep code clean :: Method – Argument
Developers are authors. And one thing about authors is that they have readers.
Method – Argument |
- The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic).
- Three arguments (triadic) should be avoided where possible.
- More than three (polyadic) requires very special justification-and then shouldn’t be used anyway.
- Arguments are hard. They take a lot of conceptual power.
Flag Arguments
- Flag arguments are ugly. Passing a boolean into a function is a truly terrible practice.
- It immediately complicates the signature of the method, loudly proclaiming that this function does more than one thing.
- It does one thing if the flag is true and another if the flag is false!
Argument Objects
When a function seems to need more than two or three arguments, it is likely that some of those arguments ought to be wrapped into a class of their own.
For Example,
Circle makeCircle(double x, double y, double radius);
Circle makeCircle(Point center, double radius);
Suggestions
- Functions that take three arguments are significantly harder to understand than dyads. The issues of ordering, pausing, and ignoring are more than doubled.
- I suggest you think very carefully before creating a triad.
Recent Comments