DEV Community

Cover image for Code Smell 75 - Comments Inside a Method
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3 3

Code Smell 75 - Comments Inside a Method

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.

Sample Code

Wrong

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

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

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

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

    // acceptance stage
    acceptWhatHappened();
    lookToTheFuture();
    reconstructAndWalktrough();
}
Enter fullscreen mode Exit fullscreen mode

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();
    postoponeSadness();
}

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

function acceptanceStage() {
    acceptWhatHappened();
    lookToTheFuture();
    reconstructAndWalktrough();
}
Enter fullscreen mode Exit fullscreen mode

Detection

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

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

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


This article is part of the CodeSmell Series.

Last update: 2021/06/05

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

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay