Skip to main content
Every piece of code, whether for the frontend or backend, must come with comprehensive documentation. This means including inline comments within the code itself and providing detailed explanations in project wikis and readme files.

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
Instead, we should document the intent of the code where it makes sense, and let the code speak for itself in obvious places.
Prefer
This could be improved further by making the code clearer by removing “magic numbers” and turning them into constants instead.
Inline comments used correctly should add to the readability of the code by providing context on decisions.

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

Excessive Commenting

When commenting code, you need to exercise judgement on when to comment. Poorly written inline comments can reduce the readability of the code by breaking up the mental flow of reading code, and make the code less maintainable. For example, the following code contains 9 lines of code, but the comments break it up in to an unreadable 40 lines:
(Example shortened from the original for brevity.) Instead, this code should simply document the intent of the code where necessary, and let the code speak for itself:
Note that this example is about the commenting style only. In practice, this code should be rewritten to avoid micro-optimisations and unnecessary code.

Technical Design Documentation

Technical Design Documentation is important for understanding the project at a higher level. This is where you can document decisions made about architecture, choice of external libraries and plugins, and process. Long-lived documentation for a project should live within the project repository in the form of markdown files. This ensures that all documentation is version-controlled along with the project, and any changes needed to documentation can be done as part of a pull request on the project. As a standard, projects should also contain READMEs at the project root level. These READMEs should lay out the basics of getting started with the project (i.e. instructions for installation and workflow), and no other information.

Credential Ownership

We often supply API keys, OAuth client configurations, or other “secrets” in config files and code files. When working on projects where you are using or setting up an API key, please make sure that you comment on the ownership of the API keys somewhere clear. This should be done inline above where it is used as much as possible.

Examples:

variables stored in code:

Further Reading