Documenting code
How we document our code and where to find it
Last updated
Was this helpful?
How we document our code and where to find it
Last updated
Was this helpful?
Most languages have three types of comments
Inline comments: // like this
or # like this
Use these sparingly, and mainly to document one or a few unclear lines of code
Multiline comments: /* Like this */
or """ Like this """
Avoid these, as they are often out-of-context and inaccessible outside of the code
Doc comments: /// Like this
or """Like this, but in a function"""
Use these on every function, class, and field
This section focuses on doc comments, which should be used to describe how to use your code to someone who is reading it for the first time.
All repositories have a GitHub workflow configured that scans the code for doc comments, compiles them into a clean and searchable website, and uploads it to the repository's documentation page. For example, you can find the Dashboard's documentation here
For Dart repositories, the documentation looks like this:
First off, use proper English. Full sentences, capitalization, etc. Look at the Flutter docs for some really good examples of proper documentation. Structure your sentences as though you are the author of the code (even if you're only making small changes) and are talking to a reader who is trying to use your code or fix some sort of bug.
Don't describe the actual implementation of your function; your code should do that well enough, with inline comments where needed. Instead, describe how to use your function, when it should be called, and any side effects of calling it. Avoid saying "this function" or "this variable". For example,
This function opens a connection to the dashboard.Opens a connection to the dashboard.
It can be helpful to first document any fields in your class, then methods/functions, and then the class itself. The class docs shouldn't repeat anything (except for a basic overview) but should instead reference your other docs. The function/field docs should describe what each member does but the class docs should describe how to use/construct/manage the class as a whole.
Your doc comments should be structured like this:
To provide a coherent docs experience, be sure to reference other documentation where relevant. For example,
The documentation tools (either Doxygen or DartDocs) will automatically insert a link to the relevant page when you include a reference. In Doxygen, the syntax is #MyClass
or MyClass::Function
. Avoid using references excessively. For example,
The doc pages already show the return type and parameter types, so save references for code that isn't part of the code you are documenting, like other functions and side effects.