nep1.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ==============================================
  2. Nim Enhancement Proposal #1 - Standard Library Style Guide
  3. ==============================================
  4. :Author: Clay Sweetser, Dominik Picheta
  5. :Version: |nimversion|
  6. .. contents::
  7. Introduction
  8. ============
  9. Although Nim supports a variety of code and formatting styles, it is
  10. nevertheless beneficial that certain community efforts, such as the standard
  11. library, should follow a consistent set of style guidelines when suitable.
  12. This enhancement proposal aims to list a series of guidelines that the standard
  13. library should follow.
  14. Note that there can be exceptions to these rules. Nim being as flexible as it
  15. is, there will be parts of this style guide that don't make sense in certain
  16. contexts. Furthermore, just as
  17. `Python's style guide<http://legacy.python.org/dev/peps/pep-0008/>`_ changes
  18. over time, this style guide will too.
  19. These rules will only be enforced for contributions to the Nim
  20. codebase and official projects, such as the Nim compiler, the standard library,
  21. and the various official tools such as C2Nim.
  22. ----------------
  23. Style Guidelines
  24. ----------------
  25. Spacing and Whitespace Conventions
  26. -----------------------------------
  27. - Lines should be no longer than 80 characters. Limiting the amount of
  28. information present on each line makes for more readable code - the reader
  29. has smaller chunks to process.
  30. - Two spaces should be used for indentation of blocks; tabstops are not allowed
  31. (the compiler enforces this). Using spaces means that the appearance of code
  32. is more consistent across editors. Unlike spaces, tabstop width varies across
  33. editors, and not all editors provide means of changing this width.
  34. - Although use of whitespace for stylistic reasons other than the ones endorsed
  35. by this guide are allowed, careful thought should be put into such practices.
  36. Not all editors support automatic alignment of code sections, and re-aligning
  37. long sections of code by hand can quickly become tedious.
  38. .. code-block:: nim
  39. # This is bad, as the next time someone comes
  40. # to edit this code block, they
  41. # must re-align all the assignments again:
  42. type
  43. WordBool* = int16
  44. CalType* = int
  45. ... # 5 lines later
  46. CalId* = int
  47. LongLong* = int64
  48. LongLongPtr* = ptr LongLong
  49. Naming Conventions
  50. ------------------
  51. Note: While the rules outlined below are the *current* naming conventions,
  52. these conventions have not always been in place. Previously, the naming
  53. conventions for identifiers followed the Pascal tradition of prefixes which
  54. indicated the base type of the identifier - PFoo for pointer and reference
  55. types, TFoo for value types, EFoo for exceptions, etc. Though this has since
  56. changed, there are many places in the standard library which still use this
  57. convention. Such style remains in place purely for legacy reasons, and will be
  58. changed in the future.
  59. - Type identifiers should be in PascalCase. All other identifiers should be in
  60. camelCase with the exception of constants which **may** use PascalCase but
  61. are not required to.
  62. .. code-block:: nim
  63. # Constants can start with either a lower case or upper case letter.
  64. const aConstant = 42
  65. const FooBar = 4.2
  66. var aVariable = "Meep" # Variables must start with a lowercase letter.
  67. # Types must start with an uppercase letter.
  68. type
  69. FooBar = object
  70. For constants coming from a C/C++ wrapper, ALL_UPPERCASE are allowed, but ugly.
  71. (Why shout CONSTANT? Constants do no harm, variables do!)
  72. - When naming types that come in value, pointer, and reference varieties, use a
  73. regular name for the variety that is to be used the most, and add a "Obj",
  74. "Ref", or "Ptr" suffix for the other varieties. If there is no single variety
  75. that will be used the most, add the suffixes to the pointer variants only. The
  76. same applies to C/C++ wrappers.
  77. .. code-block:: nim
  78. type
  79. Handle = object # Will be used most often
  80. fd: int64
  81. HandleRef = ref Handle # Will be used less often
  82. - Exception and Error types should have the "Error" or "Defect" suffix.
  83. .. code-block:: nim
  84. type
  85. ValueError = object of CatchableError
  86. AssertionDefect = object of Defect
  87. Foo = object of Exception # bad style, try to inherit CatchableError or Defect
  88. - Unless marked with the `{.pure.}` pragma, members of enums should have an
  89. identifying prefix, such as an abbreviation of the enum's name.
  90. .. code-block:: nim
  91. type
  92. PathComponent = enum
  93. pcDir
  94. pcLinkToDir
  95. pcFile
  96. pcLinkToFile
  97. - Non-pure enum values should use camelCase whereas pure enum values should use
  98. PascalCase.
  99. .. code-block:: nim
  100. type
  101. PathComponent {.pure.} = enum
  102. Dir
  103. LinkToDir
  104. File
  105. LinkToFile
  106. - In the age of HTTP, HTML, FTP, TCP, IP, UTF, WWW it is foolish to pretend
  107. these are somewhat special words requiring all uppercase. Instead treat them
  108. as what they are: Real words. So it's ``parseUrl`` rather than
  109. ``parseURL``, ``checkHttpHeader`` instead of ``checkHTTPHeader`` etc.
  110. - Operations like ``mitems`` or ``mpairs`` (or the now deprecated ``mget``)
  111. that allow a *mutating view* into some data structure should start with an ``m``.
  112. - When both in-place mutation and 'returns transformed copy' are available the latter
  113. is a past participle of the former:
  114. - reverse and reversed in algorithm
  115. - sort and sorted
  116. - rotate and rotated
  117. - When the 'returns transformed copy' version already exists like ``strutils.replace``
  118. an in-place version should get an ``-In`` suffix (``replaceIn`` for this example).
  119. - Use `subjectVerb`, not `verbSubject`, e.g.: `fileExists`, not `existsFile`.
  120. The stdlib API is designed to be **easy to use** and consistent. Ease of use is
  121. measured by the number of calls to achieve a concrete high level action. The
  122. ultimate goal is that the programmer can *guess* a name.
  123. The library uses a simple naming scheme that makes use of common abbreviations
  124. to keep the names short but meaningful.
  125. ------------------- ------------ --------------------------------------
  126. English word To use Notes
  127. ------------------- ------------ --------------------------------------
  128. initialize initT ``init`` is used to create a
  129. value type ``T``
  130. new newP ``new`` is used to create a
  131. reference type ``P``
  132. find find should return the position where
  133. something was found; for a bool result
  134. use ``contains``
  135. contains contains often short for ``find() >= 0``
  136. append add use ``add`` instead of ``append``
  137. compare cmp should return an int with the
  138. ``< 0`` ``== 0`` or ``> 0`` semantics;
  139. for a bool result use ``sameXYZ``
  140. put put, ``[]=`` consider overloading ``[]=`` for put
  141. get get, ``[]`` consider overloading ``[]`` for get;
  142. consider to not use ``get`` as a
  143. prefix: ``len`` instead of ``getLen``
  144. length len also used for *number of elements*
  145. size size, len size should refer to a byte size
  146. capacity cap
  147. memory mem implies a low-level operation
  148. items items default iterator over a collection
  149. pairs pairs iterator over (key, value) pairs
  150. delete delete, del del is supposed to be faster than
  151. delete, because it does not keep
  152. the order; delete keeps the order
  153. remove delete, del inconsistent right now
  154. include incl
  155. exclude excl
  156. command cmd
  157. execute exec
  158. environment env
  159. variable var
  160. value value, val val is preferred, inconsistent right
  161. now
  162. executable exe
  163. directory dir
  164. path path path is the string "/usr/bin" (for
  165. example), dir is the content of
  166. "/usr/bin"; inconsistent right now
  167. extension ext
  168. separator sep
  169. column col, column col is preferred, inconsistent right
  170. now
  171. application app
  172. configuration cfg
  173. message msg
  174. argument arg
  175. object obj
  176. parameter param
  177. operator opr
  178. procedure proc
  179. function func
  180. coordinate coord
  181. rectangle rect
  182. point point
  183. symbol sym
  184. literal lit
  185. string str
  186. identifier ident
  187. indentation indent
  188. ------------------- ------------ --------------------------------------
  189. Coding Conventions
  190. ------------------
  191. - The 'return' statement should ideally be used when its control-flow properties
  192. are required. Use a procedure's implicit 'result' variable whenever possible.
  193. This improves readability.
  194. .. code-block:: nim
  195. proc repeat(text: string, x: int): string =
  196. result = ""
  197. for i in 0 .. x:
  198. result.add($i)
  199. - Use a proc when possible, only using the more powerful facilities of macros,
  200. templates, iterators, and converters when necessary.
  201. - Use the ``let`` statement (not the ``var`` statement) when declaring variables that
  202. do not change within their scope. Using the ``let`` statement ensures that
  203. variables remain immutable, and gives those who read the code a better idea
  204. of the code's purpose.
  205. Conventions for multi-line statements and expressions
  206. -----------------------------------------------------
  207. - Tuples which are longer than one line should indent their parameters to
  208. align with the parameters above it.
  209. .. code-block:: nim
  210. type
  211. LongTupleA = tuple[wordyTupleMemberOne: int, wordyTupleMemberTwo: string,
  212. wordyTupleMemberThree: float]
  213. - Similarly, any procedure and procedure type declarations that are longer
  214. than one line should do the same thing.
  215. .. code-block:: nim
  216. type
  217. EventCallback = proc (timeReceived: Time, errorCode: int, event: Event,
  218. output: var string)
  219. proc lotsOfArguments(argOne: string, argTwo: int, argThree: float,
  220. argFour: proc(), argFive: bool): int
  221. {.heyLookALongPragma.} =
  222. - Multi-line procedure calls should continue on the same column as the opening
  223. parenthesis (like multi-line procedure declarations).
  224. .. code-block:: nim
  225. startProcess(nimExecutable, currentDirectory, compilerArguments
  226. environment, processOptions)