po: Update German man pages translation
[dpkg.git] / doc / coding-style.txt
blob536e5ca3c0e2add7ab6b9dd48f16e5aa553e5ae2
1 Dpkg POD coding style 2021-06-25
2 =====================
4 General
5 ~~~~~~~
7 Verbatim code sections that need to be formatted, need to be prefixed with
8 a line containing exactly «Z<>», to trick the parser.
10 New sentences inside a paragraph should start on a new line, so that we
11 do not need to reflow the text when adding new content.
13 Every new feature, option or behavior change needs to be documented with
14 the version introducing the change.
17 Dpkg M4sh/Autoconf coding style 2016-09-05
18 ===============================
20 General
21 ~~~~~~~
23 All dpkg specific macros need to be prefixed with «DPKG_». Complex checks
24 should be defined as new macros under m4/dpkg-<name>.m4, and those used
25 in configure.ac which should look pretty simple.
27 Quoting and indentation
28 ~~~~~~~~~~~~~~~~~~~~~~~
30 Code and arguments that wrap into the next line are indented by two spaces.
32 In principle all macro argument should always be quoted. Which brings one
33 of the biggest readability issues with M4sh/Autoconf code, the amount of
34 consecutive quotes and parenthesis pairs, which can make it very hard to
35 notice if they are unbalanced. To avoid this we use a style that tries to
36 avoid more than two consecutive blocks of «])».
38 We can either use a style resembling very simple function calls, when the
39 arguments are as well very simple, such as:
41   AC_DEFINE_UNQUOTED([SOME_VARIABLE],
42     [SOME_CONCOCTED_WAY_TO_GET_A_VALUE],
43     [Some descriptive text here])
45   AS_CASE([condition],
46     [case-a], [do-a],
47     [case-b], [do-b])
49 Or one resembling curly-braced C-style blocks, such as this:
51   AS_IF([condition], [
52     DO_SOMETHING([here])
53   ], [
54     DO_OTHERWISE([there])
55   ])
57 Except for AC_ARG_WITH, AC_ARG_ENABLE and AM_CONDITIONAL which need their
58 second argument quoted tightly surrounding the code, like this:
60   AC_ARG_ENABLE([feature],
61     [AS_HELP_STRING([--disable-feature], [Disable feature])],
62     [], [enable_feature="yes"])
64   AM_CONDITIONAL([HAVE_SOME_FEATURE],
65     [test "x$ac_cv_have_some_feature" = "xyes" && \
66      test "x$ac_cv_have_some_feature" = "xyes"])
68 or the output will get messed up.
71 Dpkg C/C++ coding style 2016-01-29
72 =======================
74 Standards
75 ~~~~~~~~~
77 The C code base assumes C99, except for the following features:
79  - Variable length arrays.
80  - Mixed declaration and code.
82 The C++ code base assumes C++03, plus the following C++11 extension:
84  + Null pointer keyword (nullptr).
86 The code base assumes a POSIX.1-2008 compatible environment.
88 The required features are checked at build time, and it will either use
89 compatibility code in case the needed extensions are not supported and it
90 is possible to support them, otherwise it will abort in case a needed one
91 cannot be used.
93 General
94 ~~~~~~~
96 The coding style is a mix of parts from KNF [K] and the Linux CodingStyle [L].
97 If in doubt or missing from this file please ask, although files using the
98 tab based indentation can be considered canon.
100   [K] <https://www.freebsd.org/cgi/man.cgi?query=style>
101   [L] <https://www.kernel.org/doc/Documentation/CodingStyle>
103 The code has a mix of an old coding style being phased out and the new
104 style. New files should use the new style, changes to files with the old
105 style should switch the code being touched except for the indentation level,
106 which should be preserved to match (2 spaces).
108 Code should generally strive for clarity. Monster functions should be split
109 into logical and small pieces.
111 Variable and function names should be generally descriptive, not needed
112 for variables commonly used (for example an index inside a loop, etc),
113 acronyms should only be used if they are widely known externally or
114 inside the project. The names should separate logical concepts within
115 with underscores.
117 On comments use UTF-8 characters for quotes, copyright symbols, etc.
119 On strings in code use simple or double quotes «''» «""». Not the unpaired
120 ones «`'». Strings marked for translation, should only be fixed if there's
121 other changes to be done on them, otherwise we get unneeded fuzzies.
123   <https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html>
125 Code documentation
126 ~~~~~~~~~~~~~~~~~~
128 Public declarations should be documented using JavaDoc style comments.
130 Documentation should always be close to its definition (usually in the .c
131 or .cc files) and not its declaration, so that when the code changes it's
132 less likely that they will get out of sync. For data types, macros and
133 similar where there's only declarations, the documentation will usually
134 go instead in the header files.
136 For enum values and struct members, the documentation should usually be
137 one-line comments, preceding the item.
139 The comment title should be on the second line on its own, and the long
140 description on its own paragraph.
142 Examples:
145  * This is the enum title.
146  */
147 enum value_list {
148         /** Value doing foo. */
149         VALUE_A,
150         /** Value doing bar. */
151         VALUE_B,
155  * This is the title.
157  * This is the long description extending several lines, explaining in
158  * detail what this item does.
160  * @param a This is the a parameter.
161  * @param b This is the b parameter.
163  * @return This is the return value.
164  */
166 Indentation, alignment and spacing
167 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169 Lines should be 80 chars max. Indentation is done with hard tabs (which
170 should be considered to take 8 spaces width). Aligning with spaces:
172 static void
173 function(void *ptr, int value)
175         void *ref_ptr = get_ref(ptr);
176         int ref_value = get_value(ref);
178         if (value > 10)
179                 do_something(GLOBAL_MACRO, ptr, value, "some-string",
180                              ref_ptr, ref_value, "other-string",
181                              "extra-string");
184 When wrapping, logical operators should be kept on the preceding line:
186         if (really_long_variable_to_be_checked_against_a &&
187             really_long_variable_to_be_checked_against_b)
188                 foo();
190 Spaces around operators:
192         if (a && (b || c) && c == d)
193                 break;
195         a = (b + 4 * (5 - 6)) & 0xff;
197 Spaces around assignments:
199         a += b;
201 Spaces after comma:
203         foo(a, b);
205 Space after keywords (for, while, do, if, etc, but sizeof should be
206 treated like a function):
208         for (i = 0; i < 10; i++)
209                 foo(i);
211         memcpy(dst, src, sizeof(src));
213 Definition of local variables, related code blocks, functions bodies
214 should be split with blank lines:
217 function(void)
219         int a;
221         foo();
222         bar();
224         quux();
226         return 0;
229 Braces
230 ~~~~~~
232 Braces should be placed on the same line as the keyword, but on a new line
233 for the function body. No braces should be used for unambiguous one line
234 statements:
236         if (a > 0) {
237                 foo(a);
238                 bar(a);
239         } else {
240                 quux(0)
241                 bar(0);
242         }
244         for (;;) {
245                 foo();
246                 bar();
247         }
249         do {
250                 foo();
251                 bar();
252         } while (quux());
254         switch (foo) {
255         case a:
256                 bar();
257                 break;
258         case b:
259         default:
260                 baz();
261                 break;
262         }
264 Code conventions
265 ~~~~~~~~~~~~~~~~
267 Prefer assigning outside of conditionals:
269         n = read_items();
270         if (n < 100)
271                 foo();
273 String comparisons should use comparison operators to make it easier to
274 see what operation is being done:
276         if (strcmp(a, b) == 0)
277                 foo();
279         if (strcmp(a, b) < 0)
280                 foo();
283 Dpkg Perl coding style 2019-03-27
284 ======================
286 Perl version
287 ~~~~~~~~~~~~
289 We don't want to impose a too-recent Perl version, so only use features
290 supported by the Perl version that is currently in Debian oldstable when
291 possible. Currently that means Perl 5.32.1.
293 General
294 ~~~~~~~
296 In general you should follow the conventions listed in perlstyle(1).
297 All the code should run with the “use strict” and “use warnings” pragmas,
298 and pass «make authorcheck».
300 Code documentation
301 ~~~~~~~~~~~~~~~~~~
303 Public modules should be documented with POD (see perlpod(1)). Private
304 code doesn't have to use POD, simple comment lines (starting with "#") are
305 enough, but if they use POD they need to note the fact that the module is
306 private in the CHANGES section and specify a version «0.xx». Public scripts
307 are documented in their corresponding manual pages.
309 Indentation, alignment and spacing
310 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312 Lines should be 80 chars max. The indentation level is 4 characters, and
313 indentation is done with soft tabs (no hard tabs) and spaces.
315 if ($foo) {
316     if ($bar) {
317         print "Hello\n";
318         unless ($baz) {
319             print "Who are you?\n";
320         }
321     }
324 Object methods
325 ~~~~~~~~~~~~~~
327 Use a single line to retrieve all the arguments and use $self as name
328 for the current object:
330 sub do_something {
331     my ($self, $arg1, $arg2, %opts) = @_;
332     ...
335 Supplementary optional arguments should be named and thus stored in a
336 hash.