5ae72cc0e3b7679dc877021fda95c0ea1e4ec843
[alexxy/gromacs.git] / docs / dev-manual / naming.rst
1 Naming conventions
2 ==================
3
4 The conventions here should be applied to all new code, and with common sense
5 when modifying existing code.  For example, renaming a widely used, existing
6 function to follow these conventions may not be justified unless the whole code
7 is getting a rework.
8
9 Currently, this only documents the present state of the code: no particular
10 attempt has been made to consolidate the naming.
11
12 Files
13 -----
14
15 * Avoid having multiple files with the same name in different places within
16   the same library.  In addition to making things harder to find, C++ source
17   files with the same name can cause obscure problems with some compilers.
18   Currently, unit tests are an exception to the rule (there is only one
19   particular compiler that had problems with this, and a workaround is
20   possible if/when that starts to affect more than a few of the test files).
21 * For C++ files, prefer naming the file the same as the (main) class it
22   contains.  Currently all file names are all-lowercase, even though class
23   names contain capital letters.
24   It is OK to use commonly known abbreviations, and/or omit the name of the
25   containing directory if that would cause unnecessary repetition (e.g., as a
26   common prefix to every file name in the directory) and the remaining part of
27   the name is unique enough.
28
29 .. TODO: Copy/move relevant content from codelayout.rst here, and add
30    details.
31
32 .. TODO: Consider usage of underscores vs dashes in file names.
33
34 Common guidelines for C and C++ code
35 ------------------------------------
36
37 * Preprocessor macros should be all upper-case.  Do not use leading
38   underscores, as all such names are reserved according to the C/C++ standard.
39 * Name include guards like ``GMX_DIRNAME_HEADERNAME_H``.
40 * Boolean variables are always named with a ``b`` prefix, followed by a
41   CamelCase name.
42 * Enum values are named with an ``e`` prefix.  For enum types exposed widely in
43   the codebase, this is followed typically by a part that makes the enum
44   values not conflict with other enums in the same scope.  In C code, this is
45   typically an all-lowercase acronym (e.g., ``epbcNONE``); in C++, the same
46   approach may be used, or the name of the enum type is used (e.g.,
47   ``eHelpOutputFormat_Console``).
48 * Avoid abbreviations that are not obvious to a general reader.
49 * If you use acronyms (e.g., PME, DD) in names, follow the Microsoft policy on
50   casing: two letters is uppercase (DD), three or more is lowercase (Pme).
51   If the first letter would be lowercase in the context where it is used
52   (e.g., at the beginning of a function name, or anywhere in a C function
53   name), it is clearest to use all-lowercase acronym.
54
55 C code
56 ------
57
58 * All function and variable names are lowercase, with underscores as word
59   separators where needed for clarity.
60 * All functions that are part of the public API should start with ``gmx_``.
61   Preferably, other functions should as well.
62   Some parts of the code use a ``_gmx_`` prefix for internal functions, but
63   strictly speaking, these are reserved names, so, e.g., a trailing underscore
64   would be better.
65
66 C++ code
67 --------
68
69 * Use CamelCase for all names.  Start types (such as classes, structs, and
70   typedefs) with a capital letter, other names (functions, variables) with a
71   lowercase letter.
72   You may use an all-lowercase name with underscores if your class closely
73   resembles an external construct (e.g., a standard library construct) named
74   that way.
75 * C++ interfaces are named with a ``Interface`` suffix, and abstract base
76   classes with an ``Abstract`` prefix.
77 * Member variables are named with a trailing underscore.
78 * Accessors for a variable ``foo_`` are named ``foo()`` and ``setFoo()``.
79 * Global variables are named with a ``g_`` prefix.
80 * Static class variables are named with a ``s_`` prefix.
81 * Global constants are often named with a ``c_`` prefix.
82 * If the main responsibility of a file is to implement a particular class,
83   then the name of the file should match that class, except for possible
84   abbreviations to avoid repetition in file names (e.g., if all classes within
85   a module start with the module name, omitting or abbreviating the module
86   name is OK).  Currently, all source file names are lowercase, but this
87   casing difference should be the only difference.
88
89 The rationale for the trailing underscore and the global/static prefixes is
90 that it is immediately clear whether a variable referenced in a method is local
91 to the function or has wider scope, improving the readability of the code.
92
93 Unit tests
94 ----------
95
96 * Test fixtures (the first parameter to ``TEST``/``TEST_F``) are named with a
97   ``Test`` suffix.
98 * Classes meant as base classes for test fixtures (or as names to be typedefed
99   to be fixtures) are named with a ``TestBase`` or ``Fixture`` suffix.
100 * The CTest test is named with CamelCase, ending with ``Tests`` (e.g.,
101   ``OptionsUnitTests``).
102 * The test binary is named with the name of the module and a ``-test`` suffix.