style-guide.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*====================================================================*
  2. - Copyright (C) 2001 Leptonica. All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions
  6. - are met:
  7. - 1. Redistributions of source code must retain the above copyright
  8. - notice, this list of conditions and the following disclaimer.
  9. - 2. Redistributions in binary form must reproduce the above
  10. - copyright notice, this list of conditions and the following
  11. - disclaimer in the documentation and/or other materials
  12. - provided with the distribution.
  13. -
  14. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
  18. - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *====================================================================*/
  26. style-guide.txt
  27. 10 May 2019
  28. This is not a complete guide to the coding style in leptonica.
  29. It covers most of the typographic issues and the most frequent
  30. coding patterns, such as how to check input args to functions.
  31. In general, you need to look at existing code to verify that your
  32. code meets the style guidelines. And if you find any aberrant code,
  33. please let me know!
  34. The C code in leptonica follows these conventions:
  35. (1) ANSI C, with no exceptions
  36. (a) C-style comments only: /* */
  37. (b) Variables are declared at the beginning of a function.
  38. [This is more strict than ANSI C, which only requires declarations
  39. to be at the beginning of a scope delineated by braces.]
  40. (c) Use typedefs for structs like Pix; e.g.,
  41. function(PIX *pixs)
  42. Do not do this (omitting the 'struct' keyword); it is valid C++,
  43. but not C:
  44. function(Pix *pixs)
  45. (2) Formatting
  46. (a) White space: 4 space indentation. No tabs, ever. No trailing spaces.
  47. (b) The code is set up to work with doxygen. Function headers are in
  48. this format:
  49. /*!
  50. * \brief pixSelectByAreaFraction()
  51. *
  52. * \param[in] pixs 1 bpp
  53. * \param[in] thresh threshold ratio of fg pixels to (w * h)
  54. * \param[in] connectivity 4 or 8
  55. * \param[in] type L_SELECT_IF_LT, L_SELECT_IF_GT,
  56. * L_SELECT_IF_LTE, L_SELECT_IF_GTE
  57. * \param[out] pchanged [optional] 1 if changed; 0 if clone returned
  58. * \return pixd, or NULL on error
  59. *
  60. * <pre>
  61. * Notes:
  62. * (1) The args specify constraints on the amount of foreground
  63. * coverage of the components that are kept.
  64. * ....
  65. * </pre>
  66. */
  67. (c) Function definition has return value on separate line and starting
  68. brace on separate line.
  69. PIX *
  70. function(...)
  71. {
  72. (d) Function arguments and local variables line up vertically;
  73. allow at least 2 spaces between type and variable name (including '*')
  74. function(PIX *pixs,
  75. l_int32 factor,
  76. l_float32 *pave)
  77. {
  78. char buf[BUF_SIZE];
  79. l_int32 w, h, d;
  80. l_float32 *vect;
  81. (e) Braces are placed like this for 'if', 'while', 'do':
  82. if (...) {
  83. ...
  84. } else if (...) {
  85. ...
  86. }
  87. The exceptions are for the beginning of a function and for the switch:
  88. switch (x)
  89. {
  90. case 1:
  91. ...
  92. ...
  93. }
  94. Braces are required if any of the clauses have more than one statement:
  95. if (...) {
  96. x = 0;
  97. } else {
  98. x++;
  99. y = 3.0 * x;
  100. }
  101. (f) Section headers should look like this:
  102. /*----------------------------------------------------------------------*
  103. * Statistics in an arbitrary rectangle *
  104. *----------------------------------------------------------------------*/
  105. (g) Major inline comments (starting a section) should be indented
  106. 4 extra spaces and start with a capital. Multiple line comments
  107. should be formatted like this:
  108. /* If w and h not input, determine the minimum size required
  109. * to contain the origin and all c.c. */
  110. (h) Minor inline comments (e.g., at the end of a line) should have
  111. 2 spaces and no leading capital; e.g.
  112. if (i && ((i % ncols) == 0)) { /* start new row */
  113. (3) Naming
  114. (a) Function names begin with lower case and successive words have
  115. the first letter capitalized; e.g., boxIntersects().
  116. (b) The first word in the function name is the name of the primary
  117. input data structure (if there is one); otherwise it can
  118. name the output data structure (if there is one).
  119. (c) Variable names are as short as possible, without causing confusion.
  120. (d) Pointers to data structures are typically named by the type of
  121. struct, without a leading 'p'; e.g., pixt, boxt.
  122. (e) When ptrs are input to a function, in order to return a value,
  123. if the local name would be 'ave', the pointer is 'pave'.
  124. (f) Preprocessor variables and enums are named all caps,
  125. with '_' between parts.
  126. (g) Static constants defined in a file should have the first letter of
  127. each word capitalized. (There are also some that are formatted
  128. like enums, with all caps and '_' between parts.)
  129. (h) There are very few globals in the library. Of these, there
  130. are just a handful of static globals that can be changed.
  131. Const globals are named with each word beginning with a capital; e.g.,
  132. ImageFileFormatExtensions[]
  133. Static globals that can be changed are named like preprocessor
  134. variables, except they are prepended by 'var_'; e.g.,
  135. var_PNG_WRITE_ALPHA
  136. Functions that set these static globals are named with a
  137. pre-pended 'l_'; e.g.,
  138. l_pngSetWriteAlpha()
  139. (i) Where there may be issues with namespace collisions with other
  140. libraries, function names can be prepended with 'l_'; e.g.,
  141. l_amapInsert()
  142. (4) Arg checking
  143. Both number values and ptrs can be returned in function arguments.
  144. The following applies equally to both types, and happens at the
  145. beginning of the function. We distinguish between returned entities
  146. that are optional and required. The following sequence of tests
  147. and initializations guarantees that no uninitialized arguments
  148. are returned:
  149. (a) First, all optional values are initialized if possible:
  150. if (pboxa) *pboxa = NULL; // Boxa **pboxa is optional
  151. (b) Second, if there is more than 1 required value, each is
  152. initialized if possible:
  153. if (pnar) *pnar = NULL; // Numa **pnar is required
  154. if (pnag) *pnag = NULL; // Numa **pnag is required
  155. Then all required arguments are tested in arbitrary order.
  156. But if there is exactly 1 required value, it can be checked
  157. and initialized if possible:
  158. if (!ppixd)
  159. return ERROR_INT("&pixd not defined, procName, 1);
  160. *ppixd = NULL;
  161. (5) Miscellaneous
  162. (a) Look around at the code after reviewing the guidelines.
  163. (b) Return nothing on stdout.
  164. (c) Returns to stderr should be blockable by compiler flags, such
  165. as NO_CONSOLE_IO, and by setting message severity thresholds
  166. both at compile and at run time. Naked fprintf(stderr, ...)
  167. should be avoided in the library.
  168. (d) Applications (in prog) that hand a FILE ptr to a library function,
  169. or accept heap-allocated data from a library function, should
  170. use special wrappers. See lept_*() functions in utils.c.
  171. (e) Changes to existing data structures and API changes should be
  172. avoided if possible.
  173. (f) Accessors are typically provided for struct fields that have
  174. extensive use.