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
9 Currently, this only documents the present state of the code: no particular
10 attempt has been made to consolidate the naming.
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
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
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).
47 Common guidelines for C and C++ code
48 ------------------------------------
50 * Preprocessor macros should be all upper-case. Do not use leading
51 underscores, as all such names are reserved according to the C/C++ standard.
52 * Name include guards like ``GMX_DIRNAME_HEADERNAME_H``.
53 * Avoid abbreviations that are not obvious to a general reader.
54 * If you use acronyms (e.g., PME, DD) in names, follow the Microsoft policy on
55 casing: two letters is uppercase (DD), three or more is lowercase (Pme).
56 If the first letter would be lowercase in the context where it is used
57 (e.g., at the beginning of a function name, or anywhere in a C function
58 name), it is clearest to use all-lowercase acronym.
63 * All function and variable names are lowercase, with underscores as word
64 separators where needed for clarity.
65 * All functions that are part of the public API should start with ``gmx_``.
66 Preferably, other functions should as well.
67 Some parts of the code use a ``_gmx_`` prefix for internal functions, but
68 strictly speaking, these are reserved names, so, e.g., a trailing underscore
70 * Old C code and changes to it can still use the hungarian notation for
71 booleans and enumerated variable names, as well as enum values, where they
72 are prefixed with ``b`` and ``e`` respectively, or you can gradually move
73 to the C++ practice below. Whatever you choose, avoid complex abbreviations.
78 * Use CamelCase for all names. Start types (such as classes, structs,
79 typedefs and enum values) with a capital letter, other names (functions,
80 variables) with a lowercase letter.
81 You may use an all-lowercase name with underscores if your class closely
82 resembles an external construct (e.g., a standard library construct) named
84 * C++ interfaces are named with an ``I`` prefix, such as in ICommandLineModule.
85 This keeps interfaces identifiable, without introducing too much clutter
86 (as the interface is typically used quite widely, spelling out
87 ``Interface`` would make many of the names unnecessarily long).
88 * Abstract base classes are typically named with an ``Abstract`` prefix.
89 * Member variables are named with a trailing underscore.
90 * Accessors for a variable ``foo_`` are named ``foo()`` and ``setFoo()``.
91 * Global variables are named with a ``g_`` prefix.
92 * Static class variables are named with a ``s_`` prefix.
93 * Global constants are often named with a ``c_`` prefix.
94 * If the main responsibility of a file is to implement a particular class,
95 then the name of the file should match that class, except for possible
96 abbreviations to avoid repetition in file names (e.g., if all classes within
97 a module start with the module name, omitting or abbreviating the module
98 name is OK). Currently, all source file names are lowercase, but this
99 casing difference should be the only difference.
100 * For new C++ code, avoid using the hungarian notation that is a descendant
101 from the C code (i.e., the practice of using a ``b`` prefix for boolean
102 variables and an ``e`` prefix for enumerated variables and/or values).
103 Instead, make the names long with a good description of what they control,
104 typically including a verb for boolean variables, like ``foundAtom``.
105 * Prefer class enums over regular ones, so that unexpected conversions to
107 * When using a non-class enum, prefer to include the name of the enumeration type
108 as a base in the name of enum values, e.g., ``HelpOutputFormat_Console``,
109 in particular for settings exposed to other modules.
110 * Prefer to use enumerated types and values instead of booleans as control
111 parameters to functions. It is reasonably easy to understand what the
112 argument ``HelpOutputFormat_Console`` is controlling, while it is almost
113 impossible to decipher ``TRUE`` in the same place without checking the
114 documentation for the role of the parameter.
116 The rationale for the trailing underscore and the global/static prefixes is
117 that it is immediately clear whether a variable referenced in a method is local
118 to the function or has wider scope, improving the readability of the code.
123 Rationale: on GPUs, using the right memory space is often performance critical.
125 * In CUDA device code ``sm_``, ``gm_``, and ``cm_`` prefixes are used for
126 shared, global and constant memory. The absence of a prefix indicates
127 register space. Same prefixes are used in OpenCL code, where ``sm_``
128 indicates local memory and no prefixes are added to variables in private
130 * Data transferred to and from host has to live in both CPU and GPU memory
131 spaces. Therefore it is typical to have a pointer or container (in CUDA), or
132 memory buffer (in OpenCL) in host memory that has a device-based counterpart.
133 To easily distinguish these, the variables names for such objects are
134 prefixed ``h_`` and ``d_`` and have identical names otherwise. Example:
135 ``h_masses``, and ``d_masses``.
136 * In all other cases, pointers to host memory are not required to have the
137 prefix ``h_`` (even in parts of the host code, where both host and device
138 pointers are present). The device pointers should always have the prefix
140 * In case GPU kernel arguments are combined into a structure, it is preferred
141 that all device memory pointers within the structure have the prefix ``d_``
142 (i.e. ``kernelArgs.d_data`` is preferred to ``d_kernelArgs.data``,
143 whereas both ``d_kernelArgs.d_data`` and ``kernelArgs.data`` are not
145 * Note that the same pointer can have the prefix ``d_`` in the host code,
146 and ``gm_`` in the device code. For example, if ``d_data`` is passed to
147 the kernel as an argument, it should be aliased to ``gm_data`` in the
148 kernel arguments list. In case a device pointer is a field of a passed
149 structure, it can be used directly or aliased to a pointer with ``gm_``
150 prefix (i.e. ``kernelArgs.d_data`` can be used as is or aliased to
151 ``gm_data`` inside the kernel).
152 * Avoid using uninformative names for CUDA warp, thread, block indexes and
153 their OpenCL analogs (i.e. ``threadIndex`` is preferred to ``i`` or
159 * Test fixtures (the first parameter to ``TEST``/``TEST_F``) are named with a
161 * Classes meant as base classes for test fixtures (or as names to be typedefed
162 to be fixtures) are named with a ``TestBase`` or ``Fixture`` suffix.
163 * The CTest test is named with CamelCase, ending with ``Tests`` (e.g.,
164 ``OptionsUnitTests``).
165 * The test binary is named with the name of the module and a ``-test`` suffix.