Documenting intent vs mechanics
What you'll learn: How to write comments that explain why your code exists rather than simply repeating what it is doing.
The "Translate to English" Trap
When you first start writing code, it is tempting to use comments as a literal translation service. You might write a line of C and then write a comment right next to it that says the exact same thing in English.
Imagine reading a cookbook where every instruction had a redundant description:
- Step: Crack the egg. (Description: The chef is breaking the shell of the egg).
- Step: Turn on the stove. (Description: The chef is activating the heating element).
The descriptions don't help you understand the meal; they just take up space. In programming, this is called documenting mechanics. Since the computer already reads the code, and other programmers already know C, explaining the syntax is redundant.
Explaining the "Why"
Instead, your comments should focus on intent. Intent tells the reader what you are trying to achieve or why a specific decision was made. If the code is the "how," the comment is the "why."
Think of a "No Parking" sign. A mechanical description would be: "Metal circle with a red diagonal line over the letter P." The intent is: "Keep this lane clear for emergency vehicles." The second one is much more useful if a driver is wondering why they can’t stop there.
Comparing the two
Take a look at how the same piece of code feels with mechanical comments versus intentional comments:
#include <stdio.h>
int main() {
float price = 100.00;
// MECHANICAL (Avoid this):
// Multiply price by 0.05 and add it back to price
price = price + (price * 0.05);
// INTENTIONAL (Do this):
// Apply the local state sales tax before finalizing the total
price = price + (price * 0.05);
return 0;
}
In the mechanical example, the comment is just "C code written in English." If you change the math, you have to change the comment too. In the intentional example, the comment explains the purpose of the math. If a future programmer sees a bug in the tax calculation, they know exactly what that line was supposed to be doing.
Key takeaway
Don't describe the machinery; describe the goal.