Ben and Matt explore the purpose and characteristics of good comments in code, discuss the management of to-do comments in codebases, and emphasize the role of comments in thread safety and code understanding. They also outline the risks of leaving commented out code and suggest alternatives like source control and refactoring.
42:17
AI Summary
Highlights
AI Chapters
Episode notes
auto_awesome
Podcast summary created with Snipd AI
Quick takeaways
Good comments focus on the 'why' rather than the 'how', providing insights that are not evident by simply reading the code.
Comments can be useful for documenting code, but care should be taken to avoid duplicating code or stating the obvious.
Deep dives
The Importance of Writing Good Comments in Code
Writing good comments in code is essential as they provide clarity and context to readers. Engineers often ask applicants to write code and explain their choices to understand their thought process. Comments serve as a communication tool between developers and express the intentions behind certain decisions. However, not all comments are valuable. Comments that state the obvious or duplicate code are considered bad comments. Extracting code into functions with meaningful names can often make comments unnecessary. Good comments focus on the 'why' rather than the 'how', providing insights that are not evident by simply reading the code. Additionally, comments that document potential issues, thread safety requirements, or cross-references to external documentation can be helpful. On the other hand, comments should never substitute for proper documentation or rely on commented-out code. Leveraging version control and pre-commit hooks can prevent accidental commits of commented-out code or code meant only for testing purposes.
Using Comments for Documentation and Doxygen Style Comments
Comments can be useful for documenting code, but care should be taken to avoid duplicating code or stating the obvious. Documentation comments, such as Doxygen-style comments, can provide information about how to use a function or class. However, relying solely on comments for documentation can be problematic. It's important to strike a balance between code clarity and the need for additional information. Comments should focus on explaining the 'why' behind the code, highlighting potential issues, and providing insights that go beyond what is evident from the code itself. Using comments for documentation or linking to external documentation can be valuable, especially when they provide examples or address thread safety concerns. However, comments should never replace thorough documentation or code readability.
Avoiding Bad Comment Practices
Certain comment practices should be avoided. Comments that duplicate code or contain commented-out code are considered bad practice. Commented-out code leads to confusion and potential errors when code is modified or read by others. Relying on source control and pre-commit hooks can prevent accidentally committing commented-out code. In addition, comments that only state the obvious or repeat what is already evident from the code add noise and provide little value. Proper naming conventions and well-written code can often eliminate the need for such comments. Instead, focus on commenting for clarity and context, providing insights into decisions and addressing potential issues. By avoiding bad comment practices, code readability and maintainability can be greatly improved.
The Value of Testing Code in Comments and Version Control Best Practices
Comments can be a valuable tool for testing code within a codebase. Doc tests, where code examples are provided within comments, can help ensure code correctness and functionality. These tests can also serve as valuable documentation, providing practical examples of code usage. However, it is important to ensure that these tests remain up-to-date and align with the actual code. Leveraging version control systems and pre-commit hooks can help catch and prevent outdated tests from being committed. Additionally, using proper version control practices and avoiding unnecessary commented-out code can help maintain code integrity and prevent issues in production. By using comments for testing purposes and following best practices for version control, developers can enhance code quality and reliability.
Ben and Matt comment on different types of comments in code. Join our hosts and they explore both good and bad types of comments, from the essential to the inexcusable. Matt explains how to bump the failure counter to 99. Ben suggests violence against cats.
Remember Everything You Learn from Podcasts
Save insights instantly, chat with episodes, and build lasting knowledge - all powered by AI.