nep1.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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" suffix.
  83. .. code-block:: nim
  84. type
  85. UnluckyError = object of Exception
  86. - Unless marked with the `{.pure.}` pragma, members of enums should have an
  87. identifying prefix, such as an abbreviation of the enum's name.
  88. .. code-block:: nim
  89. type
  90. PathComponent = enum
  91. pcDir
  92. pcLinkToDir
  93. pcFile
  94. pcLinkToFile
  95. - Non-pure enum values should use camelCase whereas pure enum values should use
  96. PascalCase.
  97. .. code-block:: nim
  98. type
  99. PathComponent {.pure.} = enum
  100. Dir
  101. LinkToDir
  102. File
  103. LinkToFile
  104. - In the age of HTTP, HTML, FTP, TCP, IP, UTF, WWW it is foolish to pretend
  105. these are somewhat special words requiring all uppercase. Instead treat them
  106. as what they are: Real words. So it's ``parseUrl`` rather than
  107. ``parseURL``, ``checkHttpHeader`` instead of ``checkHTTPHeader`` etc.
  108. Coding Conventions
  109. ------------------
  110. - The 'return' statement should ideally be used when its control-flow properties
  111. are required. Use a procedure's implicit 'result' variable whenever possible.
  112. This improves readability.
  113. .. code-block:: nim
  114. proc repeat(text: string, x: int): string =
  115. result = ""
  116. for i in 0 .. x:
  117. result.add($i)
  118. - Use a proc when possible, only using the more powerful facilities of macros,
  119. templates, iterators, and converters when necessary.
  120. - Use the ``let`` statement (not the ``var`` statement) when declaring variables that
  121. do not change within their scope. Using the ``let`` statement ensures that
  122. variables remain immutable, and gives those who read the code a better idea
  123. of the code's purpose.
  124. Conventions for multi-line statements and expressions
  125. -----------------------------------------------------
  126. - Tuples which are longer than one line should indent their parameters to
  127. align with the parameters above it.
  128. .. code-block:: nim
  129. type
  130. LongTupleA = tuple[wordyTupleMemberOne: int, wordyTupleMemberTwo: string,
  131. wordyTupleMemberThree: float]
  132. - Similarly, any procedure and procedure type declarations that are longer#
  133. than one line should do the same thing.
  134. .. code-block:: nim
  135. type
  136. EventCallback = proc (timeReceived: Time, errorCode: int, event: Event,
  137. output: var string)
  138. proc lotsOfArguments(argOne: string, argTwo: int, argThree: float
  139. argFour: proc(), argFive: bool): int
  140. {.heyLookALongPragma.} =
  141. - Multi-line procedure calls should continue on the same column as the opening
  142. parenthesis (like multi-line procedure declarations).
  143. .. code-block:: nim
  144. startProcess(nimExecutable, currentDirectory, compilerArguments
  145. environment, processOptions)