coding_style.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  2. <HTML>
  3. <HEAD>
  4. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
  5. <META NAME="Author" CONTENT="tterribe">
  6. <META NAME="Generator" CONTENT="vim">
  7. <TITLE>Daala Coding Style</TITLE>
  8. </HEAD>
  9. <BODY>
  10. <H1>Daala Coding Style</H1>
  11. <H2>Overview</H2>
  12. <P>
  13. This document defines the coding style of the Daala project.
  14. In general, Daala tries to conform to C89, and avoid features which are not
  15. available in C89 in portable code.
  16. We try to minimize our dependence on libc (e.g., using macros around memory
  17. allocation functions).
  18. </P>
  19. <H2>General Rules</H2>
  20. <H3>License Block</H3>
  21. <P>
  22. The top of each file should contain the following license header:
  23. </P>
  24. <PRE>
  25. /*Daala video codec
  26. Copyright (c) 2001-2013 Daala project contributors. All rights reserved.
  27. Redistribution and use in source and binary forms, with or without
  28. modification, are permitted provided that the following conditions are met:
  29. - Redistributions of source code must retain the above copyright notice, this
  30. list of conditions and the following disclaimer.
  31. - Redistributions in binary form must reproduce the above copyright notice,
  32. this list of conditions and the following disclaimer in the documentation
  33. and/or other materials provided with the distribution.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  35. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  38. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  40. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  42. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  43. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  44. </PRE>
  45. <P>
  46. Make sure the copyright years are up to date.
  47. </P>
  48. <P>
  49. In general, it is not necessary to list invididual copyright holders in each
  50. file (beyond "Daala project contributors").
  51. Xiph does not require assignment of copyright.
  52. </P>
  53. <H3>Globals</H3>
  54. <P>
  55. Non-const global variables in portable code are not allowed.
  56. They make it impossible to write re-entrant code without introducing a
  57. dependency on a threading library.
  58. </P>
  59. <H3>Naming</H3>
  60. <P>
  61. All functions and types use the canonical C naming scheme: all lowercase with
  62. underscores for separators.
  63. <CODE>#define</CODE>s and global const variables use all uppercase with
  64. underscores for separators.
  65. </P>
  66. <P>
  67. All functions and types begin with an <CODE>od_</CODE> prefix.
  68. All <CODE>#define</CODE>s and global const variables begin with an
  69. <CODE>OD_</CODE> prefix.
  70. </P>
  71. <P>
  72. Do not prefix names with single letters indicating their role (m for member
  73. variables, k for constants, g for globals, etc.).
  74. </P>
  75. <P>
  76. Do not use leading or trailing underscores in regular code.
  77. </P>
  78. <P>
  79. Macro definitions which need to declare local variables should append two
  80. trailing underscores to their name to avoid conflicts with regular code.
  81. </P>
  82. <H3>Constructors/Destructors</H3>
  83. <P>
  84. Initialization and deinitialization routines for type <CODE>od_foo</CODE> are
  85. named <CODE>od_foo_init()</CODE> and <CODE>od_foo_clear()</CODE>.
  86. These should take an already-allocated pointer to an object of the appropriate
  87. type.
  88. If allocating versions of these functions are desired, they should be named
  89. <CODE>od_foo_create()</CODE> and <CODE>od_foo_free()</CODE>, and dispatch to
  90. <CODE>od_foo_init()</CODE> and <CODE>od_foo_clear()</CODE> respectively.
  91. </P>
  92. <H2>Formatting</H2>
  93. <H3>Tabs</H3>
  94. <P>
  95. There are no tabs in source code.
  96. Do not use them, ever.
  97. </P>
  98. <H3>Trailing Whitespace</H3>
  99. <P>
  100. Remove all trailing whitespace at the end of a line.
  101. </P>
  102. <H3>Indentation</H3>
  103. <P>
  104. Use two-space indenting.
  105. </P>
  106. <H3>Line Length</H3>
  107. <P>
  108. Lines should be no more than 79 characters.
  109. This allows them to be displayed fully in a standard terminal even in diffs.
  110. The extra column on the right also makes long lines easier to spot.
  111. </P>
  112. <H3>Line Wrapping</H3>
  113. If a line needs to be continued on to the next line, use a single additional
  114. space for all subsequent lines.
  115. Do not align subsequent lines with <CODE>=</CODE> operators, function
  116. parameters, etc.
  117. Do not add additional indentation to indicate open parentheses, etc.
  118. If this produces lines you think are confusing, break them up into multiple
  119. statements.
  120. </P>
  121. <P>
  122. Prefer wrapping at lower-precedence operators.
  123. </P>
  124. <P>
  125. Prefer making wrapped lines as close in length as possible, while minimizing
  126. the total number of lines.
  127. </P>
  128. <P>
  129. All unary operators and the binary operators <CODE>+</CODE>, <CODE>-</CODE>,
  130. <CODE>&amp;&amp;</CODE>, and <CODE>||</CODE> belong to the start of the next
  131. line, not the end of the previous line.
  132. </P>
  133. <H3><CODE>struct</CODE> fields and local variables</CODE></H3>
  134. <P>
  135. One variable/field per declaration (e.g., do not use
  136. <CODE>int x, y, z;</CODE>).
  137. </P>
  138. <P>
  139. Do not attempt to align variable/field names.
  140. </P>
  141. <P>
  142. Do not initialize variables in the declaration.
  143. This makes it harder to follow C89's "no declarations after a statement" rule
  144. and can hide logic errors that would be exposed by valgrind.
  145. </P>
  146. <H3>Comments</H3>
  147. <P>
  148. Do not use C++ comments (<CODE>//</CODE>).
  149. </P>
  150. <P>
  151. Write properly punctuated comments that start with a capital letter.
  152. </P>
  153. <P>
  154. Do not put comments to the right of code.
  155. Put them on a separate line.
  156. </P>
  157. <P>
  158. Format multi-line comments as follows:
  159. </P>
  160. <PRE>
  161. /*This is a multi-line comment.
  162. Each sentence starts on a new line, and all subsequent lines in the same
  163. sentence are indented a single space, making it easier to change the comment
  164. without re-flowing a whole paragraph.
  165. This gives better "blame" information, and makes diffs easier to read.
  166. Other notes:
  167. - Start the first sentence on the same line as the start of the comment, and
  168. do not add a space in front of it.
  169. The former ensures that single-line and multi-line comments have the same
  170. style, making it easier to convert between the two.
  171. The latter maximizes the space available for prose in heavily-indented
  172. code.
  173. - Align the start of subsequent sentences with the start of the first one.
  174. - End the comment on the same line as the last sentence, and do not add an
  175. extra space after that sentence.*/
  176. </PRE>
  177. <H3>Preprocessor Statements</H3>
  178. <P>
  179. All pre-processor statements begin with a <CODE>#</CODE> in the leftmost
  180. column.
  181. Indent pre-processor statements by adding spaces after the <CODE>#</CODE>, not
  182. before it.
  183. </P>
  184. <P>
  185. Preprocessor statements inside an
  186. <CODE>#if</CODE>/<CODE>#else</CODE>/<CODE>#endif</CODE> block are indented by
  187. one space per level (after the leading <CODE>#</CODE>).
  188. </P>
  189. <P>
  190. Use <CODE>#if [!]defined(X)</CODE> instead of <CODE>#if[n]def X</CODE>.
  191. The former makes it easier to extend to multiple conditions, zero out, etc.,
  192. e.g., replacing
  193. <PRE>
  194. #if defined(X)
  195. </PRE>
  196. with
  197. <PRE>
  198. #if defined(X) &amp;&amp; 0
  199. </PRE>
  200. </P>
  201. <H3>Blank Lines</H3>
  202. <P>
  203. There should never be more than a single blank line in a row.
  204. </P>
  205. <P>
  206. Do not add blank lines in the interior of a function, except inside of a
  207. multi-line comment.
  208. If you find yourself wanting to do this, use two functions instead.
  209. </P>
  210. <H3>Operator Spacing</H3>
  211. <P>
  212. All non-unary operators with the precedence of <CODE>+</CODE> and
  213. <CODE>-</CODE> or less should have one space on either side, except for
  214. commas, which have a single following space.
  215. </P>
  216. <P>
  217. There is no space before the opening parenthesis of a function call argument
  218. list, nor after a closing parenthesis followed by a comma or a semicolon.
  219. </P>
  220. <P>
  221. Semicolons in <CODE>for</CODE> loop iterators also have a single following
  222. space, except when the following statement is empty.
  223. </P>
  224. <H3>Pointers</H3>
  225. <P>
  226. The <CODE>*</CODE> in pointer types hugs the variable name, not the type name.
  227. </P>
  228. <H3>Braces</H3>
  229. <P>
  230. Opening braces go on the same line as the start of the construct they are
  231. opening (function body, <CODE>if</CODE>, <CODE>else</CODE>, <CODE>for</CODE>,
  232. <CODE>do</CODE>, <CODE>while</CODE>, <CODE>switch</CODE>, etc.), separated by
  233. a single space.
  234. </P>
  235. <P>
  236. Do not put spaces in empty braces.
  237. </P>
  238. <P>
  239. Closing braces go on a line by themselves, indented at the same level as the
  240. opening line.
  241. </P>
  242. <H3>Array Initializers</H3>
  243. <P>
  244. If an array initializer fits on a single line, put a single space inside the
  245. brace on each end.
  246. </P>
  247. <P>
  248. If an array initializer does not fit on a single line, do not wrap it as one
  249. long line.
  250. Instead, start a new line after the opening brace and indent the initialization
  251. data.
  252. Start new lines of initialization data as necessary, without the additional one
  253. space used to indent wrapped lines.
  254. </P>
  255. <P>
  256. For multi-line multidimensional array initializers, place the comma on the same
  257. line as the closing brace for the inner array, and start the opening brace for
  258. the next element of the outer array on a new line.
  259. </P>
  260. <H3>Trailing Commas</H3>
  261. <P>
  262. Do not include trailing commas in array initializers, enums, or other lists.
  263. These cause warnings on some platforms.
  264. </P>
  265. <H3>Parentheses</H3>
  266. <P>
  267. Do not use syntactically unnecessary parentheses unless they would generate a
  268. warning with gcc or MSVC.
  269. </P>
  270. <P>
  271. Parentheses should be placed around every single-expression macro definition,
  272. including constants.
  273. </P>
  274. <H3>Keywords</H3>
  275. <P>
  276. A single space follows <CODE>if</CODE>, <CODE>for</CODE>, <CODE>while</CODE>,
  277. and <CODE>switch</CODE> keywords, before the parenthesized expression.
  278. </P>
  279. <P>
  280. An <CODE>else</CODE> and the <CODE>while</CODE> in a <CODE>do</CODE> ...
  281. <CODE>while</CODE> loop begin on a new line, instead of being placed on the
  282. same line as the closing brace from the previous block.
  283. If the <CODE>else</CODE>'s statement is a subsequent <CODE>if</CODE>, that
  284. <CODE>if</CODE> is placed on the same line as the <CODE>else</CODE>.
  285. </P>
  286. <P>
  287. If you omit the <CODE>break</CODE> after a non-empty <CODE>case</CODE>
  288. statement where control reaches the end of the block, add a comment indicating
  289. you are falling through.
  290. If control reaches the end of the block in the last <CODE>case</CODE> statement
  291. in a <CODE>switch</CODE> block, do not omit the <CODE>break</CODE>.
  292. Use braces around the body of a <CODE>case</CODE> statement, unless the body is
  293. a single statement (excluding the <CODE>break</CODE>) and fits on one line
  294. (including the <CODE>break</CODE>).
  295. </P>
  296. <P>
  297. Loop or conditional bodies with a single statement may be placed on the same
  298. line as the start of the loop/conditional without any braces.
  299. Such loops can easily be stepped over in a debugger.
  300. This is allowed even for <CODE>if</CODE> statements with an <CODE>else</CODE>
  301. clause.
  302. If a loop/conditional body has to extend to multiple lines for any reason
  303. (including exceeding the 79-character line limit, even if still a single
  304. statement), then it must be contained in braces (and the first statement start
  305. on a new line).
  306. </P>
  307. <P>
  308. Use <CODE>for (;;)</CODE> for infinite loops, not <CODE>while (1)</CODE>.
  309. Some compilers complain about conditional expressions that always evaluate to
  310. true.
  311. </P>
  312. <P>
  313. <CODE>return</CODE> values are separated from the keyword by a single space,
  314. and are not enclosed in parentheses.
  315. </P>
  316. <H4>Examples:</H4>
  317. <PRE>
  318. for (i = 0; i &lt; n; i++) x[i] = 0;
  319. </PRE>
  320. <PRE>
  321. do something();
  322. while (!done);
  323. </PRE>
  324. <PRE>
  325. while (!done) {
  326. do_something();
  327. do_something_else();
  328. }
  329. </PRE>
  330. <PRE>
  331. if (test) do_this();
  332. else do_that();
  333. </PRE>
  334. <PRE>
  335. if (condition) {
  336. do_one_thing();
  337. then_another();
  338. }
  339. else if (another_condition) do_something_else();
  340. </PRE>
  341. <PRE>
  342. switch (value) {
  343. case 1: {
  344. int foo;
  345. foo = compute_foo();
  346. use_foo(foo);
  347. break;
  348. }
  349. case 2: ignore_foo();
  350. /*Fall through.*/
  351. case 3: return -1;
  352. default: handle_default(); break;
  353. }
  354. </PRE>
  355. <H2>Functions</H2>
  356. <H3>Declarations</H3>
  357. <P>
  358. Put the return type and parameters on the same line as the function name,
  359. wrapping as necessary.
  360. Do not attempt to align the function names in a group of functions with
  361. different return types.
  362. </P>
  363. <H3>Function Parameter Ordering</H3>
  364. <P>
  365. If the function is associated with a single object, put that object first.
  366. </P>
  367. <P>
  368. If the function has both inputs and outputs, the outputs go before the inputs
  369. (like memcpy(), which was designed to mimic the assignment operator).
  370. </P>
  371. <P>
  372. When taking a variable-length array, the length/size/stride of the array goes
  373. after the pointer to the array.
  374. </P>
  375. <H2>Header Files</H2>
  376. <H3>The #define guard</H3>
  377. <P>
  378. All header files should be wrapped in a define guard as follows:
  379. <PRE>
  380. #if !defined(_path_file_H)
  381. # define _path_file_H (1)
  382. ...
  383. #endif
  384. </PRE>
  385. Path elements should be sparated by underscores.
  386. The leading <CODE>src</CODE> component of the path is omitted.
  387. Path/file names should be lower case (as identifiers which match
  388. <CODE>_[A-Z].*</CODE> are reserved by POSIX).
  389. </P>
  390. <H3>Forward Declarations</H3>
  391. <P>
  392. Do not forward declare things in order to avoid additional includes.
  393. Processing includes is fast in C, unlike C++, where they contain most of the
  394. code, and recompiling C is also fast.
  395. It's not worth the maintenance burden of having one thing defined in many
  396. places to avoid including some extra files.
  397. </P>
  398. <P>
  399. In header files which have circular dependencies, put any typedefs at the top,
  400. before including the headers which produce the cycle.
  401. </P>
  402. <H3>Inline Functions</H3>
  403. <P>
  404. Do not put inline functions in headers except in platform-specific sections.
  405. The inline keyword is not available in C89.
  406. Do not use static functions in headers as a substitute, as they generate
  407. warnings if unused.
  408. </P>
  409. </HTML>