nep1.html 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Standard Library Style Guide</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">Standard Library Style Guide</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <div class="search-groupby">
  49. Group by:
  50. <select onchange="groupBy(this.value)">
  51. <option value="section">Section</option>
  52. <option value="type">Type</option>
  53. </select>
  54. </div>
  55. <ul class="simple simple-toc" id="toc-list">
  56. <li><a class="reference" id="introduction_toc" href="#introduction">Introduction</a></li>
  57. <ul class="simple"><li><a class="reference" id="style-guidelines-spacing-and-whitespace-conventions_toc" href="#style-guidelines-spacing-and-whitespace-conventions">Spacing and Whitespace Conventions</a></li>
  58. <li><a class="reference" id="style-guidelines-naming-conventions_toc" href="#style-guidelines-naming-conventions">Naming Conventions</a></li>
  59. <li><a class="reference" id="style-guidelines-coding-conventions_toc" href="#style-guidelines-coding-conventions">Coding Conventions</a></li>
  60. <li><a class="reference" id="style-guidelines-conventions-for-multiminusline-statements-and-expressions_toc" href="#style-guidelines-conventions-for-multiminusline-statements-and-expressions">Conventions for multi-line statements and expressions</a></li>
  61. <li><a class="reference" id="style-guidelines-miscellaneous_toc" href="#style-guidelines-miscellaneous">Miscellaneous</a></li>
  62. </ul>
  63. </ul>
  64. </div>
  65. <div class="nine columns" id="content">
  66. <a href="https://github.com/nim-lang/Nim/tree/devel/doc/nep1.md#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  67. <a href="https://github.com/nim-lang/Nim/edit/devel/doc/nep1.md#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  68. <div id="tocRoot"></div>
  69. <p class="module-desc"><table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Author:</th><td>Clay Sweetser, Dominik Picheta</td></tr>
  70. <tr><th class="docinfo-name">Version:</th><td>2.3.1</td></tr>
  71. </tbody></table>
  72. <h1><a class="toc-backref" id="introduction" href="#introduction">Introduction</a></h1><p>Although Nim supports a variety of code and formatting styles, it is nevertheless beneficial that certain community efforts, such as the standard library, should follow a consistent set of style guidelines when suitable. This enhancement proposal aims to list a series of guidelines that the standard library should follow.</p>
  73. <p>Note that there can be exceptions to these rules. Nim being as flexible as it is, there will be parts of this style guide that don't make sense in certain contexts. Furthermore, just as <a class="reference external" href="https://legacy.python.org/dev/peps/pep-0008/">Python's style guide</a> changes over time, this style guide will too.</p>
  74. <p>These rules will only be enforced for contributions to the Nim codebase and official projects, such as the Nim compiler, the standard library, and the various official tools such as C2Nim.</p>
  75. <h2 id="introduction-style-guidelines"><center>Style Guidelines</center></h2>
  76. <h3><a class="toc-backref" id="style-guidelines-spacing-and-whitespace-conventions" href="#style-guidelines-spacing-and-whitespace-conventions">Spacing and Whitespace Conventions</a></h3><ul class="simple"><li>Lines should be no longer than 80 characters. Limiting the amount of information present on each line makes for more readable code - the reader has smaller chunks to process.</li>
  77. <li>Two spaces should be used for indentation of blocks; tabstops are not allowed (the compiler enforces this). Using spaces means that the appearance of code is more consistent across editors. Unlike spaces, tabstop width varies across editors, and not all editors provide means of changing this width.</li>
  78. <li><p>Although use of whitespace for stylistic reasons other than the ones endorsed by this guide are allowed, careful thought should be put into such practices. Not all editors support automatic alignment of code sections, and re-aligning long sections of code by hand can quickly become tedious.</p>
  79. <p><pre class="listing"><span class="Comment"># This is bad, as the next time someone comes</span>
  80. <span class="Comment"># to edit this code block, they</span>
  81. <span class="Comment"># must re-align all the assignments again:</span>
  82. <span class="Keyword">type</span>
  83. <span class="Identifier">WordBool</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Identifier">int16</span>
  84. <span class="Identifier">CalType</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Identifier">int</span>
  85. <span class="Operator">...</span> <span class="Comment"># 5 lines later</span>
  86. <span class="Identifier">CalId</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Identifier">int</span>
  87. <span class="Identifier">LongLong</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Identifier">int64</span>
  88. <span class="Identifier">LongLongPtr</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Keyword">ptr</span> <span class="Identifier">LongLong</span></pre></p>
  89. </li>
  90. </ul>
  91. <h3><a class="toc-backref" id="style-guidelines-naming-conventions" href="#style-guidelines-naming-conventions">Naming Conventions</a></h3><ul class="simple"><li><p>Type identifiers should be in PascalCase. All other identifiers should be in camelCase with the exception of constants which <strong>may</strong> use PascalCase but are not required to.</p>
  92. <p><pre class="listing"><span class="Comment"># Constants can start with either a lower case or upper case letter.</span>
  93. <span class="Keyword">const</span> <span class="Identifier">aConstant</span> <span class="Operator">=</span> <span class="DecNumber">42</span>
  94. <span class="Keyword">const</span> <span class="Identifier">FooBar</span> <span class="Operator">=</span> <span class="FloatNumber">4.2</span>
  95. <span class="Keyword">var</span> <span class="Identifier">aVariable</span> <span class="Operator">=</span> <span class="StringLit">&quot;Meep&quot;</span> <span class="Comment"># Variables must start with a lowercase letter.</span>
  96. <span class="Comment"># Types must start with an uppercase letter.</span>
  97. <span class="Keyword">type</span>
  98. <span class="Identifier">FooBar</span> <span class="Operator">=</span> <span class="Keyword">object</span></pre></p>
  99. <p>For constants coming from a C/C++ wrapper, ALL_UPPERCASE are allowed, but ugly. (Why shout CONSTANT? Constants do no harm, variables do!)</p>
  100. </li>
  101. <li><p>When naming types that come in value, pointer, and reference varieties, use a regular name for the variety that is to be used the most, and add a &quot;Obj&quot;, &quot;Ref&quot;, or &quot;Ptr&quot; suffix for the other varieties. If there is no single variety that will be used the most, add the suffixes to the pointer variants only. The same applies to C/C++ wrappers.</p>
  102. <p><pre class="listing"><span class="Keyword">type</span>
  103. <span class="Identifier">Handle</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Comment"># Will be used most often</span>
  104. <span class="Identifier">fd</span><span class="Punctuation">:</span> <span class="Identifier">int64</span>
  105. <span class="Identifier">HandleRef</span> <span class="Operator">=</span> <span class="Keyword">ref</span> <span class="Identifier">Handle</span> <span class="Comment"># Will be used less often</span></pre></p>
  106. </li>
  107. <li><p>Exception and Error types should have the &quot;Error&quot; or &quot;Defect&quot; suffix.</p>
  108. <p><pre class="listing"><span class="Keyword">type</span>
  109. <span class="Identifier">ValueError</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Keyword">of</span> <span class="Identifier">CatchableError</span>
  110. <span class="Identifier">AssertionDefect</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Keyword">of</span> <span class="Identifier">Defect</span>
  111. <span class="Identifier">Foo</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Keyword">of</span> <span class="Identifier">Exception</span> <span class="Comment"># bad style, try to inherit CatchableError or Defect</span></pre></p>
  112. </li>
  113. <li><p>Unless marked with the <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">pure</span><span class="Operator">.</span><span class="Punctuation">}</span></span></tt> pragma, members of enums should have an identifying prefix, such as an abbreviation of the enum's name.</p>
  114. <p><pre class="listing"><span class="Keyword">type</span>
  115. <span class="Identifier">PathComponent</span> <span class="Operator">=</span> <span class="Keyword">enum</span>
  116. <span class="Identifier">pcDir</span>
  117. <span class="Identifier">pcLinkToDir</span>
  118. <span class="Identifier">pcFile</span>
  119. <span class="Identifier">pcLinkToFile</span></pre></p>
  120. </li>
  121. <li><p>Non-pure enum values should use camelCase whereas pure enum values should use PascalCase.</p>
  122. <p><pre class="listing"><span class="Keyword">type</span>
  123. <span class="Identifier">PathComponent</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">pure</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span> <span class="Keyword">enum</span>
  124. <span class="Identifier">Dir</span>
  125. <span class="Identifier">LinkToDir</span>
  126. <span class="Identifier">File</span>
  127. <span class="Identifier">LinkToFile</span></pre></p>
  128. </li>
  129. <li>In the age of HTTP, HTML, FTP, TCP, IP, UTF, WWW it is foolish to pretend these are somewhat special words requiring all uppercase. Instead treat them as what they are: Real words. So it's <tt class="docutils literal"><span class="pre"><span class="Identifier">parseUrl</span></span></tt> rather than <tt class="docutils literal"><span class="pre"><span class="Identifier">parseURL</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">checkHttpHeader</span></span></tt> instead of <tt class="docutils literal"><span class="pre"><span class="Identifier">checkHTTPHeader</span></span></tt> etc.</li>
  130. <li>Operations like <tt class="docutils literal"><span class="pre"><span class="Identifier">mitems</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Identifier">mpairs</span></span></tt> (or the now deprecated <tt class="docutils literal"><span class="pre"><span class="Identifier">mget</span></span></tt>) that allow a <em>mutating view</em> into some data structure should start with an <tt class="docutils literal"><span class="pre"><span class="Identifier">m</span></span></tt>.</li>
  131. <li>When both in-place mutation and 'returns transformed copy' are available the latter is a past participle of the former:<ul class="simple"><li>reverse and reversed in algorithm</li>
  132. <li>sort and sorted</li>
  133. <li>rotate and rotated</li>
  134. </ul>
  135. </li>
  136. <li>When the 'returns transformed copy' version already exists like <tt class="docutils literal"><span class="pre"><span class="Identifier">strutils</span><span class="Operator">.</span><span class="Identifier">replace</span></span></tt> an in-place version should get an <tt class="docutils literal"><span class="pre">-In</span></tt> suffix (<tt class="docutils literal"><span class="pre"><span class="Identifier">replaceIn</span></span></tt> for this example).</li>
  137. <li>Use <tt class="docutils literal"><span class="pre"><span class="Identifier">subjectVerb</span></span></tt>, not <tt class="docutils literal"><span class="pre"><span class="Identifier">verbSubject</span></span></tt>, e.g.: <tt class="docutils literal"><span class="pre"><span class="Identifier">fileExists</span></span></tt>, not <tt class="docutils literal"><span class="pre"><span class="Identifier">existsFile</span></span></tt>.</li>
  138. </ul>
  139. <p>The stdlib API is designed to be <strong>easy to use</strong> and consistent. Ease of use is measured by the number of calls to achieve a concrete high level action. The ultimate goal is that the programmer can <em>guess</em> a name.</p>
  140. <p>The library uses a simple naming scheme that makes use of common abbreviations to keep the names short but meaningful.</p>
  141. <table border="1" class="docutils"><tr><th>English word</th><th>To use</th><th>Notes</th></tr>
  142. <tr><td>initialize</td><td>initFoo</td><td>initializes a value type <tt class="docutils literal"><span class="pre"><span class="Identifier">Foo</span></span></tt></td></tr>
  143. <tr><td>new</td><td>newFoo</td><td>initializes a reference type <tt class="docutils literal"><span class="pre"><span class="Identifier">Foo</span></span></tt> via <tt class="docutils literal"><span class="pre"><span class="Identifier">new</span></span></tt> or a value type <tt class="docutils literal"><span class="pre"><span class="Identifier">Foo</span></span></tt> with reference semantics.</td></tr>
  144. <tr><td>this or self</td><td>self</td><td>for method like procs, e.g.: <tt class="docutils literal"><span class="pre"><span class="Keyword">proc</span> <span class="Identifier">fun</span><span class="Punctuation">(</span><span class="Identifier">self</span><span class="Punctuation">:</span> <span class="Identifier">Foo</span><span class="Punctuation">,</span> <span class="Identifier">a</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span></span></tt> rationale: <tt class="docutils literal"><span class="pre"><span class="Identifier">self</span></span></tt> is more unique in English than <tt class="docutils literal"><span class="pre"><span class="Identifier">this</span></span></tt>, and <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span></span></tt> would not be DRY.</td></tr>
  145. <tr><td>find</td><td>find</td><td>should return the position where something was found; for a bool result use <tt class="docutils literal"><span class="pre"><span class="Identifier">contains</span></span></tt></td></tr>
  146. <tr><td>contains</td><td>contains</td><td>often short for <tt class="docutils literal"><span class="pre"><span class="Identifier">find</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">&gt;=</span> <span class="DecNumber">0</span></span></tt></td></tr>
  147. <tr><td>append</td><td>add</td><td>use <tt class="docutils literal"><span class="pre"><span class="Identifier">add</span></span></tt> instead of <tt class="docutils literal"><span class="pre"><span class="Identifier">append</span></span></tt></td></tr>
  148. <tr><td>compare</td><td>cmp</td><td>should return an int with the <tt class="docutils literal"><span class="pre"><span class="Operator">&lt;</span> <span class="DecNumber">0</span></span></tt> <tt class="docutils literal"><span class="pre"><span class="Operator">==</span> <span class="DecNumber">0</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Operator">&gt;</span> <span class="DecNumber">0</span></span></tt> semantics; for a bool result use <tt class="docutils literal"><span class="pre"><span class="Identifier">sameXYZ</span></span></tt></td></tr>
  149. <tr><td>put</td><td>put, <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Operator">=</span></span></tt></td><td>consider overloading <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Operator">=</span></span></tt> for put</td></tr>
  150. <tr><td>get</td><td>get, <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Punctuation">]</span></span></tt></td><td>consider overloading <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Punctuation">]</span></span></tt> for get; consider to not use <tt class="docutils literal"><span class="pre"><span class="Identifier">get</span></span></tt> as a prefix: <tt class="docutils literal"><span class="pre"><span class="Identifier">len</span></span></tt> instead of <tt class="docutils literal"><span class="pre"><span class="Identifier">getLen</span></span></tt></td></tr>
  151. <tr><td>length</td><td>len</td><td>also used for <em>number of elements</em></td></tr>
  152. <tr><td>size</td><td>size, len</td><td>size should refer to a byte size</td></tr>
  153. <tr><td>capacity</td><td>cap</td><td></td></tr>
  154. <tr><td>memory</td><td>mem</td><td>implies a low-level operation</td></tr>
  155. <tr><td>items</td><td>items</td><td>default iterator over a collection</td></tr>
  156. <tr><td>pairs</td><td>pairs</td><td>iterator over (key, value) pairs</td></tr>
  157. <tr><td>delete</td><td>delete, del</td><td>del is supposed to be faster than delete, because it does not keep the order; delete keeps the order</td></tr>
  158. <tr><td>remove</td><td>delete, del</td><td>inconsistent right now</td></tr>
  159. <tr><td>include</td><td>incl</td><td></td></tr>
  160. <tr><td>exclude</td><td>excl</td><td></td></tr>
  161. <tr><td>command</td><td>cmd</td><td></td></tr>
  162. <tr><td>execute</td><td>exec</td><td></td></tr>
  163. <tr><td>environment</td><td>env</td><td></td></tr>
  164. <tr><td>variable</td><td>var</td><td></td></tr>
  165. <tr><td>value</td><td>value, val </td><td>val is preferred, inconsistent right now</td></tr>
  166. <tr><td>executable</td><td>exe</td><td></td></tr>
  167. <tr><td>directory</td><td>dir</td><td></td></tr>
  168. <tr><td>path</td><td>path</td><td>path is the string &quot;/usr/bin&quot; (for example), dir is the content of &quot;/usr/bin&quot;; inconsistent right now</td></tr>
  169. <tr><td>extension</td><td>ext</td><td></td></tr>
  170. <tr><td>separator</td><td>sep</td><td></td></tr>
  171. <tr><td>column</td><td>col, column </td><td>col is preferred, inconsistent right now</td></tr>
  172. <tr><td>application</td><td>app</td><td></td></tr>
  173. <tr><td>configuration</td><td>cfg</td><td></td></tr>
  174. <tr><td>message</td><td>msg</td><td></td></tr>
  175. <tr><td>argument</td><td>arg</td><td></td></tr>
  176. <tr><td>object</td><td>obj</td><td></td></tr>
  177. <tr><td>parameter</td><td>param</td><td></td></tr>
  178. <tr><td>operator</td><td>opr</td><td></td></tr>
  179. <tr><td>procedure</td><td>proc</td><td></td></tr>
  180. <tr><td>function</td><td>func</td><td></td></tr>
  181. <tr><td>coordinate</td><td>coord</td><td></td></tr>
  182. <tr><td>rectangle</td><td>rect</td><td></td></tr>
  183. <tr><td>point</td><td>point</td><td></td></tr>
  184. <tr><td>symbol</td><td>sym</td><td></td></tr>
  185. <tr><td>literal</td><td>lit</td><td></td></tr>
  186. <tr><td>string</td><td>str</td><td></td></tr>
  187. <tr><td>identifier</td><td>ident</td><td></td></tr>
  188. <tr><td>indentation</td><td>indent</td><td></td></tr>
  189. </table>
  190. <h3><a class="toc-backref" id="style-guidelines-coding-conventions" href="#style-guidelines-coding-conventions">Coding Conventions</a></h3><ul class="simple"><li><p>The <tt class="docutils literal"><span class="pre"><span class="Keyword">return</span></span></tt> statement should ideally be used when its control-flow properties are required. Use a procedure's implicit <tt class="docutils literal"><span class="pre"><span class="Identifier">result</span></span></tt> variable whenever possible. This improves readability.</p>
  191. <p><pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">repeat</span><span class="Punctuation">(</span><span class="Identifier">text</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span>
  192. <span class="Identifier">result</span> <span class="Operator">=</span> <span class="StringLit">&quot;&quot;</span>
  193. <span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="FloatNumber">0.</span><span class="Operator">.</span><span class="Identifier">x</span><span class="Punctuation">:</span>
  194. <span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Operator">$</span><span class="Identifier">i</span><span class="Punctuation">)</span></pre></p>
  195. </li>
  196. <li>Use a proc when possible, only using the more powerful facilities of macros, templates, iterators, and converters when necessary.</li>
  197. <li>Use the <tt class="docutils literal"><span class="pre"><span class="Keyword">let</span></span></tt> statement (not the <tt class="docutils literal"><span class="pre"><span class="Keyword">var</span></span></tt> statement) when declaring variables that do not change within their scope. Using the <tt class="docutils literal"><span class="pre"><span class="Keyword">let</span></span></tt> statement ensures that variables remain immutable, and gives those who read the code a better idea of the code's purpose.</li>
  198. </ul>
  199. <h3><a class="toc-backref" id="style-guidelines-conventions-for-multiminusline-statements-and-expressions" href="#style-guidelines-conventions-for-multiminusline-statements-and-expressions">Conventions for multi-line statements and expressions</a></h3><ul class="simple"><li><p>Tuples which are longer than one line should indent their parameters.</p>
  200. <p><pre class="listing"><span class="Keyword">type</span>
  201. <span class="Identifier">LongTupleA</span> <span class="Operator">=</span> <span class="Keyword">tuple</span><span class="Punctuation">[</span>
  202. <span class="Identifier">wordyTupleMemberOne</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">wordyTupleMemberTwo</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span>
  203. <span class="Identifier">wordyTupleMemberThree</span><span class="Punctuation">:</span> <span class="Identifier">float</span><span class="Punctuation">]</span></pre></p>
  204. </li>
  205. <li><p>Similarly, any procedure and procedure type declarations that are longer than one line should do the same thing. Double indent may be used to distinguish them from the body that follows - this applies to all constructs with a body (if, while, etc).</p>
  206. <p><pre class="listing"><span class="Keyword">type</span>
  207. <span class="Identifier">EventCallback</span> <span class="Operator">=</span> <span class="Keyword">proc</span><span class="Punctuation">(</span>
  208. <span class="Identifier">timeReceived</span><span class="Punctuation">:</span> <span class="Identifier">Time</span><span class="Punctuation">,</span> <span class="Identifier">errorCode</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">event</span><span class="Punctuation">:</span> <span class="Identifier">Event</span><span class="Punctuation">,</span>
  209. <span class="Identifier">output</span><span class="Punctuation">:</span> <span class="Keyword">var</span> <span class="Identifier">string</span><span class="Punctuation">)</span>
  210. <span class="Keyword">proc</span> <span class="Identifier">lotsOfArguments</span><span class="Punctuation">(</span>
  211. <span class="Identifier">argOne</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">argTwo</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">argThree</span><span class="Punctuation">:</span> <span class="Identifier">float</span><span class="Punctuation">,</span>
  212. <span class="Identifier">argFour</span><span class="Punctuation">:</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">argFive</span><span class="Punctuation">:</span> <span class="Identifier">bool</span><span class="Punctuation">,</span> <span class="Identifier">argSix</span><span class="Punctuation">:</span> <span class="Identifier">int</span>
  213. <span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">GenericType</span><span class="Punctuation">[</span><span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">string</span><span class="Punctuation">]</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">heyLookALongPragma</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
  214. <span class="Keyword">discard</span></pre></p>
  215. </li>
  216. <li><p>Multi-line procedure calls should continue indented (like multi-line procedure declarations).</p>
  217. <p><pre class="listing"><span class="Identifier">startProcess</span><span class="Punctuation">(</span>
  218. <span class="Identifier">nimExecutable</span><span class="Punctuation">,</span> <span class="Identifier">currentDirectory</span><span class="Punctuation">,</span> <span class="Identifier">compilerArguments</span>
  219. <span class="Identifier">environment</span><span class="Punctuation">,</span> <span class="Identifier">processOptions</span><span class="Punctuation">)</span></pre></p>
  220. </li>
  221. </ul>
  222. <p>Previous versions of this guide advocated vertical alignment along the opening brace / parenthesis - both styles are permissible with a preference for the current style in new code.</p>
  223. <h3><a class="toc-backref" id="style-guidelines-miscellaneous" href="#style-guidelines-miscellaneous">Miscellaneous</a></h3><ul class="simple"><li>Use <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">..</span><span class="Identifier">b</span></span></tt> instead of <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span> <span class="Operator">..</span> <span class="Identifier">b</span></span></tt>, except when <tt class="docutils literal"><span class="pre"><span class="Identifier">b</span></span></tt> contains an operator, for example <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span> <span class="Operator">..</span> <span class="Operator">-</span><span class="DecNumber">3</span></span></tt>. Likewise with <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">..&lt;</span><span class="Identifier">b</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">..^</span><span class="Identifier">b</span></span></tt> and other operators starting with <tt class="docutils literal"><span class="pre"><span class="Operator">..</span></span></tt>.</li>
  224. <li>Use <tt class="docutils literal"><span class="pre"><span class="Identifier">std</span></span></tt> prefix for standard library modules, namely use <tt class="docutils literal"><span class="pre"><span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">os</span></span></tt> for single module and use <tt class="docutils literal"><span class="pre"><span class="Identifier">std</span><span class="Operator">/</span><span class="Punctuation">[</span><span class="Identifier">os</span><span class="Punctuation">,</span> <span class="Identifier">sysrand</span><span class="Punctuation">,</span> <span class="Identifier">posix</span><span class="Punctuation">]</span></span></tt> for multiple modules.</li>
  225. <li><p>Prefer multiline triple quote literals to start with a newline; it's semantically identical (it's a feature of triple quote literals) but clearer because it aligns with the next line:</p>
  226. <p>use this:</p>
  227. <p><pre class="listing"><span class="Keyword">let</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="LongStringLit">&quot;&quot;&quot;
  228. foo
  229. bar
  230. &quot;&quot;&quot;</span></pre></p>
  231. <p>instead of:</p>
  232. <p><pre class="listing"><span class="Keyword">let</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="LongStringLit">&quot;&quot;&quot;foo
  233. bar
  234. &quot;&quot;&quot;</span></pre></p>
  235. </li>
  236. <li>A getter API for a private field <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span></span></tt> should preferably be named <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span></span></tt>, not <tt class="docutils literal"><span class="pre"><span class="Identifier">getFoo</span></span></tt>. A getter-like API should preferably be named <tt class="docutils literal"><span class="pre"><span class="Identifier">getFoo</span></span></tt>, not <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span></span></tt> if:<ul class="simple"><li>the API has side effects</li>
  237. <li>or the cost is not <tt class="docutils literal"><span class="pre"><span class="Identifier">O</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">)</span></span></tt></li>
  238. </ul>
  239. <p>For in between cases, there is no clear guideline.</p>
  240. </li>
  241. <li>Likewise with a setter API, replacing <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span></span></tt> with <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span><span class="Operator">=</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">getFoo</span></span></tt> with <tt class="docutils literal"><span class="pre"><span class="Identifier">setFoo</span></span></tt> in the above text.</li>
  242. </ul>
  243. </p>
  244. </div>
  245. </div>
  246. <div class="twelve-columns footer">
  247. <span class="nim-sprite"></span>
  248. <br>
  249. <small style="color: var(--hint);">Made with Nim. Generated: 2025-01-09 11:59:35 UTC</small>
  250. </div>
  251. </div>
  252. </div>
  253. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  254. </body>
  255. </html>