2015-01-11 18:41:59 +00:00
Nim Enhancement Proposal #1 - Standard Library Style Guide
2014-04-28 19:29:18 +00:00
Abstract
========
2015-01-11 18:41:51 +00:00
Although Nim, through its flexible AST and case-sensitivity settings, 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. Note
that these are *guidelines* only. The nature of 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 [Python's style guide][] changes over time, this
style guide will too.
[Python's style guide]: http://legacy.python.org/dev/peps/pep-0008/
2014-04-28 19:29:18 +00:00
Style Guidelines
================
2014-07-20 02:25:44 +00:00
### Spacing and Whitespace Conventions ###
2014-07-20 02:04:17 +00:00
- Lines should be no longer than 80 characters. Limiting the amount of
information present on each line makes for more readable code - the reader
2015-06-28 12:43:29 +00:00
has smaller chunks to process.
2014-07-20 02:15:12 +00:00
2014-07-20 02:04:17 +00:00
- 2 spaces should be used for indentation of blocks; tabstops are not allowed
2015-01-11 18:41:51 +00:00
(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.
2014-07-20 02:04:17 +00:00
2014-07-29 20:44:50 +00:00
- 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
2015-01-11 18:34:44 +00:00
long sections of code by hand can quickly become tedious.
2015-01-11 18:41:51 +00:00
```nimrod
# This is bad, as the next time someone comes
# to edit this code block, they
# must re-align all the assignments again:
type
WordBool* = int16
CalType* = int
... # 5 lines later
CalId* = int
LongLong* = int64
LongLongPtr* = ptr LongLong
```
2014-07-20 02:04:17 +00:00
2014-07-20 02:25:44 +00:00
### Naming Conventions ###
2014-07-20 02:04:17 +00:00
Note: While the rules outlined below are the *current* naming conventions,
these conventions have not always been in place. Previously, the naming
2015-01-11 18:41:51 +00:00
conventions for identifiers followed the Pascal tradition of prefixes which
indicated the base type of the identifier - PFoo for pointer and reference
2014-07-20 02:04:17 +00:00
types, TFoo for value types, EFoo for exceptions, etc. Though this has since
changed, there are many places in the standard library which still use this
2015-01-11 18:41:51 +00:00
convention. Such style remains in place purely for legacy reasons, and will be
changed in the future.
2014-04-28 19:29:18 +00:00
2015-02-04 20:07:44 +00:00
- Type identifiers should be in PascalCase. All other identifiers should be in
2015-01-11 18:41:51 +00:00
camelCase with the exception of constants which **may** use PascalCase but
2014-08-30 10:16:53 +00:00
are not required to.
2014-04-28 19:29:18 +00:00
2015-01-11 18:41:51 +00:00
```nimrod
2016-06-16 20:07:08 +00:00
const
aConstant = 42
FooBar = 4.2
2015-01-11 18:41:51 +00:00
2016-06-16 20:07:08 +00:00
var
aVariable = "Meep"
2015-01-11 18:41:51 +00:00
2016-06-16 20:07:08 +00:00
type
FooBar = object
2015-01-11 18:41:51 +00:00
```
2014-05-13 06:37:42 +00:00
2015-02-04 20:07:44 +00:00
For constants coming from a C/C++ wrapper, ALL_UPPERCASE are allowed, but ugly.
(Why shout CONSTANT? Constants do no harm, variables do!)
2015-01-11 18:41:51 +00:00
- 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 "Obj",
"Ref", or "Ptr" suffix for the other varieties. If there is no single variety
2015-02-04 20:07:44 +00:00
that will be used the most, add the suffixes to the pointer variants only. The
same applies to C/C++ wrappers.
2014-07-20 02:04:17 +00:00
```nimrod
type
2015-04-11 13:05:29 +00:00
Handle = int64 # Will be used most often
HandleRef = ref Handle # Will be used less often
2014-07-20 02:04:17 +00:00
```
- Exception and Error types should have the "Error" suffix.
2015-01-11 18:41:51 +00:00
2014-07-20 02:04:17 +00:00
```nimrod
2016-06-16 20:07:08 +00:00
type
UnluckyError = object of Exception
2014-07-20 02:04:17 +00:00
```
2014-05-13 06:37:42 +00:00
2014-07-20 02:04:17 +00:00
- Unless marked with the `{.pure.}` pragma, members of enums should have an
identifying prefix, such as an abbreviation of the enum's name. Since
2015-02-04 20:07:44 +00:00
non-pure enum members can be referenced without full qualification
(in the form of ``MyEnum.fooValue``).
2015-01-11 18:41:51 +00:00
```nimrod
2016-06-16 20:07:08 +00:00
type
PathComponent = enum
pcDir
pcLinkToDir
pcFile
pcLinkToFile
2015-01-11 18:41:51 +00:00
```
Non-pure enum values should use camelCase whereas pure enum values should use
PascalCase.
```nimrod
2016-06-16 20:07:08 +00:00
type
PathComponent {.pure.} = enum
Dir
LinkToDir
File
LinkToFile
2015-01-11 18:41:51 +00:00
```
2014-04-28 19:29:18 +00:00
2016-04-02 17:44:48 +00:00
- Uppercase acronyms (e.g. GPU) should be written in lowercase/capitalized: ``parseUrl`` rather than ``parseURL``, ``checkHttpHeader`` instead of ``checkHTTPHeader`` etc.
2015-05-19 11:43:34 +00:00
2014-07-20 02:25:44 +00:00
### Coding Conventions ###
2014-05-13 06:37:42 +00:00
- The 'return' statement should only be used when it's control-flow properties
2015-01-11 18:41:51 +00:00
are required. Use a procedures implicit 'result' variable instead. This
improves readability.
2014-10-11 23:00:49 +00:00
2015-01-11 18:41:51 +00:00
- Prefer to return `[]` and `""` instead of `nil` , or throw an exception if
that is appropriate.
2014-10-11 23:00:49 +00:00
- Use a proc when possible, only using the more powerful facilities of macros,
templates, iterators, and converters when necessary.
2014-05-13 06:37:42 +00:00
2015-01-11 18:41:51 +00:00
- Use the 'let' statement (not the var statement) when declaring variables that
do not change within their scope. Using the let statement ensures that
2014-05-13 06:37:42 +00:00
variables remain immutable, and gives those who read the code a better idea
of the code's purpose.
2015-07-08 02:53:25 +00:00
- For new types, it is usually recommended to have both 'ref' and 'object'
versions of the type available for others to use. By making both variants
available for use, the type may be allocated both on the stack and the heap.
2015-01-11 18:33:41 +00:00
2014-07-20 02:25:44 +00:00
### Conventions for multi-line statements and expressions ###
2014-05-13 06:37:42 +00:00
- Any tuple type declarations that are longer than one line should use the
regular object type layout instead. This enhances the readability of the
2015-01-11 18:41:51 +00:00
tuple declaration by splitting its members information across multiple lines.
2014-05-23 14:33:36 +00:00
```nimrod
2014-05-13 06:37:42 +00:00
type
ShortTuple = tuple[a: int, b: string]
ReallyLongTuple = tuple
wordyTupleMemberOne: string
wordyTupleMemberTwo: int
wordyTupleMemberThree: double
```
- Similarly, any procedure type declarations that are longer than one line
should be formatted in the style of a regular type.
2015-01-11 18:41:51 +00:00
2014-05-23 14:33:36 +00:00
```nimrod
2014-05-13 06:37:42 +00:00
type
EventCallback = proc (
2015-01-11 18:33:41 +00:00
timeRecieved: Time
2014-05-13 06:37:42 +00:00
errorCode: int
event: Event
)
```
- Multi-line procedure declarations/argument lists should continue on the same
2014-05-23 14:33:36 +00:00
column as the opening brace. This style is different from that of procedure
2015-01-11 18:41:51 +00:00
type declarations in order to distinguish between the heading of a procedure
and its body. If the procedure name is too long to make this style
2014-07-20 02:04:17 +00:00
convenient, then one of the styles for multi-line procedure calls (or
consider renaming your procedure).
2015-01-11 18:41:51 +00:00
2014-05-23 14:33:36 +00:00
```nimrod
2014-07-20 02:04:17 +00:00
proc lotsOfArguments(argOne: string, argTwo: int, argThree:float
2015-01-11 18:33:41 +00:00
argFour: proc(), argFive: bool): int
2015-01-22 16:08:34 +00:00
{.heyLookALongPragma.} =
2014-05-13 06:37:42 +00:00
```
2014-04-28 19:29:18 +00:00
2015-01-11 18:41:51 +00:00
- Multi-line procedure calls should either have one argument per line (like
multi-line type declarations) or continue on the same column as the opening
parenthesis (like multi-line procedure declarations). It is suggested that
the former style be used for procedure calls with complex argument
structures, and the latter style for procedure calls with simpler argument
structures.
2014-05-23 14:33:36 +00:00
```nimrod
# Each argument on a new line, like type declarations
# Best suited for 'complex' procedure calls.
readDirectoryChangesW(
directoryHandle.THandle,
buffer.start,
bufferSize.int32,
watchSubdir.WinBool,
filterFlags,
cast[ptr dword](nil),
2015-01-11 18:33:41 +00:00
cast[Overlapped](ol),
cast[OverlappedCompletionRoutine](nil)
2014-05-23 14:33:36 +00:00
)
# Multiple arguments on new lines, aligned to the opening parenthesis
# Best suited for 'simple' procedure calls
2015-01-06 12:34:17 +00:00
startProcess(nimExecutable, currentDirectory, compilerArguments
2014-05-23 14:33:36 +00:00
environment, processOptions)
2015-01-11 18:33:41 +00:00
```