coding-style.txt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. Dpkg C coding style 2009-09-29
  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. Those are checked at build time, and it will abort in case a needed extension
  11. is not supported.
  12. General
  13. ~~~~~~~
  14. Most of the Linux CodingStyle applies.
  15. The code has a mix of an old coding style being phased out and the new
  16. style. New files should use the new style, changes to files with the old
  17. style should switch the code being touched except for the indentation level,
  18. which should be preserved to match (2 spaces).
  19. Code should generally strive for clarity. Monster functions should be split
  20. into logical and small pieces.
  21. Variable and function names should be generally descriptive, not needed
  22. for variables commonly used (for example and index inside a loop, etc),
  23. acronyms should only be used if they are widely known externally or
  24. inside the project. The names should separate logical concepts within
  25. with underscores.
  26. On comments use UTF-8 characters for quotes, copyrigth symbols, etc.
  27. On strings in code use simple or double quotes «''» «""». Not the unpaired
  28. ones «`'». Strings marked for translation, should only be fixed if there's
  29. other changes to be done on them, oterwise we get unneeded fuzzies.
  30. <http://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html>
  31. Indentation, alignment and spacing
  32. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. Lines should be 80 chars max. Indentation is done with hard tabs (which
  34. should be considered to take 8 spaces width). Aligning with spaces:
  35. static void
  36. function(void *ptr, int value)
  37. {
  38. void *ref_ptr = get_ref(ptr);
  39. int ref_value = get_value(ref);
  40. if (value > 10)
  41. do_something(GLOBAL_MACRO, ptr, value, "some-string",
  42. ref_ptr, ref_value, "other-string",
  43. "extra-string");
  44. }
  45. When wrapping, logical operators should be kept on the preceeding line:
  46. if (really_long_variable_to_be_checked_against_a &&
  47. really_long_variable_to_be_checked_against_b)
  48. foo();
  49. Spaces between operators:
  50. if (a && (b || c) && c == d)
  51. break;
  52. a = (b + 4 * (5 - 6)) & 0xff;
  53. Spaces between asignments:
  54. a += b;
  55. Spaces after comma:
  56. foo(a, b);
  57. Space after keywords (for, while, do, if, etc, but sizeof should be
  58. treated like a function):
  59. for (i = 0; i < 10; i++)
  60. foo(i);
  61. memcpy(dst, src, sizeof(src));
  62. Definition of local variables, related code blocks, functions bodies
  63. should be split with blank lines:
  64. int
  65. function(void)
  66. {
  67. int a;
  68. foo();
  69. bar();
  70. quux();
  71. return 0;
  72. }
  73. Braces
  74. ~~~~~~
  75. Braces should be placed on the same line as the keyword, but on a new line
  76. for the function body. No braces should be used for unambiguous one line
  77. statements:
  78. if (a > 0) {
  79. foo(a);
  80. bar(a);
  81. } else {
  82. quux(0)
  83. bar(0);
  84. }
  85. for (;;) {
  86. foo();
  87. bar();
  88. }
  89. do {
  90. foo();
  91. bar();
  92. } while (quux());
  93. switch (foo) {
  94. case a:
  95. bar();
  96. break;
  97. case b:
  98. default:
  99. baz();
  100. break;
  101. }
  102. Code conventions
  103. ~~~~~~~~~~~~~~~~
  104. Prefer assigning outside of conditionals:
  105. n = read_items();
  106. if (n < 100)
  107. foo();
  108. String comparisons should use comparison operators to make it easier to
  109. see what operation is being done:
  110. if (strcmp(a, b) == 0)
  111. foo();
  112. if (strcmp(a, b) < 0)
  113. foo();