coding-style.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. Dpkg C coding style 2012-05-24
  2. ===================
  3. C language extensions
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. The code base assumes C89 plus the following C99 extensions:
  6. * Designated initializers.
  7. * Compound literals.
  8. * Trailing comma in enum.
  9. * Variadic macros.
  10. * Working bool type in <stdbool.h>.
  11. * Working %j and %z printf modifiers.
  12. * Magic __func__ variable.
  13. Those are checked at build time, and it will abort in case a needed extension
  14. is not supported.
  15. General
  16. ~~~~~~~
  17. The coding style is a mix of parts from KNF [K] and the Linux CodingStyle [L].
  18. If in doubt or missing from this file please ask, although files using the
  19. tab based indentation can be considered canon.
  20. [K] <https://www.freebsd.org/cgi/man.cgi?query=style>
  21. [L] <https://www.kernel.org/doc/Documentation/CodingStyle>
  22. The code has a mix of an old coding style being phased out and the new
  23. style. New files should use the new style, changes to files with the old
  24. style should switch the code being touched except for the indentation level,
  25. which should be preserved to match (2 spaces).
  26. Code should generally strive for clarity. Monster functions should be split
  27. into logical and small pieces.
  28. Variable and function names should be generally descriptive, not needed
  29. for variables commonly used (for example an index inside a loop, etc),
  30. acronyms should only be used if they are widely known externally or
  31. inside the project. The names should separate logical concepts within
  32. with underscores.
  33. On comments use UTF-8 characters for quotes, copyright symbols, etc.
  34. On strings in code use simple or double quotes «''» «""». Not the unpaired
  35. ones «`'». Strings marked for translation, should only be fixed if there's
  36. other changes to be done on them, otherwise we get unneeded fuzzies.
  37. <https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html>
  38. Code documentation
  39. ~~~~~~~~~~~~~~~~~~
  40. Public declarations should be documented using JavaDoc style comments.
  41. Documentation should always be close to its definition (usually in the .c
  42. or .cc files) and not its declaration, so that when the code changes it's
  43. less likely that they will get out of sync. For data types, macros and
  44. similar where there's only declarations, the documentation will usually
  45. go instead in the header files.
  46. For enum values and struct members, the documentation should usually be
  47. one-line comments, preceding the item.
  48. The comment title should be on the second line on its own, and the long
  49. description on its own paragraph.
  50. Examples:
  51. /**
  52. * This is the enum title.
  53. */
  54. enum value_list {
  55. /** Value doing foo. */
  56. value_a,
  57. /** Value doing bar. */
  58. value_b,
  59. };
  60. /**
  61. * This is the title.
  62. *
  63. * This is the long description extending several lines, explaining in
  64. * detail what this item does.
  65. *
  66. * @param a This is the a parameter.
  67. * @param b This is the b parameter.
  68. *
  69. * @return This is the return value.
  70. */
  71. Indentation, alignment and spacing
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. Lines should be 80 chars max. Indentation is done with hard tabs (which
  74. should be considered to take 8 spaces width). Aligning with spaces:
  75. static void
  76. function(void *ptr, int value)
  77. {
  78. void *ref_ptr = get_ref(ptr);
  79. int ref_value = get_value(ref);
  80. if (value > 10)
  81. do_something(GLOBAL_MACRO, ptr, value, "some-string",
  82. ref_ptr, ref_value, "other-string",
  83. "extra-string");
  84. }
  85. When wrapping, logical operators should be kept on the preceding line:
  86. if (really_long_variable_to_be_checked_against_a &&
  87. really_long_variable_to_be_checked_against_b)
  88. foo();
  89. Spaces between operators:
  90. if (a && (b || c) && c == d)
  91. break;
  92. a = (b + 4 * (5 - 6)) & 0xff;
  93. Spaces between assignments:
  94. a += b;
  95. Spaces after comma:
  96. foo(a, b);
  97. Space after keywords (for, while, do, if, etc, but sizeof should be
  98. treated like a function):
  99. for (i = 0; i < 10; i++)
  100. foo(i);
  101. memcpy(dst, src, sizeof(src));
  102. Definition of local variables, related code blocks, functions bodies
  103. should be split with blank lines:
  104. int
  105. function(void)
  106. {
  107. int a;
  108. foo();
  109. bar();
  110. quux();
  111. return 0;
  112. }
  113. Braces
  114. ~~~~~~
  115. Braces should be placed on the same line as the keyword, but on a new line
  116. for the function body. No braces should be used for unambiguous one line
  117. statements:
  118. if (a > 0) {
  119. foo(a);
  120. bar(a);
  121. } else {
  122. quux(0)
  123. bar(0);
  124. }
  125. for (;;) {
  126. foo();
  127. bar();
  128. }
  129. do {
  130. foo();
  131. bar();
  132. } while (quux());
  133. switch (foo) {
  134. case a:
  135. bar();
  136. break;
  137. case b:
  138. default:
  139. baz();
  140. break;
  141. }
  142. Code conventions
  143. ~~~~~~~~~~~~~~~~
  144. Prefer assigning outside of conditionals:
  145. n = read_items();
  146. if (n < 100)
  147. foo();
  148. String comparisons should use comparison operators to make it easier to
  149. see what operation is being done:
  150. if (strcmp(a, b) == 0)
  151. foo();
  152. if (strcmp(a, b) < 0)
  153. foo();
  154. Dpkg Perl coding style 2013-01-04
  155. ======================
  156. General
  157. ~~~~~~~
  158. In general you should follow the conventions listed in perlstyle(1).
  159. All the code should run with the “use strict” and “use warnings” pragmas.
  160. Code documentation
  161. ~~~~~~~~~~~~~~~~~~
  162. Public modules should be documented with POD (see perlpod(1)). Private
  163. code doesn't have to use POD, simple comment lines (starting with "#") are
  164. enough. Public scripts are documented in their corresponding manual pages.
  165. Indentation, alignment and spacing
  166. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  167. Lines should be 80 chars max. The indentation level is 4 characters, and
  168. indentation is done with hard tabs (which should be considered to take 8
  169. spaces width) and spaces.
  170. if ($foo) {
  171. if ($bar) {
  172. print "Hello\n";
  173. unless ($baz) {
  174. print "Who are you?\n";
  175. }
  176. }
  177. }
  178. Perl version
  179. ~~~~~~~~~~~~
  180. We don't want to impose a too-recent Perl version, so only use features
  181. supported by the Perl version that is currently in Debian oldstable when
  182. possible. Currently that means Perl 5.10.0.
  183. Object methods
  184. ~~~~~~~~~~~~~~
  185. Use a single line to retrieve all the arguments and use $self as name
  186. for the current object:
  187. sub do_something {
  188. my ($self, $arg1, $arg2, %opts) = @_;
  189. ...
  190. }
  191. Supplementary optional arguments should be named and thus stored in a
  192. hash.