coding-style.txt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. Dpkg troff coding style 2016-01-29
  2. =======================
  3. General
  4. ~~~~~~~
  5. Dashes that are relevant when copy & pasted need to be escaped (e.g. those
  6. present in program, file, argument and field names).
  7. New sentences inside a paragraph should start on a new line, so that we
  8. do not need to reflow the text when adding new content.
  9. Every new feature, option or behavior change needs to be documented with
  10. the version introducing the change.
  11. Dpkg M4sh/Autoconf coding style 1016-09-05
  12. ===============================
  13. General
  14. ~~~~~~~
  15. All dpkg specific macros need to be prefixed with «DPKG_». Complex checks
  16. should be defined as new macros under m4/dpkg-<name>.m4, and those used
  17. in configure.ac which should look pretty simple.
  18. Quoting and indentation
  19. ~~~~~~~~~~~~~~~~~~~~~~~
  20. Code and arguments that wrap into the next line are indented by two spaces.
  21. In principle all macro argument should always be quoted. Which gives brings
  22. one of the biggest readability issues with M4sh/Autoconf code, the amount of
  23. consecutive quotes and parenthesis pairs, which can make it very hard to
  24. notice if they are unbalanced. To avoid this we use a style that tries to
  25. avoid more than two consecutive blocks of «])».
  26. We can either use a style resembling very simple function calls, when the
  27. arguments are as well very simple, such as:
  28. AC_DEFINE_UNQUOTED([SOME_VARIABLE],
  29. [SOME_CONCOCTED_WAY_TO_GET_A_VALUE],
  30. [Some descriptive text here])
  31. AS_CASE([condition],
  32. [case-a], [do-a],
  33. [case-b], [do-b])
  34. Or one resembling curly-braced C-style blocks, such as this:
  35. AS_IF([condition], [
  36. DO_SOMETHING([here])
  37. ], [
  38. DO_OTHERWISE([there])
  39. ])
  40. Except for AC_ARG_WITH, AC_ARG_ENABLE and AM_CONDITIONAL which need their
  41. second argument quoted tightly surrounding the code, like this:
  42. AC_ARG_ENABLE([feature],
  43. [AS_HELP_STRING([--disable-feature], [Disable feature])],
  44. [], [enable_feature="yes"])
  45. AM_CONDITIONAL([HAVE_SOME_FEATURE],
  46. [test "x$ac_cv_have_some_feature" = "xyes" && \
  47. test "x$ac_cv_have_some_feature" = "xyes"])
  48. or the output will get messed up.
  49. Dpkg C/C++ coding style 2016-01-29
  50. =======================
  51. C language extensions
  52. ~~~~~~~~~~~~~~~~~~~~~
  53. The code base assumes C89 plus the following C99 extensions:
  54. * Designated initializers.
  55. * Compound literals.
  56. * Trailing comma in enum.
  57. * Variadic macros.
  58. * Working bool type in <stdbool.h>.
  59. * Working %j and %z printf modifiers.
  60. * Magic __func__ variable.
  61. Those are checked at build time, and it will abort in case a needed extension
  62. is not supported.
  63. C++ language extensions
  64. ~~~~~~~~~~~~~~~~~~~~~~~
  65. The code base assumes C++03 plus the following C++11 extension:
  66. * Null pointer keyword (nullptr).
  67. This is checked at build time, and it will use compatibility code in case the
  68. needed extension is not supported.
  69. General
  70. ~~~~~~~
  71. The coding style is a mix of parts from KNF [K] and the Linux CodingStyle [L].
  72. If in doubt or missing from this file please ask, although files using the
  73. tab based indentation can be considered canon.
  74. [K] <https://www.freebsd.org/cgi/man.cgi?query=style>
  75. [L] <https://www.kernel.org/doc/Documentation/CodingStyle>
  76. The code has a mix of an old coding style being phased out and the new
  77. style. New files should use the new style, changes to files with the old
  78. style should switch the code being touched except for the indentation level,
  79. which should be preserved to match (2 spaces).
  80. Code should generally strive for clarity. Monster functions should be split
  81. into logical and small pieces.
  82. Variable and function names should be generally descriptive, not needed
  83. for variables commonly used (for example an index inside a loop, etc),
  84. acronyms should only be used if they are widely known externally or
  85. inside the project. The names should separate logical concepts within
  86. with underscores.
  87. On comments use UTF-8 characters for quotes, copyright symbols, etc.
  88. On strings in code use simple or double quotes «''» «""». Not the unpaired
  89. ones «`'». Strings marked for translation, should only be fixed if there's
  90. other changes to be done on them, otherwise we get unneeded fuzzies.
  91. <https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html>
  92. Code documentation
  93. ~~~~~~~~~~~~~~~~~~
  94. Public declarations should be documented using JavaDoc style comments.
  95. Documentation should always be close to its definition (usually in the .c
  96. or .cc files) and not its declaration, so that when the code changes it's
  97. less likely that they will get out of sync. For data types, macros and
  98. similar where there's only declarations, the documentation will usually
  99. go instead in the header files.
  100. For enum values and struct members, the documentation should usually be
  101. one-line comments, preceding the item.
  102. The comment title should be on the second line on its own, and the long
  103. description on its own paragraph.
  104. Examples:
  105. /**
  106. * This is the enum title.
  107. */
  108. enum value_list {
  109. /** Value doing foo. */
  110. VALUE_A,
  111. /** Value doing bar. */
  112. VALUE_B,
  113. };
  114. /**
  115. * This is the title.
  116. *
  117. * This is the long description extending several lines, explaining in
  118. * detail what this item does.
  119. *
  120. * @param a This is the a parameter.
  121. * @param b This is the b parameter.
  122. *
  123. * @return This is the return value.
  124. */
  125. Indentation, alignment and spacing
  126. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127. Lines should be 80 chars max. Indentation is done with hard tabs (which
  128. should be considered to take 8 spaces width). Aligning with spaces:
  129. static void
  130. function(void *ptr, int value)
  131. {
  132. void *ref_ptr = get_ref(ptr);
  133. int ref_value = get_value(ref);
  134. if (value > 10)
  135. do_something(GLOBAL_MACRO, ptr, value, "some-string",
  136. ref_ptr, ref_value, "other-string",
  137. "extra-string");
  138. }
  139. When wrapping, logical operators should be kept on the preceding line:
  140. if (really_long_variable_to_be_checked_against_a &&
  141. really_long_variable_to_be_checked_against_b)
  142. foo();
  143. Spaces between operators:
  144. if (a && (b || c) && c == d)
  145. break;
  146. a = (b + 4 * (5 - 6)) & 0xff;
  147. Spaces between assignments:
  148. a += b;
  149. Spaces after comma:
  150. foo(a, b);
  151. Space after keywords (for, while, do, if, etc, but sizeof should be
  152. treated like a function):
  153. for (i = 0; i < 10; i++)
  154. foo(i);
  155. memcpy(dst, src, sizeof(src));
  156. Definition of local variables, related code blocks, functions bodies
  157. should be split with blank lines:
  158. int
  159. function(void)
  160. {
  161. int a;
  162. foo();
  163. bar();
  164. quux();
  165. return 0;
  166. }
  167. Braces
  168. ~~~~~~
  169. Braces should be placed on the same line as the keyword, but on a new line
  170. for the function body. No braces should be used for unambiguous one line
  171. statements:
  172. if (a > 0) {
  173. foo(a);
  174. bar(a);
  175. } else {
  176. quux(0)
  177. bar(0);
  178. }
  179. for (;;) {
  180. foo();
  181. bar();
  182. }
  183. do {
  184. foo();
  185. bar();
  186. } while (quux());
  187. switch (foo) {
  188. case a:
  189. bar();
  190. break;
  191. case b:
  192. default:
  193. baz();
  194. break;
  195. }
  196. Code conventions
  197. ~~~~~~~~~~~~~~~~
  198. Prefer assigning outside of conditionals:
  199. n = read_items();
  200. if (n < 100)
  201. foo();
  202. String comparisons should use comparison operators to make it easier to
  203. see what operation is being done:
  204. if (strcmp(a, b) == 0)
  205. foo();
  206. if (strcmp(a, b) < 0)
  207. foo();
  208. Dpkg Perl coding style 2016-01-29
  209. ======================
  210. General
  211. ~~~~~~~
  212. In general you should follow the conventions listed in perlstyle(1).
  213. All the code should run with the “use strict” and “use warnings” pragmas,
  214. and pass «DPKG_DEVEL_MODE=1 make check».
  215. Code documentation
  216. ~~~~~~~~~~~~~~~~~~
  217. Public modules should be documented with POD (see perlpod(1)). Private
  218. code doesn't have to use POD, simple comment lines (starting with "#") are
  219. enough, but if they use POD they need to note the fact that the module is
  220. private in the CHANGES section and specify a version «0.xx». Public scripts
  221. are documented in their corresponding manual pages.
  222. Indentation, alignment and spacing
  223. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  224. Lines should be 80 chars max. The indentation level is 4 characters, and
  225. indentation is done with soft tabs (no hard tabs) and spaces.
  226. if ($foo) {
  227. if ($bar) {
  228. print "Hello\n";
  229. unless ($baz) {
  230. print "Who are you?\n";
  231. }
  232. }
  233. }
  234. Perl version
  235. ~~~~~~~~~~~~
  236. We don't want to impose a too-recent Perl version, so only use features
  237. supported by the Perl version that is currently in Debian oldstable when
  238. possible. Currently that means Perl 5.14.2.
  239. Object methods
  240. ~~~~~~~~~~~~~~
  241. Use a single line to retrieve all the arguments and use $self as name
  242. for the current object:
  243. sub do_something {
  244. my ($self, $arg1, $arg2, %opts) = @_;
  245. ...
  246. }
  247. Supplementary optional arguments should be named and thus stored in a
  248. hash.