xref: /aosp_15_r20/external/google-styleguide/docguide/best_practices.md (revision 8c35d5ee8e2913d4bd6623e2b93232b1da0ab719)
1*8c35d5eeSXin Li# Documentation Best Practices
2*8c35d5eeSXin Li
3*8c35d5eeSXin Li"Say what you mean, simply and directly." - [Brian Kernighan]
4*8c35d5eeSXin Li(https://en.wikipedia.org/wiki/The_Elements_of_Programming_Style)
5*8c35d5eeSXin Li
6*8c35d5eeSXin LiContents:
7*8c35d5eeSXin Li
8*8c35d5eeSXin Li1.  [Minimum viable documentation](#minimum-viable-documentation)
9*8c35d5eeSXin Li1.  [Update docs with code](#update-docs-with-code)
10*8c35d5eeSXin Li1.  [Delete dead documentation](#delete-dead-documentation)
11*8c35d5eeSXin Li1.  [Documentation is the story of your code](#documentation-is-the-story-of-your-code)
12*8c35d5eeSXin Li
13*8c35d5eeSXin Li## Minimum viable documentation
14*8c35d5eeSXin Li
15*8c35d5eeSXin LiA small set of fresh and accurate docs are better than a sprawling, loose
16*8c35d5eeSXin Liassembly of "documentation" in various states of disrepair.
17*8c35d5eeSXin Li
18*8c35d5eeSXin LiWrite short and useful documents. Cut out everything unnecessary, while also
19*8c35d5eeSXin Limaking a habit of continually massaging and improving every doc to suit your
20*8c35d5eeSXin Lichanging needs. **Docs work best when they are alive but frequently trimmed,
21*8c35d5eeSXin Lilike a bonsai tree**.
22*8c35d5eeSXin Li
23*8c35d5eeSXin LiThis guide encourages engineers to take ownership of their docs and keep
24*8c35d5eeSXin Lithem up to date with the same zeal we keep our tests in good order. Strive for
25*8c35d5eeSXin Lithis.
26*8c35d5eeSXin Li
27*8c35d5eeSXin Li* Identify what you really need: release docs, API docs, testing guidelines.
28*8c35d5eeSXin Li* Delete cruft frequently and in small batches.
29*8c35d5eeSXin Li
30*8c35d5eeSXin Li## Update docs with code
31*8c35d5eeSXin Li
32*8c35d5eeSXin Li**Change your documentation in the same CL as the code change**. This keeps your
33*8c35d5eeSXin Lidocs fresh, and is also a good place to explain to your reviewer what you're
34*8c35d5eeSXin Lidoing.
35*8c35d5eeSXin Li
36*8c35d5eeSXin LiA good reviewer can at least insist that docstrings, header files, README.md
37*8c35d5eeSXin Lifiles, and any other docs get updated alongside the CL.
38*8c35d5eeSXin Li
39*8c35d5eeSXin Li## Delete dead documentation
40*8c35d5eeSXin Li
41*8c35d5eeSXin LiDead docs are bad. They misinform, they slow down, they incite despair in
42*8c35d5eeSXin Liengineers and laziness in team leads. They set a precedent for leaving behind
43*8c35d5eeSXin Limesses in a code base. If your home is clean, most guests will be clean without
44*8c35d5eeSXin Libeing asked.
45*8c35d5eeSXin Li
46*8c35d5eeSXin LiJust like any big cleaning project, **it's easy to be overwhelmed**. If your
47*8c35d5eeSXin Lidocs are in bad shape:
48*8c35d5eeSXin Li
49*8c35d5eeSXin Li*   Take it slow, doc health is a gradual accumulation.
50*8c35d5eeSXin Li*   First delete what you're certain is wrong, ignore what's unclear.
51*8c35d5eeSXin Li*   Get your whole team involved. Devote time to quickly scan every doc and make
52*8c35d5eeSXin Li    a simple decision: Keep or delete?
53*8c35d5eeSXin Li*   Default to delete or leave behind if migrating. Stragglers can always be
54*8c35d5eeSXin Li    recovered.
55*8c35d5eeSXin Li*   Iterate.
56*8c35d5eeSXin Li
57*8c35d5eeSXin Li## Prefer the good over the perfect
58*8c35d5eeSXin Li
59*8c35d5eeSXin LiYour documentation should be as good as possible within a reasonable time frame.
60*8c35d5eeSXin LiThe standards for a documentation review are different from the
61*8c35d5eeSXin Listandards for code reviews. Reviewers can and should ask for improvements, but
62*8c35d5eeSXin Liin general, the author should always be able to invoke the "Good Over Perfect
63*8c35d5eeSXin LiRule". It's preferable to allow authors to quickly submit changes that improve
64*8c35d5eeSXin Lithe document, instead of forcing rounds of review until it's "perfect". Docs are
65*8c35d5eeSXin Linever perfect, and tend to gradually improve as the team learns what they really
66*8c35d5eeSXin Lineed to write down.
67*8c35d5eeSXin Li
68*8c35d5eeSXin Li## Documentation is the story of your code
69*8c35d5eeSXin Li
70*8c35d5eeSXin LiWriting excellent code doesn't end when your code compiles or even if your
71*8c35d5eeSXin Litest coverage reaches 100%. It's easy to write something a computer understands,
72*8c35d5eeSXin Liit's much harder to write something both a human and a computer understand. Your
73*8c35d5eeSXin Limission as a Code Health-conscious engineer is to **write for humans first,
74*8c35d5eeSXin Licomputers second.** Documentation is an important part of this skill.
75*8c35d5eeSXin Li
76*8c35d5eeSXin LiThere's a spectrum of engineering documentation that ranges from terse comments
77*8c35d5eeSXin Lito detailed prose:
78*8c35d5eeSXin Li
79*8c35d5eeSXin Li1.  **Inline comments**: The primary purpose of inline comments is to provide
80*8c35d5eeSXin Li    information that the code itself cannot contain, such as why the code is
81*8c35d5eeSXin Li    there.
82*8c35d5eeSXin Li
83*8c35d5eeSXin Li2.  **Method and class comments**:
84*8c35d5eeSXin Li
85*8c35d5eeSXin Li    *   **Method API documentation**: The header / Javadoc / docstring
86*8c35d5eeSXin Li        comments that say what methods do and how to use them. This
87*8c35d5eeSXin Li        documentation is **the contract of how your code must behave**. The
88*8c35d5eeSXin Li        intended audience is future programmers who will use and modify your
89*8c35d5eeSXin Li        code.
90*8c35d5eeSXin Li
91*8c35d5eeSXin Li        It is often reasonable to say that any behavior documented here should
92*8c35d5eeSXin Li        have a test verifying it. This documentation details what arguments the
93*8c35d5eeSXin Li        method takes, what it returns, any "gotchas" or restrictions, and what
94*8c35d5eeSXin Li        exceptions it can throw or errors it can return. It does not usually
95*8c35d5eeSXin Li        explain why code behaves a particular way unless that's relevant to a
96*8c35d5eeSXin Li        developer's understanding of how to use the method. "Why" explanations
97*8c35d5eeSXin Li        are for inline comments. Think in practical terms when writing method
98*8c35d5eeSXin Li        documentation: "This is a hammer. You use it to pound nails."
99*8c35d5eeSXin Li
100*8c35d5eeSXin Li    *   **Class / Module API documentation**: The header / Javadoc / docstring
101*8c35d5eeSXin Li        comments for a class or a whole file. This documentation gives a brief
102*8c35d5eeSXin Li        overview of what the class / file does and often gives a few short
103*8c35d5eeSXin Li        examples of how you might use the class / file.
104*8c35d5eeSXin Li
105*8c35d5eeSXin Li        Examples are particularly relevant when there's several distinct ways to
106*8c35d5eeSXin Li        use the class (some advanced, some simple). Always list the simplest
107*8c35d5eeSXin Li        use case first.
108*8c35d5eeSXin Li
109*8c35d5eeSXin Li3.  **README.md**: A good README.md orients the new user to the directory and
110*8c35d5eeSXin Li    points to more detailed explanation and user guides:
111*8c35d5eeSXin Li    * What is this directory intended to hold?
112*8c35d5eeSXin Li    * Which files should the developer look at first? Are some files an API?
113*8c35d5eeSXin Li    * Who maintains this directory and where I can learn more?
114*8c35d5eeSXin Li
115*8c35d5eeSXin Li    See the [README.md guidelines](READMEs.md).
116