Skip to main content

Command Palette

Search for a command to run...

Code Smell 19 - Optional Arguments

Updated
2 min read
Code Smell 19 - Optional Arguments
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

Disguised as a friendly shortcut is another coupling smell.

TL;DR: Optional Arguments generate a hidden coupling in the name of smaller code.

Problems

  • Coupling

  • Unexpected results

  • Side effects

  • Ripple Effect

  • In languages with optional arguments but limited to basic types, we need to set a flag and add an accidental IF (another smell).

Solutions

  1. Make arguments explicit.
  2. All function calls must have the same arity.
  3. Use Named Parameters if your language supports them.

Sample Code

Wrong

<?

final class Poll {

    function _construct(array $questions, bool $annonymousAllowed = false, $validationPolicy = 'Normal') {

        if ($validationPolicy == 'Normal') {
          $validationPolicy = new NormalValidationPolicy();
        }
        //...
    }
}

//Valid
new Poll([]);
new Poll([], true);
new Poll([], true , new NormalValidationPolicy());
new Poll([], , new StrictValidationPolicy());

Right

<? 

final class Poll {

    function _construct(array $questions, AnonnyomousStrategy $annonymousStrategy, ValidationPolicy $validationPolicy) {            
        //...
    }
}


//invalid
new Poll([]);
new Poll([], new NoAnonnyomousValidStrategy());
new Poll([], , new StrictValidationPolicy());

//Valid
new Poll([], new NoAnonnyomousValidStrategy(), new StrictValidationPolicy());

Detection

Detection is easy if the language supports optional arguments.

Tags

  • Optional

  • Laziness

Conclusion

Be explicit. Favor readability over shorter (and more coupled) function calls.

More info


The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.

Seymour Cray


This article is part of the CodeSmell Series.

Last update: 2021/06/30

D

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.

1
M

Named parameters and optionals are great.

But you are still coupled to defaults (implementative), when you should just couple to declarative

I’ll add this to the article. Thank you so much

3

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.