style_guidelines.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>libigl - Style Guidelines</title>
  5. <link href="./style.css" rel="stylesheet" type="text/css">
  6. <body>
  7. <div id=container>
  8. <div class=article_inner>
  9. <a href=.><img src=libigl-logo.jpg alt="igl logo" class=center></a>
  10. <h1>libigl Style Guidelines</h1>
  11. <p>
  12. This library is shared by many people. This document highlights some style
  13. guidelines for <i>developers</i> of the <a href=.>libigl</a> library.
  14. </p>
  15. <p>
  16. Each function prototype should be well documented. Write a summary of what
  17. the function does and a description of each template, input and output in
  18. each prototype.
  19. </p>
  20. <h2>Example</h2>
  21. <p>
  22. Here is an example function defined in <code>include/igl/example_fun.h</code> and
  23. implemented in <code>include/igl/example_fun.cpp</code>.
  24. </p>
  25. <h3>example_fun.h</h3>
  26. <pre><code>
  27. #ifndef IGL_EXAMPLE_FUN_H
  28. #define IGL_EXAMPLE_FUN_H
  29. #include "igl_inline.h"
  30. namespace igl
  31. {
  32. // This is an example of a function, it takes a templated parameter and
  33. // shovels it into cout
  34. //
  35. // Templates:
  36. // T type that supports
  37. // Input:
  38. // input some input of a Printable type
  39. // Returns true for the sake of returning something
  40. template &lt;typename Printable&gt;
  41. IGL_INLINE bool example_fun(const Printable & input);
  42. }
  43. #ifdef IGL_HEADER_ONLY
  44. # include "example_fun.cpp"
  45. #endif
  46. #endif
  47. </code></pre>
  48. <h3>example_fun.cpp</h3>
  49. <pre><code>
  50. #include "igl/example_fun.h"
  51. #include &lt;iostream&gt;
  52. template &lt;typename Printable&gt;
  53. IGL_INLINE bool igl::example_fun(const Printable & input)
  54. {
  55. using namespace std;
  56. cout&lt;&lt;"example_fun: "&lt;&lt;input&lt;&lt;endl;
  57. return true;
  58. }
  59. #ifndef IGL_HEADER_ONLY
  60. template bool igl::example_fun&lt;double&gt;(const double& input);
  61. template bool igl::example_fun&lt;int&gt;(const int& input);
  62. #endif
  63. </code></pre>
  64. <h2>General rules</h2>
  65. <ul>
  66. <li> Use a single .h/.cpp pair with the same name as the function </li>
  67. <li>
  68. At least one version of the function should use references for all outputs
  69. </li>
  70. <li>
  71. Functions with external dependencies should be a single .h file (no .cpp file)
  72. so it won't appear in libigl.a
  73. </li>
  74. <li>
  75. Use wrappers and additional prototypes for returning single-output functions'
  76. outputs, but the default is to only use outputs
  77. </li>
  78. <li> All inputs should be const when appropriate </li>
  79. <li>
  80. Use c++ references for inputs and outputs rather than pointers or pass-by-copy
  81. </li>
  82. <li>
  83. Take the time to write multiple prototypes if you'd like to have optional
  84. parameters with default values
  85. </li>
  86. <li>
  87. External dependencies (besides Eigen, OpenGL, etc.) are discouraged
  88. </li>
  89. <li>
  90. External dependencies must be clearly identified at the top of each file.
  91. </li>
  92. <li>
  93. External dependencies can go in the external/ directory
  94. </li>
  95. <li>
  96. Do not use the using namespace directive anywhere outside a local scope. This
  97. means never write: <code>using namespace std;</code> or <code>using namespace
  98. igl;</code> etc. at the top of a file.
  99. </li>
  100. <li>
  101. Function names should either be the same as the corresponding MATLAB function
  102. or should use all lowercase separated by underscores: e.g.
  103. <code>my_function_name</code>
  104. </li>
  105. <li> Classes should be in <code>CamelCase</code></li>
  106. <li> No tabs, only two spaces per indentation level </li>
  107. <li> Be generous with assertions, but always identify them with strings:
  108. <br> e.g. <code>assert(m&lt;n && "m must be less than n");</code></li>
  109. </ul>
  110. <h2>Specific rules</h2>
  111. <p>
  112. Each file should be wrapped in an ifndef compiler directive. If the
  113. file's (and function's) name is example.h, then the ifndef should
  114. always begin with IGL_, then the function/file name in all caps then
  115. _H. As in:
  116. </p>
  117. <pre><code>
  118. #ifndef IGL_EXAMPLE_H
  119. #define IGL_EXAMPLE_H
  120. ...
  121. #endif</code></pre>
  122. <p>
  123. Each file should begin with prototypes *without implementations* of
  124. each version of the function. All defined in the igl namespace. Each
  125. prototype should have its own comments describing what it is doing,
  126. and its templates, inputs, outputs.
  127. </p>
  128. <p>
  129. Implementation should be separated from prototypes in a .cpp file.
  130. </p>
  131. <p>
  132. Any includes, such as std libraries etc. should be in the .cpp file not the
  133. header .h file (whenever feasibly possible).
  134. </p>
  135. <p>
  136. Be generous by templating your inputs and outputs. If you do use
  137. templates, you must document the template just as you document inputs
  138. and outputs.
  139. </p>
  140. <h2>Useful scripts to check style</h2>
  141. <table>
  142. <tr>
  143. <th>script</th>
  144. <th>Description</th>
  145. <tr class=d0>
  146. <td><code>grep -L IGL_INLINE *</code></td>
  147. <td>Find files that aren't using "IGL_INLINE"</td>
  148. </tr>
  149. <tr class=d1>
  150. <td><code>grep -L "namespace igl" *</code></td>
  151. <td>Find files that aren't using igl namespace</td>
  152. </tr>
  153. <tr class=d0>
  154. <td><code>grep -P '\t' *</code></td>
  155. <td>Find files using [TAB] character</td>
  156. </tr>
  157. <tr class=d1>
  158. <td><code>grep -l "^using namespace" *.cpp</code></td>
  159. <td>Find .cpp files that contain ^using namespace</td>
  160. </tr>
  161. <tr class=d0>
  162. <td><code>grep -l 'assert([^"]*);' *</code></td>
  163. <td>Find files using asserts without identifier strings</td>
  164. </tr>
  165. <tr class=d1>
  166. <td><code>grep -l "ifndef IGL_.*[^H] *$" *</code></td>
  167. <td>Find .h files that contain ifndef IGL_*[^H]</td>
  168. </tr>
  169. <tr class=d0>
  170. <td><code>grep -L "^#ifndef IGL_" *</code></td>
  171. <td>Find files that don't contain #ifndef IGL_</td>
  172. </tr>
  173. </table>
  174. <p>See also: <a href=tutorial.html>tutorial</a>, <a
  175. href=doc.html>auto-documentation</a>, <a href=file-formats/index.html>file
  176. formats</a></p>
  177. </div>
  178. </div>
  179. </body>
  180. </html>