nep1.rst 12 KB

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