# 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](https://maximilianocontieri.com/what-exactly-is-a-name-part-i-the-quest)

- Refactor tricky code

- Prefer clarity first

- Avoid hidden tricks

- Optimize only later with strong real evidence

# Refactorings ⚙️

%[https://maximilianocontieri.com/refactoring-005-replace-comment-with-function-name]

# 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 🚫

<!-- [Gist Url](https://gist.github.com/mcsee/df27505a28b5f65faaa273b0bfe1f322) -->

```javascript
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 👉

<!-- [Gist Url](https://gist.github.com/mcsee/4749cfe51de1c02848df1aa802fa5705) -->

```javascript
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](https://maximilianocontieri.com/the-one-and-only-software-design-principle) between
your program and the [MAPPER](https://maximilianocontieri.com/what-is-wrong-with-software), 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

| Without Proper Instructions    | With Specific Instructions |
| -------- | ------- |
| [ChatGPT](https://chat.openai.com/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) | [ChatGPT](https://chat.openai.com/?q=correct%3DRemove+cleverness+from+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Claude](https://claude.ai/new?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) | [Claude](https://claude.ai/new?q=correct%3DRemove+cleverness+from+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Perplexity](https://www.perplexity.ai/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) | [Perplexity](https://www.perplexity.ai/?q=correct%3DRemove+cleverness+from+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Copilot](https://www.bing.com/chat?showconv=1&sendquery=1&q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) | [Copilot](https://www.bing.com/chat?showconv=1&sendquery=1&q=correct%3DRemove+cleverness+from+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) |
| [You](https://you.com/search?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) | [You](https://you.com/search?q=correct%3DRemove+cleverness+from+code%3A+%60%60%60javascript%0D%0Afunction+primeFactors%28n%29+%7B%0D%0A++var+f+%3D+%5B%5D%2C++i+%3D+0%2C+d+%3D+2%3B++%0D%0A++%0D%0A++for+%28i+%3D+0%3B+n+%3E%3D+2%3B+%29+%7B%0D%0A+++++if%28n+%25+d+%3D%3D+0%29+%7B%0D%0A+++++++f%5Bi%2B%2B%5D%3D%28d%29%3B+%0D%0A+++++++n+%2F%3D+d%3B%0D%0A++++%7D%0D%0A++++else+%7B%0D%0A++++++d%2B%2B%3B%0D%0A++++%7D+++++%0D%0A++%7D%0D%0A++return+f%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Gemini](https://gemini.google.com/) | [Gemini](https://gemini.google.com/) | 
| [DeepSeek](https://chat.deepseek.com/) | [DeepSeek](https://chat.deepseek.com/) | 
| [Meta AI](https://www.meta.ai/chat) | [Meta AI](https://www.meta.ai/) | 
| [Grok](https://grok.com/) | [Grok](https://grok.com/) | 
| [Qwen](https://chat.qwen.ai/) | [Qwen](https://chat.qwen.ai/) | 

# Conclusion 🏁

Clever developers write cryptic code to brag.

Smart developers write clean code.

Clear beats clever.

# Relations 👩‍❤️‍💋‍👨

%[https://maximilianocontieri.com/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]

# More Information 📕

%[https://ardalis.com/are-boolean-flags-on-methods-a-code-smell/]

# Also Known as

- Obfuscator

# Credits 🙏

Photo by [NeONBRAND](https://unsplash.com/@neonbrand) on [Unsplash](https://unsplash.com/s/photos/smart-brain)

* * *

> 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]
