style_guidelines.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // This file is part of libigl, a simple c++ geometry processing library.
  28. //
  29. // Copyright (C) 2013 Alec Jacobson &lt;alecjacobson@gmail.com&gt;
  30. //
  31. // This Source Code Form is subject to the terms of the Mozilla Public License
  32. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  33. // obtain one at http://mozilla.org/MPL/2.0/.
  34. #ifndef IGL_EXAMPLE_FUN_H
  35. #define IGL_EXAMPLE_FUN_H
  36. #include "igl_inline.h"
  37. namespace igl
  38. {
  39. // This is an example of a function, it takes a templated parameter and
  40. // shovels it into cout
  41. //
  42. // Templates:
  43. // T type that supports
  44. // Input:
  45. // input some input of a Printable type
  46. // Returns true for the sake of returning something
  47. template &lt;typename Printable&gt;
  48. IGL_INLINE bool example_fun(const Printable &amp; input);
  49. }
  50. #ifdef IGL_HEADER_ONLY
  51. # include "example_fun.cpp"
  52. #endif
  53. #endif
  54. </code></pre>
  55. <h3>example_fun.cpp</h3>
  56. <pre><code>
  57. // This file is part of libigl, a simple c++ geometry processing library.
  58. //
  59. // Copyright (C) 2013 Alec Jacobson &lt;alecjacobson@gmail.com&gt;
  60. //
  61. // This Source Code Form is subject to the terms of the Mozilla Public License
  62. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  63. // obtain one at http://mozilla.org/MPL/2.0/.#include "igl/example_fun.h"
  64. #include &lt;iostream&gt;
  65. template &lt;typename Printable&gt;
  66. IGL_INLINE bool igl::example_fun(const Printable &amp; input)
  67. {
  68. using namespace std;
  69. cout&lt;&lt;"example_fun: "&lt;&lt;input&lt;&lt;endl;
  70. return true;
  71. }
  72. #ifndef IGL_HEADER_ONLY
  73. template bool igl::example_fun&lt;double&gt;(const double&amp; input);
  74. template bool igl::example_fun&lt;int&gt;(const int&amp; input);
  75. #endif
  76. </code></pre>
  77. <h2 id=general_rules>General rules</h2>
  78. <ul>
  79. <li> Use a single .h/.cpp pair with the same name as the function </li>
  80. <li>
  81. At least one version of the function should use references for all outputs
  82. </li>
  83. <li>
  84. Use wrappers and additional prototypes for returning single-output functions'
  85. outputs, but the default is to only use outputs
  86. </li>
  87. <li> All inputs should be <code>const</code>.</li>
  88. <li>
  89. Use C++ references (e.g. <code>Matrix &amp; mat</code>) for inputs and outputs
  90. rather than pointers (e.g. <code>Matrix * mat</code>) or pass-by-copy (e.g.
  91. <code>Matrix mat</code>).
  92. </li>
  93. <li>
  94. Write multiple prototypes if you'd like to have optional parameters with
  95. default values.
  96. </li>
  97. <li>
  98. External dependencies (besides Eigen and OpenGL) are only allowed within extras.
  99. </li>
  100. <li>
  101. New extras and their dependencies must be discussed first.
  102. </li>
  103. <li>
  104. Hard-to-compile or obscure external dependencies can go in the <code>external/</code> directory.
  105. </li>
  106. <li>
  107. Do not use the <code>using namespace</code> directive anywhere outside a local scope. This
  108. means never write: <code>using namespace std;</code> or <code>using namespace
  109. igl;</code> etc. at the top of a file.
  110. </li>
  111. <li>
  112. Function names should either be the same as the corresponding MATLAB function
  113. or should use all lowercase separated by underscores: e.g.
  114. <code>my_function_name</code>
  115. </li>
  116. <li> Classes should be in <code>CamelCase</code></li>
  117. <li> No tabs, only two spaces per indentation level </li>
  118. <li> Be generous with assertions and always identify them with strings:
  119. <br> e.g. <code>assert(m&lt;n &amp;&amp; "m must be less than n");</code></li>
  120. </ul>
  121. <h2>Specific rules</h2>
  122. <p>
  123. Each file should be wrapped in an ifndef compiler directive. If the
  124. file's (and function's) name is example.h, then the ifndef should
  125. always begin with IGL_, then the function/file name in all caps then
  126. _H. As in:
  127. </p>
  128. <pre><code>
  129. #ifndef IGL_EXAMPLE_H
  130. #define IGL_EXAMPLE_H
  131. ...
  132. #endif</code></pre>
  133. <p>
  134. Each file should begin with prototypes *without implementations* of
  135. each version of the function. All defined in the igl namespace. Each
  136. prototype should have its own comments describing what it is doing,
  137. and its templates, inputs, outputs.
  138. </p>
  139. <p>
  140. Implementation should be separated from prototypes in a .cpp file.
  141. </p>
  142. <p>
  143. Any includes, such as std libraries etc. should be in the .cpp file not the
  144. header .h file (whenever feasibly possible).
  145. </p>
  146. <p>
  147. Be generous by templating your inputs and outputs. If you do use
  148. templates, you must document the template just as you document inputs
  149. and outputs.
  150. </p>
  151. <h2>Useful scripts to check style</h2>
  152. <table>
  153. <tr>
  154. <th>script</th>
  155. <th>Description</th>
  156. <tr class=d0>
  157. <td><code>grep -L IGL_INLINE *</code></td>
  158. <td>Find files that aren't using "IGL_INLINE"</td>
  159. </tr>
  160. <tr class=d1>
  161. <td><code>grep -L "namespace igl" *</code></td>
  162. <td>Find files that aren't using igl namespace</td>
  163. </tr>
  164. <tr class=d0>
  165. <td><code>grep -P '\t' *</code></td>
  166. <td>Find files using [TAB] character</td>
  167. </tr>
  168. <tr class=d1>
  169. <td><code>grep -l "^using namespace" *.cpp</code></td>
  170. <td>Find .cpp files that contain ^using namespace</td>
  171. </tr>
  172. <tr class=d0>
  173. <td><code>grep -l 'assert([^"]*);' *</code></td>
  174. <td>Find files using asserts without identifier strings</td>
  175. </tr>
  176. <tr class=d1>
  177. <td><code>grep -l "ifndef IGL_.*[^H] *$" *</code></td>
  178. <td>Find .h files that contain ifndef IGL_*[^H]</td>
  179. </tr>
  180. <tr class=d0>
  181. <td><code>grep -L "^#ifndef IGL_" *</code></td>
  182. <td>Find files that don't contain #ifndef IGL_</td>
  183. </tr>
  184. <tr class=d1>
  185. <td><code>grep -L "^This file is part of libigl" *</code></td>
  186. </tr>
  187. </table>
  188. <p>See also: <a href=tutorial.html>tutorial</a>, <a
  189. href=doc.html>auto-documentation</a>, <a href=file-formats/index.html>file
  190. formats</a></p>
  191. </div>
  192. </div>
  193. </body>
  194. </html>