DEV Community

Cover image for Code Smell 06 - Too Clever Programmer
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

5

Code Smell 06 - Too Clever Programmer

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

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Detection

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

Relations

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

Boolean Flags

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.

Last update: 2021/06/08

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (7)

Collapse
 
mt3o_23 profile image
Teodor Kulej

You are of course aware that math background promotes using single character names for variables? Also, there are valid (due to historical reasons) short names, like i for iterator in a for loop?

Collapse
 
dakujem profile image
Andrej Rypo

The code in the "wrong" example is not being clever at all. It's just obfuscated. That kind is typically written by juniors trying to look smart.
I'd swap the "A.K.A." section with the headline.

Collapse
 
tayyabtalha profile image
Tayyab Talha • Edited

There is no difference between wrong and right code.

Collapse
 
oloryn profile image
Ben Coleman

Refresh the page. This appears to be a bug in (at least) the Android DEV app.

Collapse
 
tayyabtalha profile image
Tayyab Talha

Yep you are right

Collapse
 
mcsee profile image
Maxi Contieri

From a machine point of view it isn't. The semantic parsing trees are equivalent

Collapse
 
bn_geek profile image
Mohcin Bounouara

In the point.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay