Code Smell 214 - Duplicate Parameter Names

Two arguments of the same type. Two equal names

TL;DR: Turn on Strict Checks

Problems

  • Unexpected errors

  • Ambiguity

  • The Least Surprise Principle violation

  • Portability

Solutions

  1. Enable strict mode

  2. Use role-naming arguments

Context

Most compilers forbid duplicate parameters since they are a common mistake in a large parameters list

Sample Code

Wrong

function addNumbers(a, b, a) {
  console.log(a + b);
}

addNumbers(2, 3, 4);

// Outputs 7 (2 + 3 + 2)

Right

"use strict";

function addNumbers(a, b, a) { }
//                          ^
// SyntaxError: Duplicate parameter name not allowed in this context

Detection

[X] Automatic

By enabling strict mode, the compiler will warn us

Tags

  • Naming

Conclusion

Enable the stricter modes you can find on your compilers.

Try to fail fast and catch errors as early as possible and leave the hard and dumb work to the tools.

Relations

More Info

Sonar Source

Mozilla Org

Disclaimer

Code Smells are my opinion.

Credits

Photo by Caroline Veronez on Unsplash


One of the things I've been trying to do is look for simpler or rules underpinning good or bad design. I think one of the most valuable rules is avoiding duplication. "Once and only once" is the Extreme Programming phrase.

Martin Fowler


This article is part of the CodeSmell Series.