Code Smell 151 — Commented Code

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;
}

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;
}

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

Refactoring 005 — Replace Comment with Function Name

Credits

Photo by maxim bober on Unsplash

Don’t document the problem, fix it.

Atli Björgvin Oddsson

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Top jobs for software engineers


Code Smell 151 — Commented Code was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri

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;
}

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;
}

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

Refactoring 005 — Replace Comment with Function Name

Credits

Photo by maxim bober on Unsplash

Don’t document the problem, fix it.

Atli Björgvin Oddsson

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Top jobs for software engineers


Code Smell 151 — Commented Code was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2022-07-24T23:49:20+00:00) Code Smell 151 — Commented Code. Retrieved from https://www.scien.cx/2022/07/24/code-smell-151-commented-code/

MLA
" » Code Smell 151 — Commented Code." Maximiliano Contieri | Sciencx - Sunday July 24, 2022, https://www.scien.cx/2022/07/24/code-smell-151-commented-code/
HARVARD
Maximiliano Contieri | Sciencx Sunday July 24, 2022 » Code Smell 151 — Commented Code., viewed ,<https://www.scien.cx/2022/07/24/code-smell-151-commented-code/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 151 — Commented Code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/24/code-smell-151-commented-code/
CHICAGO
" » Code Smell 151 — Commented Code." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2022/07/24/code-smell-151-commented-code/
IEEE
" » Code Smell 151 — Commented Code." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/07/24/code-smell-151-commented-code/. [Accessed: ]
rf:citation
» Code Smell 151 — Commented Code | Maximiliano Contieri | Sciencx | https://www.scien.cx/2022/07/24/code-smell-151-commented-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.