Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 75 — Comments Inside a Method

Maximiliano Contieri
Dev Genius
Published in
1 min readJun 5, 2021

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

Right

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

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

Dave Storer

Sign up to discover human stories that deepen your understanding of the world.

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

Responses (2)

Write a response

I am really beginning to hate the term "Code Smell" because it appears to be a thin veneer label over someone's often times unsubstantiated opinion. I fear that this article is exactly one of those types. There was a time when our profession was…

--

I think the code should be written in a self-explantory fashion, no need for comments unless the code is doing something unusual for example it is a hack, then the comments will serve the purpose. I often see comments that are out of date…

--