Code Smell 19 - Optional Arguments

Code Smell 19 - Optional Arguments

Maxi Contieri⭐⭐⭐'s photo
Maxi Contieri⭐⭐⭐
·Nov 7, 2020·

2 min read

Play this article

Table of contents

  • Problems
  • Solutions
  • Sample Code
  • Detection
  • Tags
  • Conclusion
  • More info

Disguised as a friendly shortcut is another coupling smell.

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


  • 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).


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

Sample Code



final class Poll {

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

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

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



final class Poll {

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

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

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


Detection is easy if the language supports optional arguments.


  • Optional

  • Laziness


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

Share this