Skip to main content

Command Palette

Search for a command to run...

Code Smell 06 - Too Clever Programmer

Code is difficult to read, and tricky with names without semantics. Sometimes using language's accidental complexity.

Updated
β€’3 min read
Code Smell 06 - Too Clever Programmer

TL;DR: Don't try to look too smart. Clean code emphasizes readability and simplicity.

Problems πŸ˜”

  • Readability

  • Maintainability

  • Code Quality

  • Premature Optimization

Solutions πŸ˜ƒ

  • Refactor the code

  • Use good names

  • Refactor tricky code

  • Prefer clarity first

  • Avoid hidden tricks

  • Optimize only later with strong real evidence

Refactorings βš™οΈ

Examples

  • Optimized loops

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.

Tags 🏷️

  • Complexity -

Level πŸ”‹

[X] Intermediate

Why the Bijection Is Important πŸ—ΊοΈ

When you keep a clear bijection between your program and the MAPPER, other developers 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 πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨

More Information πŸ“•

Also Known as

  • Obfuscator

Credits πŸ™

Photo by NeONBRAND on Unsplash


Programming can be fun, so can cryptography; however they should not be combined.

Kreitzberg & Shneiderman


This article is part of the CodeSmell Series.

Code Smells

Part 1 of 50

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.