annotate Sphinx/source/developers/coding-style.rst @ 660:a6e371768a70

dicom-as-json
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Apr 2021 12:41:31 +0200
parents 2922fb1bd65e
children 1316bc62b5d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
25fa874803ab plugins inside book
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
1 .. _coding-style:
0
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 .. highlight:: c++
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 Coding style
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 ============
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 .. contents::
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 The developers of Orthanc should follow these `C++ Programming Style
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 Guidelines <http://geosoft.no/development/cppstyle.html>`__, that are
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 similar to the so-called "`BSD/Allman style
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 <https://en.wikipedia.org/wiki/Indent_style#Allman_style>`__", with
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 some adaptations that are described below. A compliant Eclipse
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 formatter is `available in the Orthanc distribution
449
2922fb1bd65e fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 360
diff changeset
15 <https://hg.orthanc-server.com/orthanc/file/default/OrthancFramework/Resources/Graveyard/EclipseCodingStyle.xml>`__ (not maintained anymore).
0
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 Licensing
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 ---------
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 Do not forget to include licensing information (GPLv3 with OpenSSL
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 exception) in each ``.cpp`` and ``.h``.
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 Tabulations
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 -----------
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 No tab characters. Replace 1 tab by 2 spaces.
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 Strengthened Rules
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 ------------------
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 * Rule 31: Use ``COLOR_RED`` instead of ``Color::RED``
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 * Rule 34: Use the suffix ``.cpp``
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 * Rule 35: A single header file must contain a single public class
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 * Rule 72: Use the Example 2 style (aka. Allman style, used by MSDN
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 and Visual Studio)::
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 while (!done)
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 doSomething();
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 done = moreToDo();
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 Replaced Rules
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 --------------
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 * Rule 6: The names of the methods are camel-case to move the coding style closer to that of the .NET framework.
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 * Rule 36:
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 * One-liners are always ok in a ``.h``,
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 * High-performance code is also allowed but only with the inline
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 keyword (the code being moved at the end of the header)
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 * Rule 40: Use ``#pragma once`` in each header file (cf. `Wikipedia
25
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
55 <https://en.wikipedia.org/wiki/Pragma_once>`__)
0
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 * Rules 73 and 80: Use Visual Studio's default style that does not add
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 two whitespaces in front of public, protected, private and case::
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 class SomeClass : public BaseClass
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 public:
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 ...
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 protected:
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 ...
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 private:
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 ...
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 };
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 Additional Rules
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 ----------------
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 * Use C++ exceptions, avoid error codes.
25
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
74 * Use the `RAII design pattern <https://en.wikipedia.org/wiki/RAII>`__ (Resource Allocation Is Initialization) wherever possible.
0
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 * No C-style casting, use ``static_cast``, ``reinterpret_cast``,
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 ``dynamic_cast`` and ``const_cast``.
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 * Never use ``using namespace`` in header files (except inside inline
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 implementations).
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 * Complement to rule 20: ``Finalize`` is the complementary word to
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 ``Initialize``.
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 * Minimize the number of #include in header files.
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 * Never use ``catch (...)``, except when protecting non-Orthanc code.
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 * To ease unit testing, favor the `JavaBeans
25
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
84 <https://en.wikipedia.org/wiki/Java_beans>`__ conventions:
0
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 * Single constructor without argument,
901e8961f46e initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 * Use getters/setters.
25
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
88
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
89
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
90 Conventions for pointers and references
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
91 ---------------------------------------
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
92
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
93 Except when clearly stated in the documentation:
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
94
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
95 * A function that receives a **reference** to an object as an argument
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
96 is not responsible for the lifecycle of the object, and must not
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
97 ``delete`` it. The object is allowed to keep this reference in a
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
98 member variable. The caller must ensure that the referenced object
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
99 will not be freed between the object that references it.
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
100 * A function that receives a **pointer** to an object as an argument
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
101 takes the ownership of the object.
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
102 * A function that returns a **reference** to an object indicates that
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
103 the object should not be freed by the caller.
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
104 * A function that returns a **pointer** to an object transfers its
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
105 ownership to the caller. Occasionally, a pointer is also returned to
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
106 indicate the presence or the absence (represented by ``NULL``) of
669ea65ba7fb fix links
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
107 some resource: In such a case, the pointer must not be freed.