DEV Community

loading...
Cover image for Code Smell 19 — Optional Arguments

Code Smell 19 — Optional Arguments

mcsee profile image Maxi Contieri Originally published at mcsee.Medium ・1 min read

Disguised as a friendly shortcut is another coupling smell.

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.

Sample Code

Wrong

Right

Detection

Detection is easy if language supports optional arguments.

Tags

  • Optional

  • Laziness

Conclusion

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

More info


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

Seymour Cray

Discussion (0)

pic
Editor guide