
Darwin Martin
Principal Software Engineer

The Art of Code Comments: Explaining the 'Why' Not the 'What'
In software development, clear and effective communication is just as crucial as the code itself. Comments within your code serve as guideposts for other developers—including future you—who need to understand your thought process. This blog post explores why comments should focus on explaining the 'why' behind the code decisions, rather than just detailing 'what' the code does.
Comments in code are meant to clarify the intent, rationale, and complexity that might not be immediately apparent from the code itself. While the code explains 'what' is being done, comments should explain 'why' it is done that way. This distinction is crucial for several reasons:
- Maintainability: Future maintainers of the code can understand the context and reasons behind specific decisions. This insight is invaluable when they need to modify or extend the existing code without breaking functionality.
- Complexity Explanation: Some code is inherently complex due to business rules, optimizations, or compromises. Comments that explain these complexities ensure that the intent behind such code is not lost over time.
- Avoiding Redundancy: Explaining 'what' the code does is often redundant if the code is written clearly. Modern coding practices and naming conventions should ensure that the code is self-explanatory to some extent.
Writing useful comments is a skill that improves with practice and intent. Here are some best practices to enhance the quality of your comments:
- Be Concise and Relevant: Your comments should be brief and to the point. Avoid stating the obvious, and focus on adding value with additional context or explanations that are not immediately obvious from the code.
- Use the Right Tools: For complex logic, consider using diagrams or additional documentation outside the codebase, referenced by a brief comment. Tools like Doxygen or Javadoc can be used to generate documentation that includes these diagrams and extensive explanations.
- Regular Updates: Comments can become outdated as the code evolves. It's crucial to maintain comments as diligently as you maintain your code. Ensure that changes in code logic are reflected in your comments.
Comments are a significant part of your codebase. They communicate not just to others but also to your future self. By focusing on the 'why' rather than the 'what,' comments can transform from mere annotations into a rich, useful documentation of your software's design and decision-making processes.
Examples of Good vs. Bad Comments:
Good Comment:
// Compensate for edge case due to unexpected API response format
if (response.isEmpty) {
response = defaultResponse;
}
Bad Comment:
// Check if response is empty
if (response.isEmpty) {
response = defaultResponse; // Set response to default
}
Think about the last time you struggled to understand a piece of code because of inadequate commenting. Going forward, how can you improve your comments to help someone else—including future you—understand your code better? Share your thoughts and experiences in the comments below!