Code Smell 06 - Too Clever Programmer

Code Smell 06 - Too Clever Programmer

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

Maxi Contieri
·Oct 25, 2020·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Problems
  • Solutions
  • Examples
  • Exceptions
  • Sample Code
  • Detection
  • Relations
  • Refactorings
  • Also Known as
  • Conclusion
  • Tags
  • More Info
  • Credits

TL;DR: Don't pretend you are too smart. Clean code asks for readability and simplicity.

Problems

  • Readability

  • Maintainability

  • Code Quality

  • Premature Optimization

Solutions

Examples

  • Optimized loops

Exceptions

  • Optimized code for low-level operations.

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

Automatic detection is possible in some languages. Watch some warnings related to complexity, bad names, post-increment variables, etc.

Relations

Refactorings

Also Known as

  • Obfuscator

Conclusion

Too clever developers write cryptic code to brag. Smart developers write clean code. Clear beats clever.

Tags

  • Declarative

More Info

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.

 
Share this

Impressum

Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)maximilianocontieri.com