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
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
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
This article is part of the CodeSmell Series.
About the author
I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Comments
Join the discussionNo comments yet. Be the first to comment.
Code Smells
In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.





