Code Comments
Code should be commented thoroughly throughout to ensure that code remains readable, approachable, and understandable to anyone reading it. When writing comments, it’s important to think of the “Why” and not only the “What”. Well-written code should be inherently understandable, but does not convey why the code exists and is written that way. For example, the following comments merely repeat the code, and are not useful:Avoid
Prefer
Documentation Blocks
Documentation blocks (or “doc blocks”) are specific types of inline comments with semantic meaning. These are typically attached to a “structural element” (e.g. function, class, etc) and document the element’s behaviour and intent. Doc blocks have a machine readable structure, allowing most editors and IDEs to pull out their data and display it in the editor. This allows better code completion tooling, and allows automated documentation to be generated for the project. Our coding standards require the use of documentation blocks where possible. In doc blocks, you should state the behaviour of the overall element, allowing the element to be understood without viewing the source. Again, this does not mean repeating the code, but rather stating why the element should be used. For example:Avoid
Prefer