style_guidelines.html 5.7 KB

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