DEV Community

Cover image for Code Smell 151 - Commented Code
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

4 1

Code Smell 151 - Commented Code

Beginners are afraid to remove code. And many seniors too.

TL;DR: Don't leave commented code. Remove it.

Problems

  • Readability

  • Dead Code

  • Lack of Coverage

  • Lack of Source Version Control

Solutions

  1. Remove commented code

  2. Implement Source Version Control

Context

When debugging code we tend to comment on code to see what happens.

As a final step, after all our tests pass, we must remove them following clean code practices.

Sample Code

Wrong

function arabicToRoman(num) {
  var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  var roman = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
  var result = '';

  for(var i = 0; i < decimal.length; i++) {
    // print(i)
    while(num >= decimal[i]) {
      result += roman[i];
      num -= decimal[i];
    }    
  }
  // if (result > 0 return ' ' += result)

 return result;
}
Enter fullscreen mode Exit fullscreen mode

Right

function arabicToRoman(arabicNumber) {
  var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  var roman = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
  var romanString = '';

  for(var i = 0; i < decimal.length; i++) {
    while(arabicNumber >= decimal[i]) {
      romanString += roman[i];
      num -= decimal[i];
    }    
  }

 return romanString;
}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

Some machine learning analyzers can detect or parse comments and guide as to remove them.

Tags

  • Comments

Conclusion

We need to remove all commented-out code.

Relations

Refactorings

Credits

Photo by maxim bober on Unsplash


Don’t document the problem, fix it.

Atli Björgvin Oddsson


This article is part of the CodeSmell Series.

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

Top comments (1)

Collapse
 
jordanjlatimer profile image
jordanjlatimer

At my current company, which is using C#, they still have some files with commented out Visual Basic code. They changed languages years ago.

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay