Skip to content
SciTools Blog
Menu
  • Home
  • Blog
  • Support
  • Contact
  • Login
  • Pricing
  • Free Trial
Menu

Add Comments When the Code Can’t Explain Itself

Posted on September 21, 2023

TLDR

The one certain time you should add comments to code is when the code can’t or doesn’t explain itself.

Adding Comments

I read a lot of code. And thus, I read a lot of comments. Many are useless and distract from reading the code. Take this, for example:

int countOfSortIterations = 0;  // Count of Sort Iterations, defaults to zero.

But sometimes, comments are literally… gold:

A screenshot of an in-line comment left in the Understand static analysis tool.

This comment is key to succeeding in using this code, or maintaining it, or in using this code as an example/pattern for some other way you are going to use this API.

I asked the engineer about it, since he’s a very tight coder that generally doesn’t add many comments:

In this case, the comment is essential. The comment is the only documentation available. It wouldn’t be as critical if the library had some other kind of documentation. It’s still useful to document in the code because you’re more likely to update the comment when the code changes.

If this comment wasn’t here, somebody maintaining or using this code is going to waste hours (days??) sorting out what needs to happen or why things aren’t working.

This suggests a good guideline for deciding when to add well-written explanatory comments:

What can I say that is critical for maintaining or using this code that the CODE CAN’T readily explain on its own?

Comments, like all other parts of code, should be lean and have a specific and necessary purpose.

  • Instagram
  • Facebook
  • LinkedIn
  • Twitter
  • YouTube

Learn more about Understand's Features

  • Dependency Graph
    View Dependency Graphs
  • Comply with Standards
  • View Useful Metrics
  • Team Annotations
  • Browse Depenedencies
  • Edit and Refactor

Related

  • API
  • Architectures
  • Business
  • Code Comparison
  • Code Comprehension
  • Code Navigation
  • Code Visualization
  • Coding Standards
  • Dependencies
  • Developer Notes
  • DevOps
  • Getting Started
  • Legacy Code
  • Licensing
  • Metrics
  • Platform
  • Plugins
  • Power User Tips
  • Programming Practices
  • Uncategorized
  • Useful Scripts
  • User Stories
  • May 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • April 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

©2025 SciTools Blog | Design: Newspaperly WordPress Theme