Convert developer manual to rst
[alexxy/gromacs.git] / docs / dev-manual / formatting.rst
1 Guidelines for code formatting
2 ==============================
3
4 The following list provides the general formatting/indentation rules for
5 |Gromacs| code (C/C++):
6
7 * Basic indentation is four spaces.
8 * Keep lines at a reasonable length.  There is no hard limit, but use 80
9   characters as a guideline.  If you end up indenting very deeply,
10   consider splitting the code into functions.
11 * Do not use tabs, only spaces.  Most editors can be configured to generate
12   spaces even when pressing tab.  Tabs (in particular when mixed with spaces)
13   easily break indentation in contexts where settings are not exactly equal
14   (e.g., in ``git diff`` output).
15 * No trailing whitespace.
16 * Use braces always for delimiting blocks, even when there is only a single
17   statement in an ``if`` block or similar.
18 * Put braces on their own lines.  The only exception is short one-line inline
19   functions in C++ classes, which can be put on a single line.
20 * Use spaces liberally.
21 * ``extern "C"`` and ``namespace`` blocks are not indented, but all others
22   (including ``class`` and ``switch`` bodies) are.
23
24 Additionally:
25
26 * All source files and other non-trivial scripts should contain a copyright
27   header with a predetermined format and license information (check existing
28   files).  Copyright holder should be "the GROMACS development team" for the
29   years where the code has been in the |Gromacs| source repository, but earlier
30   years can hold other copyrights.
31 * Whenever you update a file, you should check that the current year is listed
32   as a copyright year.
33
34 Most of the above guidelines are enforced using uncrustify, an automatic source
35 code formatting tool.  The copyright guidelines are enforced by a separate
36 Python script.  See :doc:`uncrustify` for details.  Note that due to the
37 nature of uncrustify (it only does all-or-nothing formatting), it enforces
38 several additional formatting rules in addition to those above.
39
40 Enforcing a consistent formatting has a few advantages:
41
42 * No one needs to manually review code for most of these formatting issues,
43   and people can focus on content.
44 * A separate automatic script (see below) can be applied to re-establish the
45   formatting after refactoring like renaming symbols or changing some
46   parameters, without needing to manually do it all.