CODING_STYLE 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. Cairo coding style.
  2. This document is intended to be a short description of the preferred
  3. coding style for the cairo source code. Good style requires good
  4. taste, which means this can't all be reduced to automated rules, and
  5. there are exceptions.
  6. We want the code to be easy to understand and maintain, and consistent
  7. style plays an important part in that, even if some of the specific
  8. details seem trivial. If nothing else, this document gives a place to
  9. put consistent answers for issues that would otherwise be arbitrary.
  10. Most of the guidelines here are demonstrated by examples, (which means
  11. this document is quicker to read than it might appear given its
  12. length). Most of the examples are positive examples that you should
  13. imitate. The few negative examples are clearly marked with a comment
  14. of /* Yuck! */. Please don't submit code to cairo that looks like any
  15. of these.
  16. Indentation
  17. -----------
  18. Each new level is indented 4 more spaces than the previous level:
  19. if (condition)
  20. do_something ();
  21. This may be achieved with space characters or a combination of tab
  22. characters and space characters. It may not be achieved with tab
  23. characters exclusively (see below).
  24. Tab characters
  25. --------------
  26. The tab character must always be interpreted according to its
  27. traditional meaning:
  28. Advance to the next column which is a multiple of 8.
  29. With this definition, even levels of indentation can be achieved with
  30. a sequence of tab characters, while odd levels of indentation may
  31. begin with a sequence of tab character but must end with 4 space
  32. characters.
  33. Some programmers have been misled by certain text editors into
  34. thinking that 4-space indentation can be achieved with tab characters
  35. exclusively by changing the meaning of tab character to be "advance to
  36. the next column which is a multiple of 4". Code formatted in this way,
  37. making an assumption of a fictitious 4-character-tab will not be
  38. accepted into cairo.
  39. The rationale here is that tabs are used in the code for lining things
  40. up other than indentation, (see the Whitespace section below), and
  41. changing the interpretation of tab from its traditional meaning will
  42. break this alignment.
  43. Braces
  44. ------
  45. Most of the code in cairo uses bracing in the style of K&R:
  46. if (condition) {
  47. do_this ();
  48. do_that ();
  49. } else {
  50. do_the_other ();
  51. }
  52. but some of the code uses an alternate style:
  53. if (condition)
  54. {
  55. do_this ();
  56. do_that ();
  57. }
  58. else
  59. {
  60. do_the_other ();
  61. }
  62. and that seems just fine. We won't lay down any strict rule on this
  63. point, (though there should be some local consistency). If you came
  64. here hoping to find some guidance, then use the first form above.
  65. If all of the substatements of an if statement are single statements,
  66. the optional braces should not usually appear:
  67. if (condition)
  68. do_this ();
  69. else
  70. do_that ();
  71. But the braces are mandatory when mixing single statement and compound
  72. statements in the various clauses. For example, do not do this:
  73. if (condition) {
  74. do_this ();
  75. do_that ();
  76. } else /* Yuck! */
  77. do_the_other ();
  78. And of course, there are exceptions for when the code just looks
  79. better with the braces:
  80. if (condition) {
  81. /* Note that we have to be careful here. */
  82. do_something_dangerous (with_care);
  83. }
  84. if (condition &&
  85. other_condition &&
  86. yet_another)
  87. {
  88. do_something ();
  89. }
  90. And note that this last example also shows a situation in which the
  91. opening brace really needs to be on its own line. The following looks awful:
  92. if (condition &&
  93. other_condition &&
  94. yet_another) { /* Yuck! */
  95. do_something ();
  96. }
  97. As we said above, legible code that is easy to understand and maintain
  98. is the goal, not adherence to strict rules.
  99. Whitespace
  100. ----------
  101. Separate logically distinct chunks with a single newline. This
  102. obviously applies between functions, but also applies within a
  103. function or block and can even be used to good effect within a
  104. structure definition:
  105. struct _cairo_gstate {
  106. cairo_operator_t op;
  107. double tolerance;
  108. /* stroke style */
  109. double line_width;
  110. cairo_line_cap_t line_cap;
  111. cairo_line_join_t line_join;
  112. double miter_limit;
  113. cairo_fill_rule_t fill_rule;
  114. double *dash;
  115. int num_dashes;
  116. double dash_offset;
  117. ...
  118. }
  119. Use a single space before a left parenthesis, except where the
  120. standard will not allow it, (eg. when defining a parameterized macro).
  121. Don't eliminate newlines just because things would still fit on one
  122. line. This breaks the expected visual structure of the code making it
  123. much harder to read and understand:
  124. if (condition) foo (); else bar (); /* Yuck! */
  125. Do eliminate trailing whitespace (space or tab characters) on any
  126. line. Also, avoid putting initial or final blank lines into any file,
  127. and never use multiple blank lines instead of a single blank line.
  128. Do enable the default git pre-commit hook that detect trailing
  129. whitespace for you and help you to avoid corrupting cairo's tree with
  130. it. Do that as follows:
  131. chmod a+x .git/hooks/pre-commit
  132. You might also find the git-stripspace utility helpful which acts as a
  133. filter to remove trailing whitespace as well as initial, final, and
  134. duplicate blank lines.
  135. As a special case of the bracing and whitespace guidelines, function
  136. definitions should always take the following form:
  137. void
  138. my_function (argument)
  139. {
  140. do_my_things ();
  141. }
  142. And function prototypes should similarly have the return type (and
  143. associated specifiers and qualifiers) on a line above the function, so
  144. that the function name is flush left.
  145. Break up long lines (> ~80 characters) and use whitespace to align
  146. things nicely. For example the arguments in a long list to a function
  147. call should all be aligned with each other:
  148. align_function_arguments (argument_the_first,
  149. argument_the_second,
  150. argument_the_third);
  151. And as a special rule, in a function prototype, (as well as in the
  152. definition), whitespace should be inserted between the parameter types
  153. and names so that the names are aligned:
  154. void
  155. align_parameter_names_in_prototypes (const char *char_star_arg,
  156. int int_arg,
  157. double *double_star_arg,
  158. double double_arg);
  159. Note that parameters with a * prefix are aligned one character to the
  160. left so that the actual names are aligned.
  161. Managing nested blocks
  162. ----------------------
  163. Long blocks that are deeply nested make the code very hard to
  164. read. Fortunately such blocks often indicate logically distinct chunks
  165. of functionality that are begging to be split into their own
  166. functions. Please listen to the blocks when they beg.
  167. In other cases, gratuitous nesting comes about because the primary
  168. functionality gets buried in a nested block rather than living at the
  169. primary level where it belongs. Consider the following:
  170. foo = malloc (sizeof (foo_t));
  171. if (foo) { /* Yuck! */
  172. ...
  173. /* lots of code to initialize foo */
  174. ...
  175. return SUCCESS;
  176. }
  177. return FAILURE;
  178. This kind of gratuitous nesting can be avoided by following a pattern
  179. of handling exceptional cases early and returning:
  180. foo = malloc (sizeof (foo_t));
  181. if (foo == NULL)
  182. return FAILURE;
  183. ...
  184. /* lots of code to initialize foo */
  185. ...
  186. return SUCCESS;
  187. The return statement is often the best thing to use in a pattern like
  188. this. If it's not available due to additional nesting above which
  189. require some cleanup after the current block, then consider splitting
  190. the current block into a new function before using goto.
  191. Memory allocation
  192. -----------------
  193. Because much of cairo's data consists of dynamically allocated arrays,
  194. it's very easy to introduce integer overflow issues whenever malloc()
  195. is called. Use the _cairo_malloc2(), _cairo_malloc3(), and
  196. _cairo_malloc2_add1 macros to avoid these cases; these macros check
  197. for overflow and will return NULL in that case.
  198. malloc (n * size) => _cairo_malloc_ab (n, size)
  199. e.g. malloc (num_elts * sizeof(some_type)) =>
  200. _cairo_malloc2 (num_elts, sizeof(some_type))
  201. malloc (a * b * size) => _cairo_malloc_abc (a, b, size)
  202. e.g. malloc (width * height * 4) =>
  203. _cairo_malloc3 (width, height, 4)
  204. malloc (n * size + k) => _cairo_malloc_ab_plus_c (n, size, k)
  205. e.g. malloc (num * sizeof(entry) + sizeof(header)) =>
  206. _cairo_malloc2k (num, sizeof(entry), sizeof(header))
  207. In general, be wary of performing any arithmetic operations in an
  208. argument to malloc. You should explicitly check for integer overflow
  209. yourself in any more complex situations.
  210. Mode lines
  211. ----------
  212. So given the rules above, what is the best way to simplify one's life as
  213. a code monkey? Get your editor to do most of the tedious work of
  214. beautifying your code!
  215. As a reward for reading this far, here are some mode lines for the more
  216. popular editors:
  217. /*
  218. * vim:sw=4:sts=4:ts=8:tw=78:fo=tcroq:cindent:cino=\:0,(0
  219. * vim:isk=a-z,A-Z,48-57,_,.,-,>
  220. */
  221. TODO
  222. ----
  223. Write rules for common editors to use this style. Also cleanup/unify
  224. the modelines in the source files.