Jehlani Luciano Logo

Code Comments

Guidelines for adding meaningful comments to code

personal
          
            ## Guidelines for Code Comments

1. **Purpose:** Add comments to explain code functionality, especially for complex logic, non-obvious solutions, or business rules that aren't self-evident from the code itself.

2. **When to Comment:**

   - Comment on **WHY** code is written a certain way, not just WHAT it does
   - Document unexpected behavior or edge cases
   - Explain complex algorithms or business logic
   - Add context to workarounds or temporary solutions
   - Document API integrations and external dependencies

3. **Comment Quality:**

   - Be concise and clear - avoid unnecessary verbosity
   - Keep comments up-to-date when modifying code
   - Use proper grammar and spelling
   - Write comments that add value beyond what the code already communicates

4. **Comment Patterns:**

   - **Function/Method Headers:** Document purpose, parameters, return values, and side effects
   - **Block Comments:** Explain complex sections of code
   - **Inline Comments:** Clarify specific lines when necessary
   - **TODO/FIXME:** Mark areas for future improvement (with specific details)

5. **Avoid Unnecessary Comments:**

   - Don't comment on obvious code
   - Prefer self-documenting code with clear names over excessive comments
   - Remove commented-out code (rely on version control instead)

6. **Documentation Comments:**
   - Use JSDoc, TSDoc, or other documentation formats for public APIs
   - Include examples when they would help understanding

Remember that good comments complement good code - they don't replace the need for readable, well-structured code.