cover-img

Code Smell 75 - Comments Inside a Method

Comments are often a code smell. Inserting them inside a method calls for an urgent refactor.

7 June, 2023

4

4

0

Comments are often a code smell. Inserting them inside a method calls for an urgent refactor.

TL;DR: Don't add comments inside your methods. Extract them and leave declarative comments just for not obvious design decisions.

Problems

  • Readability
  • Kiss
  • Low Reuse
  • Bad Documentation

Solutions

  1. Extract Method
  2. Refactor
  3. Remove not declarative comments.

Refactorings

Refactoring 005 - Replace Comment with Function Name

Sample Code

Wrong

function recoverFromGrief() {
// Denial stage
absorbTheBadNews();
setNumbAsProtectiveState();
startToRiseEmotions();
feelSorrow();

// Anger stage
maskRealEffects();
directAngerToOtherPeople();
blameOthers();
getIrrational();

// bargaining stage
feelVulnerable();
regret();
askWhyToMyself();
dreamOfAlternativeWhatIfScenarios();
postponeSadness();

// depression stage
stayQuiet();
getOverwhelmed();
beConfused();

// acceptance stage
acceptWhatHappened();
lookToTheFuture();
reconstructAndWalktrough();
}

Right

function recoverFromGrief() {
denialStage();
angerStage();
bargainingStage();
depressionStage();
acceptanceStage();
}

function denialStage() {
absorbTheBadNews();
setNumbAsProtectiveState();
startToRiseEmotions();
feelSorrow();
}

function angerStage() {
maskRealEffects();
directAngerToOtherPeople();
blameOthers();
getIrrational();
}

function bargainingStage() {
feelVulnerable();
regret();
askWhyToMyself();
dreamOfAlternativeWhatIfScenarios();
postponeSadness();
}

function depressionStage() {
stayQuiet();
getOverwhelmed();
beConfused();
}

function acceptanceStage() {
acceptWhatHappened();
lookToTheFuture();
reconstructAndWalktrough();
}

Detection

This is a policy smell. Every linter can detect comments not present in the first line and warn us.

Tip: (Thanks @GreenFieldCoder)

To get rid of comments fast, change your IDE to display comments with red background and yellow text. It will literally scream refactor me when browsing the code.

Also prevents you from writing new comments.

Tags

  • Readability
  • Long Methods
  • Comments

Conclusion

Comments are a code smell. If you need to document a design decision, you should do it before the actual method code.

Relations

Code Smell 03 - Functions Are Too Long

Code Smell 74 - Empty Lines

Code Smell 05 - Comment Abusers

Code Smell 168 - Undocumented Decisions

Credits

Photo by Jason Rosewell on Unsplash


Don't get suckered in by the comments, they can be terribly misleading: Debug only the code.

Dave Storer

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

4

4

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.