Code is hard to read when you use tricky names with no semantics or rely on accidental language complexity.
TL;DR: Don't try to look too smart. Clean code emphasizes readability and simplicity.
Problems
Solutions
Refactorings ⚙️
https://maximilianocontieri.com/refactoring-005-replace-comment-with-function-name
Examples
Context
You might feel the urge to show off your skills with complex tricks or cryptic names.
This makes your code harder to read, debug, and extend.
You must remember that you write code for humans, not machines.
Sample Code
Wrong
function primeFactors(n) {
var f = [], i = 0, d = 2;
for (i = 0; n >= 2; ) {
if(n % d == 0) {
f[i++]=(d);
n /= d;
}
else {
d++;
}
}
return f;
}
Right
function primeFactors(numberToFactor) {
var factors = [],
divisor = 2,
remainder = numberToFactor;
while(remainder>=2) {
if(remainder % divisor === 0) {
factors.push(divisor);
remainder = remainder / divisor;
}
else {
divisor++;
}
}
return factors;
}
Detection
[X] Semi-Automatic
Automatic detection is possible in some languages.
Look for warnings about complexity, bad names, post-increment variables, and similar patterns.
Exceptions
- Optimized code for low-level operations.
Level
[X] Intermediate
Why the Bijection Is Important ️
When you keep a clear bijection between your program and the MAPPER.
Other developers and your future self can understand it quickly.
Clever tricks break this mapping and force future readers to guess instead of reading.
AI Generation
AI models sometimes generate clever one-liners or compressed solutions.
They might look smart but lack readability and semantics.
AI Detection
AI assistants can rewrite clever code into readable code if you instruct them to prefer clarity to optimization.
Try Them!
Remember: AI Assistants make lots of mistakes
Suggested Prompt: correct=Remove cleverness from code
Conclusion
Clever developers write cryptic code to brag.
Smart developers write clean code.
Clear beats clever.
Relations ❤️
https://coderlegion.com/7475/code-smell-02-constants-and-magic-numbers
https://maximilianocontieri.com/code-smell-20-premature-optimization
https://maximilianocontieri.com/code-smell-44-magic-corrections
https://maximilianocontieri.com/code-smell-41-regular-expression-abusers
https://maximilianocontieri.com/code-smell-78-callback-hell
https://maximilianocontieri.com/code-smell-51-double-negatives
https://maximilianocontieri.com/code-smell-33-abbreviations
https://maximilianocontieri.com/code-smell-48-code-without-standards
https://maximilianocontieri.com/code-smell-196-javascript-array-constructors
https://maximilianocontieri.com/code-smell-25-pattern-abusers
https://maximilianocontieri.com/code-smell-93-send-me-anything
https://maximilianocontieri.com/code-smell-145-short-circuit-hack
https://maximilianocontieri.com/code-smell-212-elvis-operator
https://maximilianocontieri.com/code-smell-180-bitwise-optimizations
https://maximilianocontieri.com/code-smell-129-structural-optimizations
https://maximilianocontieri.com/code-smell-32-singletons
https://maximilianocontieri.com/code-smell-21-anonymous-functions-abusers
https://maximilianocontieri.com/code-smell-24-boolean-coercions
https://maximilianocontieri.com/code-smell-69-big-bang-javascript-ridiculous-castings
https://ardalis.com/are-boolean-flags-on-methods-a-code-smell/
Also Known as
Credits
Photo by NeONBRAND on Unsplash
Programming can be fun, so can cryptography; however they should not be combined.
Kreitzberg & Shneiderman
https://maximilianocontieri.com/software-engineering-great-quotes
This article is part of the CodeSmell Series.
https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code