f8bdbc56124fef96ecbee49b619b09c58b78d42b
[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 * C++ source files have a ``.cpp`` extension, C source files ``.c``, and
16   headers for both use ``.h``.
17 * For source file :file:`{file}.c`/:file:`{file}.cpp`, declarations that are
18   visible outside the source file should go into a correspondingly named
19   header: :file:`{file}.h`.  Some code may deviate from this rule to improve
20   readability and/or usability of the API, but this should then be clearly
21   documented.
22
23   There can also be a :file:`{file}-impl.h` file that declares classes or
24   functions that are not accessible outside the module.  If the whole file only
25   declares symbols internal to the module, then the :file:`-impl.h` suffix is
26   omitted.
27
28   In most cases, declarations that are not used outside a single source file
29   are in the source file.
30 * Use suffix :file:`-doc.h` for files that contain only Doxygen documentation
31   for some module or such, for cases where there is no natural single header
32   for putting the documentation.
33 * For C++ files, prefer naming the file the same as the (main) class it
34   contains.  Currently all file names are all-lowercase, even though class
35   names contain capital letters.
36   It is OK to use commonly known abbreviations, and/or omit the name of the
37   containing directory if that would cause unnecessary repetition (e.g., as a
38   common prefix to every file name in the directory) and the remaining part of
39   the name is unique enough.
40 * Avoid having multiple files with the same name in different places within
41   the same library.  In addition to making things harder to find, C++ source
42   files with the same name can cause obscure problems with some compilers.
43   Currently, unit tests are an exception to the rule (there is only one
44   particular compiler that had problems with this, and a workaround is
45   possible if/when that starts to affect more than a few of the test files).
46
47 .. TODO: Consider usage of underscores vs dashes in file names.
48
49 Common guidelines for C and C++ code
50 ------------------------------------
51
52 * Preprocessor macros should be all upper-case.  Do not use leading
53   underscores, as all such names are reserved according to the C/C++ standard.
54 * Name include guards like ``GMX_DIRNAME_HEADERNAME_H``.
55 * Boolean variables are always named with a ``b`` prefix, followed by a
56   CamelCase name.
57 * Enum values are named with an ``e`` prefix.  For enum types exposed widely in
58   the codebase, this is followed typically by a part that makes the enum
59   values not conflict with other enums in the same scope.  In C code, this is
60   typically an all-lowercase acronym (e.g., ``epbcNONE``); in C++, the same
61   approach may be used, or the name of the enum type is used (e.g.,
62   ``eHelpOutputFormat_Console``).
63 * Avoid abbreviations that are not obvious to a general reader.
64 * If you use acronyms (e.g., PME, DD) in names, follow the Microsoft policy on
65   casing: two letters is uppercase (DD), three or more is lowercase (Pme).
66   If the first letter would be lowercase in the context where it is used
67   (e.g., at the beginning of a function name, or anywhere in a C function
68   name), it is clearest to use all-lowercase acronym.
69
70 C code
71 ------
72
73 * All function and variable names are lowercase, with underscores as word
74   separators where needed for clarity.
75 * All functions that are part of the public API should start with ``gmx_``.
76   Preferably, other functions should as well.
77   Some parts of the code use a ``_gmx_`` prefix for internal functions, but
78   strictly speaking, these are reserved names, so, e.g., a trailing underscore
79   would be better.
80
81 C++ code
82 --------
83
84 * Use CamelCase for all names.  Start types (such as classes, structs, and
85   typedefs) with a capital letter, other names (functions, variables) with a
86   lowercase letter.
87   You may use an all-lowercase name with underscores if your class closely
88   resembles an external construct (e.g., a standard library construct) named
89   that way.
90 * C++ interfaces are named with an ``I`` prefix, such as in ICommandLineModule.
91   This keeps interfaces identifiable, without introducing too much clutter
92   (as the interface is typically used quite widely, spelling out
93   ``Interface`` would make many of the names unnecessarily long).
94 * Abstract base classes are typically named with an ``Abstract`` prefix.
95 * Member variables are named with a trailing underscore.
96 * Accessors for a variable ``foo_`` are named ``foo()`` and ``setFoo()``.
97 * Global variables are named with a ``g_`` prefix.
98 * Static class variables are named with a ``s_`` prefix.
99 * Global constants are often named with a ``c_`` prefix.
100 * If the main responsibility of a file is to implement a particular class,
101   then the name of the file should match that class, except for possible
102   abbreviations to avoid repetition in file names (e.g., if all classes within
103   a module start with the module name, omitting or abbreviating the module
104   name is OK).  Currently, all source file names are lowercase, but this
105   casing difference should be the only difference.
106
107 The rationale for the trailing underscore and the global/static prefixes is
108 that it is immediately clear whether a variable referenced in a method is local
109 to the function or has wider scope, improving the readability of the code.
110
111 Unit tests
112 ----------
113
114 * Test fixtures (the first parameter to ``TEST``/``TEST_F``) are named with a
115   ``Test`` suffix.
116 * Classes meant as base classes for test fixtures (or as names to be typedefed
117   to be fixtures) are named with a ``TestBase`` or ``Fixture`` suffix.
118 * The CTest test is named with CamelCase, ending with ``Tests`` (e.g.,
119   ``OptionsUnitTests``).
120 * The test binary is named with the name of the module and a ``-test`` suffix.