Chapter 5. Knowing What to Comment
The goal of this chapter is to help you realize what you should be commenting. You might think the purpose of commenting is to âexplain what the code does,â but that is just a small part of it.
Key Idea
The purpose of commenting is to help the reader know as much as the writer did.
When youâre writing code, you have a lot of valuable information in your head. When other people read your code, that information is lostâall they have is the code in front of them.
In this chapter, weâll show you many examples of when to write down that information in your head. Weâve left out the more mundane points about commenting; instead, weâve focused on the more interesting and âunderservedâ aspects of commenting.
Weâve organized the chapter into the following areas:
Knowing what not to comment
Recording your thoughts as you code
Putting yourself in the readersâ shoes, to imagine what theyâll need to know
What NOT to Comment
Reading a comment takes time away from reading the actual code, and each comment takes up space on the screen. That is, it better be worth it. So where do you draw the line between a worthless comment and a good one?
All of the comments in this code are worthless:
// The class definition for Account class Account { public: // Constructor ...
Get The Art of Readable Code now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.