Introduction

Hello and welcome to this lesson on comments and documentation. In our previous lessons, we concentrated on the value of using meaningful names and crafting well-organized functions to enhance the readability and maintainability of clean code. In this session, we'll shift our focus to mastering the art of writing effective comments and documentation, which act as guiding lights through complex sections of the code.

Commenting Practices at a Glance

Comments are a vital instrument for clarity, but they must be wielded judiciously. Below are scenarios where comments are truly beneficial:

  • Legal Comments: ✅ Use these to specify legal obligations and protect intellectual property.
  • Clarification: ✅ Aid understanding by explicating complex or non-obvious code logic.
  • Warning of Consequences: ✅ Highlight potentially risky operations to prevent unintended effects.
  • TODO Comments: ✅ Track tasks or improvements that need to be addressed in the future.
  • PHPDoc for Public Methods: ✅ Provide structured documentation for public methods and APIs, elucidating their usage and input-output expectations.

Here are situations where comments can be detrimental:

  • Redundant Comments: 🔴 Eliminate comments that merely reiterate code.
  • Noise Comments: 🔴 Avoid comments that offer no meaningful insight.
  • Commented-Out Code: 🔴 Remove old code unless retaining it is justified with a reason.
  • Unnecessary PHPDoc: 🔴 Avoid PHPDoc for methods that don't require explanation, such as trivial ones.
Legal Comments

Legal comments are key to ensuring proper acknowledgment and protection of intellectual property. They're used to specify licensing or copyright information, offering legal clarity to users and contributors about the code's usage terms. This practice not only fosters trust but also shields against potential legal disputes:

/*
 * Copyright 2023, XYZ Corporation.
 * Licensed under the Apache License, Version 2.0.
 */

These comments ensure that users and contributors are aware of the legal rights and restrictions associated with the code.

Clarification
Warning of Consequences

These comments serve as cautionary notes, alerting developers to potential impacts or risks within code execution. By highlighting operations that could lead to unintended side effects, they act as safeguards against misuse or oversight, especially in complex systems:

// ⚠️ Warning: This operation will overwrite existing data
saveDataToDatabase($newData);

Such comments can prevent unintentional behavior that might occur from using certain operations.

TODO Comments

Mark sections with tasks or improvements that need attention. They're placeholders for future work, ensuring that ideas for enhancement or optimization don't get lost. Properly maintained TODO comments can help prioritize development focus and facilitate better project management:

// 📝 TODO: Add unit tests for edge cases
function processInput() {
    // Implementation
}
PHPDoc for Public Methods
Redundant Comments
Noise Comments

Eliminate comments that don't add any meaningful insight. Noise comments serve no educational purpose and detract from the clarity of the code. Instead, they introduce more text for developers to parse, slowing down the comprehension process unnecessarily:

// 🔴 Now we increment i
$i++;
// 🔴 End of increment

Noise comments obstruct the flow and make reading code cumbersome.

Commented-Out Code
Unnecessary PHPDoc

Creating PHPDoc for trivial methods adds clutter and should be avoided. It's a misuse of resources to document what is already conventional or intuitive. Focus on documenting nuanced or pivotal aspects of your code instead:

/**
 * 🔴 The main handler.
 */
function main() {
    // Implementation
}

For well-known contextual methods like main, detailed PHPDoc isn't needed.

Summary

In this lesson, we explored various types of comments and documentation, emphasizing writing meaningful, succinct, and maintainable comments while avoiding redundant or noisy ones. You've also seen how to use PHPDoc effectively for public methods. As you proceed to the practice exercises, these guidelines will aid in honing your skills to create clean and well-documented code. Enjoy your practice and keep building those clean coding habits!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal