DEV Community

Cover image for Code Smell 19 — Optional Arguments
Maxi Contieri
Maxi Contieri

Posted on • Updated on • Originally published at maximilianocontieri.com

Code Smell 19 — Optional Arguments

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 same arity.
  3. Use Named Parameters if your language supports them.