Code Smell 19 - Optional Arguments
Disguised as a friendly shortcut is another coupling smell.
In languages with optional arguments but limited to basic types, we need to set a flag and add an accidental IF (another smell).
- Make arguments explicit.
- All function calls must have same arity.
- Use Named Parameters if your language supports them.
Detection is easy if language supports optional arguments.
Be explicit. Favor readability over shorter (and more coupled) function call.
The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.
This article is part of the CodeSmell Series.
I fail to see a problem. All it couples to are sensible defaults, which actually tend to solve such side effect as tons of unnecessary boilerplate code.
Yes, methods are used not with full list of arguments, but if their types don't overlap, then it's got to be right. (Another good example would be named parameters in Python - you avoid binding to the order of arguments and call out parameters' names and values instead, which is way more specific.) Again, optionals save the hassle of calls being too over the defaults that might change with the environment.