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
Enable strict mode
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
Code Smell 188 - Redundant Parameter Names
Maxi Contieri ・ Dec 24 '22
Code Smell 65 - Variables Named After Types
Maxi Contieri ・ Apr 2 '21
More Info
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
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.