coding-style.txt 5.9 KB

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