1.. ## Copyright (c) 2017-2021, Lawrence Livermore National Security, LLC and
2.. ## other Axom Project Developers. See the top-level LICENSE file for details.
3.. ##
4.. ## SPDX-License-Identifier: (BSD-3-Clause)
5
6.. _docsec-label:
7
8========================================
97 Code Documentation
10========================================
11
12This section contains guidelines for content and formatting of code
13documentation mentioned in earlier sections. The aims of these
14guidelines are to:
15
16   * Document files, data types, functions, etc. consistently.
17   * Promote good documentation practices so that essential information is
18     presented clearly and lucidly, and which do not over-burden developers.
19   * Generate source code documentation using the Doxygen system.
20
21
22-----------------------------------------
23Document only what's needed
24-----------------------------------------
25
267.1 Documentation **should** only include what is essential for users and
27other developers to easily understand code. Comments **should** be limited to
28describing constraints, pre- and post-conditions, and other issues that
29are important, but not obvious. Extraneous comments (e.g., documenting
30"the obvious") **should** be avoided.
31
32      Code that uses clear, descriptive names (functions, variables, etc.) and
33      clear logical structure is preferable to code that relies on a lot of
34      comments for understanding. To be useful, comments must be understood by
35      others and kept current with the actual code. Generally, maintenance
36      and understanding are better served by rewriting tricky, unclear code
37      than by adding comments to it.
38
39
40-----------------------------------------
41Documenting new code vs. existing code
42-----------------------------------------
43
447.2 New source code **must** be documented following the guidelines in this
45section. Documentation of existing code **should** be modified to conform to
46these guidelines when appropriate.
47
487.3 Existing code documentation **should** be improved when its inadequate,
49incorrect, or unclear.
50
51.. note:: When code is modified, documentation **must** be changed to reflect
52          the changes.
53
54
55-----------------------------------------
56Write clear documentation
57-----------------------------------------
58
597.4 To make comment text clear and reduce confusion, code comments
60**should** be written in grammatically-correct complete sentences or
61easily understood sentence fragments.
62
63
64-----------------------------------------
65Documentation should be easy to spot
66-----------------------------------------
67
687.5 End-of-line comments **should not** be used to document code logic,
69since they tend to be less visible than other comment forms and may be
70difficult to format cleanly.
71
72      Short end-of-line comments **may** be useful for labeling closing braces
73      associated with nested loops, conditionals, for scope in general, and
74      for documenting local variable declarations.
75
767.6 All comments, except end-of-line comments, **should** be indented to
77match the indentation of the code they are documenting. Multiple line comment
78blocks **should** be aligned vertically on the left.
79
807.7 Comments **should** be clearly delimited from executable code with blank
81lines and "blocking characters" (see examples below) to make them stand out
82and, thus, improve the chances they will be read.
83
847.8 White space, such as blank lines, indentation, and vertical alignment
85**should** be used in comment blocks to enhance readability, emphasize
86important information, etc.
87
88
89--------------------------------------------------------------------
90General Doxygen usage
91--------------------------------------------------------------------
92
93The Doxygen code documentation system uses C or C++ style comment sections
94with special markings and Doxygen-specific commands to extract documentation
95from source and header files. Although Doxygen provides many sophisticated
96documentation capabilities and can generate a source code manual in a variety
97of formats such as LaTeX, PDF, and HTML, these guidelines address only a small
98subset of Doxygen syntax. The goal of adhering to a small, simple set of
99documentation commands is that developers will be encouraged to build useful
100documentation when they are writing code.
101
102
103Brief vs. detailed comments
104^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105
106The Doxygen system interprets each documentation comment as either "brief"
107or "detailed".
108
109 - A brief comment is a concise statement of purpose for an item (usually no
110   more than one line) and starts with the Doxygen command "\\brief"
111   (or "@brief"). Brief comments appear in summary sections of the generated
112   documentation. They are typically seen before detailed comments when
113   scanning the documentation; thus good brief comments make it easier to
114   can or navigate a source code manual.
115
116 - A detailed comment is any comment that is not identified as 'brief'.
117
1187.9 A "brief" description **should** be provided in the Doxygen comment
119section for each of the following items:
120
121      * A type definition (i.e., class, struct, typedef, enum, etc.)
122      * A macro definition
123      * A struct field or class data member
124      * A class member function declaration (in the header file class
125        definition)
126      * An unbound function signature (in a header file)
127      * A function implementation (when there is no description in the
128        associated header file)
129
1307.10 Important information of a more lengthy nature (e.g., usage examples
131spanning multiple lines) **should** be provided for files, major data types
132and definitions, functions, etc. when needed. A detailed comment **must** be
133separated from a brief comment in the same comment block with a line containing
134no documentation text.
135
136
137Doxygen comment blocks
138^^^^^^^^^^^^^^^^^^^^^^^
139
1407.11 Doxygen comment blocks **may** use either JavaDoc, Qt style, or one
141of the C++ comment forms described below.
142
143      JavaDoc style comments consist of a C-style comment block starting with
144      two \*'s, like this::
145
146         /**
147          * ...comment text...
148          */
149
150      Qt style comments add an exclamation mark (!) after the opening of a
151      C-style comment block,like this::
152
153         /*!
154          * ...comment text...
155          */
156
157      For JavaDoc or Qt style comments, the asterisk characters ("\*") on
158      intermediate lines are optional, but encouraged.
159
160      C++ comment block forms start each line with an additional slash::
161
162         ///
163         /// ...comment text...
164         ///
165
166      or an exclamation mark::
167
168         //!
169         //! ...comment text...
170         //!
171
172      For these C++ style comment forms, the comment delimiter is required on
173      each line.
174
1757.12 A consistent Doxygen comment block style **must** be used within a component.
176
1777.13 Doxygen comment blocks **must** appear immediately before the items they
178describe; i.e., no blank lines between comment and documented item. This
179insures that Doxygen will properly associate the comment with the item.
180
181
182Doxygen inline comments
183^^^^^^^^^^^^^^^^^^^^^^^^
184
1857.14 Inline Doxygen comments **may** be used for class/struct data members,
186enum values, function arguments, etc.
187
188      When inline comments are used, they **must** appear after the item
189      **on the same line** and **must** use the following syntax::
190
191          /*!< ...comment text... */
192
193      Note that the "<" character must appear immediately after the opening of
194      the Doxygen comment (with no space before). This tells Doxygen that the
195      comment applies to the item immediately preceding the comment. See
196      examples in later sections.
197
1987.15 When an item is documented using the inline form, the comment
199**should not** span multiple lines.
200
201
202--------------------------------------------------------------------
203Copyright and release statement
204--------------------------------------------------------------------
205
2067.16 Each file **must** contain a comment section that includes the project
207software release information (using whichever comment characters are
208appropriate for the language the file is written in). In the interest of
209brevity, the complete release statement is summarized here to show the
210essential information. The full version can be found in any of the project
211files.
212
213.. note:: Change this when we release the code.
214
215.. code-block:: cpp
216
217   /*
218    * Copyright (c) 2017-2021, Lawrence Livermore National Security, LLC.
219    * Produced at the Lawrence Livermore National Laboratory.
220    *
221    * All rights reserved.
222    *
223    * This source code cannot be distributed without permission and
224    * further review from Lawrence Livermore National Laboratory.
225    */
226
227See :ref:`headerlayout-label` and :ref:`sourcelayout-label` for guidelines
228on placement of copyright and release statement in header and source files,
229respectively.
230
231
232--------------------------------------------------------------------
233File documentation
234--------------------------------------------------------------------
235
2367.17 Each header file that declares a global type, method, etc. **must**
237have a Doxygen file prologue similar to the following:
238
239.. code-block:: cpp
240
241   /*!
242    ***************************************************************************
243    *
244    * \file ...optional name of file...
245    *
246    * \brief A brief statement describing the file contents/purpose. (optional)
247    *
248    * Optional detailed explanatory notes about the file.
249    *
250    ****************************************************************************
251    */
252
253      The "\\file" command **must** appear first in the file prologue. It
254      identifies the comment section as documentation for the file.
255
256      The file name **may** include (part of) the path if the file name is not
257      unique. If the file name is omitted on the line after the "\\file"
258      command, then any documentation in the comment block will belong to
259      the file in which it is located instead of the summary documentation
260      in the listing of documented files.
261
262.. note:: Doxygen requires that a file itself must be documented for
263          documentation to be generated for any global item (global function,
264          typedef, enum, etc.) defined in the file.
265
266See :ref:`headerlayout-label` and :ref:`sourcelayout-label` for guidelines
267on placement of file prologue in header and source files, respectively.
268
269
270Brief and detailed comments
271^^^^^^^^^^^^^^^^^^^^^^^^^^^^
272
2737.18 A brief statement of purpose for the file **should** appear as the first
274comment after the file command. If included, the brief statement, **must** be
275preceded by the "\\brief" command.
276
2777.19 Any detailed notes about the file **may** be included after the brief
278comment. If this is done, the detailed comments **must** be separated from
279the brief statement by a line containing no documentation text.
280
281
282--------------------------------------------------------------------
283Type documentation
284--------------------------------------------------------------------
285
2867.20 Each type and macro definition appearing in a header file **must** have
287a Doxygen type definition comment prologue immediately before it. For example
288
289.. code-block:: cpp
290
291   /*!
292    ****************************************************************************
293    *
294    * \brief A brief statement of purpose of the type or macro.
295    *
296    * Optional detailed information that is helpful in understanding the
297    * purpose, usage, etc. of the type/macro ...
298    *
299    * \sa optional cross-reference to other types, functions, etc...
300    * \sa etc...
301    *
302    * \warning This class is only partially functional.
303    *
304    ****************************************************************************
305    */
306
307.. note:: Doxygen requires that a compound entity, such as a class, struct,
308          etc. be documented in order to document any of its members.
309
310
311Brief and detailed comments
312^^^^^^^^^^^^^^^^^^^^^^^^^^^^
313
3147.21 A brief statement describing the type **must** appear as the first text
315comment using the Doxygen command "\\brief".
316
3177.22 Important details about the item **should** be included after the brief
318comment and, if included, **must** be separated from the brief comment by a
319blank line.
320
321
322Cross-references and caveats
323^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
324
3257.23 Cross-references to other items, such as other related types **should**
326be included in the prologue to enhance the navigability of the documentation.
327
328      The Doxygen command "\\sa" (for "see also") **should** appear before each
329      such cross-reference so that links are generated in the documentation.
330
3317.24 Caveats or limitations about the documented type **should** be noted
332using the "\\warning" Doxygen command as shown above.
333
334
335--------------------------------------------------------------------
336Function documentation
337--------------------------------------------------------------------
338
3397.25 Each unbound function **should** be be documented with a function
340prologue in the header file where its prototype appears or in a source file
341immediately preceding its implementation.
342
3437.26 Since C++ class member functions define the class interface, they
344**should** be documented with a function prologue immediately preceding
345their declaration in the class definition.
346
347
348Example function documentation
349^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
350
351The following examples show two function prologue variations that may
352be used to document a method in a class definition. The first shows how
353to document the function arguments in the function prologue.
354
355.. code-block:: cpp
356
357      /*!
358       *************************************************************************
359       *
360       * \brief Initialize a Foo object with given operation mode.
361       *
362       * The "read" mode means one thing, while "write" mode means another.
363       *
364       * \return bool indicating success or failure of initialization.
365       *              Success returns true, failure returns false.
366       *
367       * \param[in] mode OpMode enum value specifying initialization mode.
368       *                 ReadMode and WriteMode are valid options.
369       *                 Any other value generates a warning message and the
370       *                 failure value ("false") is returned.
371       *
372       *************************************************************************
373       */
374       bool initMode(OpMode mode);
375
376The second example shows how to document the function argument inline.
377
378.. code-block:: cpp
379
380      /*!
381       ************************************************************************
382       *
383       * @brief Initialize a Foo object to given operation mode.
384       *
385       * The "read" mode means one thing, while "write" mode means another.
386       *
387       * @return bool value indicating success or failure of initialization.
388       *             Success returns true, failure returns false.
389       *
390       *************************************************************************
391       */
392       bool initMode(OpMode mode /*!< [in] ReadMode, WriteMode are valid options */ );
393
394Note that the first example uses the "\\" character to identify Doxygen
395commands; the second uses "@".
396
397
398Brief and detailed comments
399^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
400
4017.27 A brief statement of purpose for a function must appear as the first
402text comment after the Doxygen command "\\brief" (or "@brief").
403
4047.28 Any detailed function description, when included, **must** appear
405after the brief comment and **must** be separated from the brief comment by
406a line containing no text.
407
408
409Return values
410^^^^^^^^^^^^^
411
4127.29 If the function has a non-void return type, the return value **should**
413be documented in the prologue using the Doxygen command "\\return"
414(or "@return") preceding a description of the return value.
415
416      Functions with "void" return type and C++ class constructors and
417      destructors **should not** have such documentation.
418
419Arguments
420^^^^^^^^^^^^^^^^^^^
421
4227.30 Function arguments **should** be documented in the function prologue
423or inline (as shown above) when the intent or usage of the arguments is not
424obvious.
425
426      The inline form of the comment may be preferable when the argument
427      documentation is short. When a longer description is provided (such as
428      when noting the range of valid values, error conditions, etc.) the
429      description **should** be placed within the function prologue for
430      readability. However, the two alternatives for documenting function
431      arguments **must not** be mixed within the documentation of a single
432      function to reduce confusion.
433
434      In any case, superfluous documentation should be avoided. For example,
435      when there are one or two arguments and their meaning is obvious from
436      their names or the description of the function, providing no comments is
437      better than cluttering the code by documenting the obvious. Comments
438      that impart no useful information are distracting and less helpful than
439      no comment at all.
440
4417.31 When a function argument is documented in the prologue comment section,
442the Doxygen command "\param" **should** appear before the comment as in the
443first example above.
444
4457.32 The "in/out" status of each function argument **should** be documented.
446
447       The Doxygen "\param" command supports this directly by allowing such an
448       attribute to be specified as "\param[in]", "\param[out]", or
449       "\param[in,out]". Although the inline comment form does not support
450       this, such a description **should** be included; e.g., by using "[in]",
451       "[out]", or "[in,out]" in the comment.
452
453
454Grouping small functions
455^^^^^^^^^^^^^^^^^^^^^^^^^
456
4577.33 Short, simple functions (e.g., inline methods) **may** be grouped
458together and documented with a single descriptive comment when this is
459sufficient.
460
461      An example of Doxygen syntax for such a grouping is::
462
463         //@{
464         //! @name Setters for data members
465
466         void setMember1(int arg1) { m_member1 = arg1; }
467         void setMember2(int arg2) { m_member2 = arg2; }
468
469         //@}
470
471
472Header file vs. source file documentation
473^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
474
4757.34 Important implementation details (vs. usage detailed) about a function
476**should** be documented in the source file where the function is implemented,
477rather than the header file where the function is declared.
478
479      Header file documentation **should** include only purpose and usage
480      information germane to an interface. When a function has separate
481      implementation documentation, the comments **must not** contain Doxygen
482      syntax. Using Doxygen syntax to document an item in more than one location
483      (e.g., header file and source file) can cause undesired Doxygen
484      formatting issues and potentially confusing documentation.
485
486      A member of a class may be documented as follows in the source file
487      for the class as follows (i.e., no Doxygen comments)::
488
489        /*
490         ***********************************************************************
491         *
492         * Set operation mode for a Foo object.
493         *
494         * Important detailed information about what the function does...
495         *
496         ***********************************************************************
497         */
498         bool Foo::initMode(OpMode mode)
499         {
500            ...function body...
501         }
502
503
504--------------------------------------------------------------------
505Data member documentation
506--------------------------------------------------------------------
507
5087.35 Each struct field or class data member **should** have a descriptive
509comment indicating its purpose.
510
511     This comment may as appear as a prologue before the item, such as::
512
513        /*!
514         * \brief Brief statement describing the input mode...
515         *
516         * Optional detailed information about the input mode...
517         */
518        int m_input_mode;
519
520     or, it may appear after the item as an inline comment such as::
521
522        int m_input_mode; /*!< \brief Brief statement describing the input mode.... */
523
524
525Brief and detailed comments
526^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
527
5287.36 Regardless of which documentation form is used, a brief description
529**must** be included using the Doxygen command "\\brief" (or "@brief").
530
5317.37 Any detailed description of an item, if included, **must** appear after
532the brief comment and be separated from the brief comment with a line
533containing no documentation text.
534
535     When a detailed comment is provided, or the brief statement requires
536     more than one line, the prologue comment form **should** be used instead
537     of the inline form to make the documentation easier to read.
538
539
540Grouping data members
541^^^^^^^^^^^^^^^^^^^^^^^^^
542
5437.38 If the names of data members are sufficiently clear that their meaning
544and purpose are obvious to other developers (which should be determined in
545a code review), then the members **may** be grouped together and documented
546with a single descriptive comment.
547
548      An example of Doxygen syntax for such a grouping is::
549
550         //@{
551         //!  @name Data member description...
552
553         int m_member1;
554         int m_member2;
555         ...
556         //@}
557
558
559--------------------------------------------------------------------
560Summary of common Doxygen commands
561--------------------------------------------------------------------
562
563This Section provides an overview of commonly used Doxygen commands.
564Please see the `Doxygen guide <http://www.doxygen.nl/manual/>`_ for more details and information about other commands.
565
566Note that to be processed properly, Doxygen commands **must** be preceded with
567either "\\" or "\@" character. For brevity, we use "\\" for all commands
568described here.
569
570   \\brief
571     The "brief" command is used to begin a brief description of
572     a documented item. The brief description ends at the next blank line.
573   \\file
574     The "file" command is used to document a file. Doxygen requires
575     that to document any global item (function, typedef, enum, etc.), the file
576     in which it is defined **must be** documented.
577   \\name
578     The "name" command, followed by a name containing no blank
579     spaces, is used to define a name that can be referred to elsewhere
580     in the documentation (via a link).
581   \\param
582     The "param" command documents a function parameter/argument.
583     It is followed by the parameter name and description. The "\\param"
584     command can be given an optional attribute to indicate usage of the
585     function argument; possible values are "[in]", "[out]", and "[in,out]".
586   \\return
587     The "return" command is used to describe the return value
588     of a function.
589   \\sa
590     The "sa" command (i.e., "see also") is used to refer (and
591     provide a link to) another documented item. It is followed by the target
592     of the reference (e.g., class/struct name, function name, documentation
593     page, etc.).
594   \@{,\@}
595     These two-character sequences begin and end a
596     grouping of documented items. Optionally, the group can be given a name
597     using the "name" command. Groups are useful for providing additional
598     organization in the documentation, and also when several items can be
599     documented with a single description (e.g., a set of simple, related
600     functions).
601   \\verbatim, \\endverbatim
602     The "verbatim/endverbatim" commands are
603     used to start/stop a block of text that is to appear exactly as it is
604     typed, without additional formatting, in the generated documentation.
605   -, -#
606     The "-" and "-#" symbols begin an item in a bulleted
607     list or numbered list, respectively. In either case, the item ends at
608     the next blank line or next item.
609   \\b, \\e
610     These symbols are used to make the next word bold or
611     emphasized/italicized, respectively, in the generated documentation.
612
613
614