-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Best practices
Danil Yarantsev edited this page Mar 18, 2021
·
7 revisions
This list is a community-maintained guide for some "best practice" suggestions. Written in the TLDR style, suggestions are in no particular order, should be beneficial to users with any level of Nim proficiency.
- Use 2 spaces indentation.
- Use
funcwhere possible. - Use
autoinstead ofany. - Use
selfinstead ofthisfor specifying the main object argument in routines. - Use
constandletwhere possible. - Put your types near the top of the file.
- Use
importfor public code instead ofinclude. - Use
includefor unittesting private code instead ofimport. -
Use testament instead of
unittest. - Use
runnableExamplesfor code examples in the documentation. - Use
tupleto return multiple values of different types. - Standard library imports should be prefixed with
std/, likestd/os,std/[strutils, sequtils], etc. - Use
when isMainModule:to specify code that'll only run if the source file is the main one. - Design your code to work in-place, then use
sugar.dupif you need to call it out-of-place. - Prefer in-place functions, for example,
sortinstead ofsortedwhere appropriate. - Use
optionsfor optional types and optional returns,Optionis lightweight. - Use
NaturalorPositiveas an argument or input type for non-negative integers. - Use
charto operate on single characters, for example"foo" & '\n'instead of"foo" & "\n". - Annotating routines with
{.deprecated.}will make the compiler print out all places where that routine is called. - Procedures like
echo,repr,astToStr,assert,expandMacrosand the compiler switch--expandArccan be useful for debugging. - In macros prefer to operate on the AST instead of using
parseStmtand string operations. - For designing new Macros, write the code you want to generate inside a
dumpAstGenblock and check the macro printed. - Use
stringto represent strings, ASCII or Unicode, that are not raw binary blobs. - Use
seq[byte]to represent raw binary blobs, that are not strings ASCII or Unicode. - Use
funcandproceven for OOP, do not usemethodbecause most code can be done withoutmethod. - Use
nimble initor a "repo template" to start a new project. - Use code comments with
FIXME,NOTE,OPTIMIZE,TODOso IDE, editors and GitHub Actions can read them. - For documentation it is generally advised to use descriptive third person present tense with proper punctuation.
- For documentation the first paragraph of a doc comment must be a Summary, it must concisely define purpose and functionality.
- Do NOT use
distinct tuple. - Do NOT use
floatfor money. - Do NOT use
discardon aFuture. - Do NOT use all caps on error messages.
- Do NOT add unused label names to
block. - Do NOT use exclamation marks on error messages.
- Do NOT use variable names on all uppercase, unless it is for FFI purposes.
- Do NOT use
NaturalnorPositiveas return type for integers, useintoruintas return type instead. - Do NOT use
tryblocks with lots ofexceptbranches, use explicit control flow instead. - Do NOT repeat
&too much to concatenate strings, useaddinstead. - Do NOT repeat
&too much to format strings, usestrformatinstead. - Do NOT hardcode
randomize()when coding API libs withrandom, let users call it, to avoid repeated calls torandomize().
See also:
Intro
Getting Started
- Install
- Docs
- Curated Packages
- Editor Support
- Unofficial FAQ
- Nim for C programmers
- Nim for Python programmers
- Nim for TypeScript programmers
- Nim for D programmers
- Nim for Java programmers
- Nim for Haskell programmers
Developing
- Build
- Contribute
- Creating a release
- Compiler module reference
- Consts defined by the compiler
- Debugging the compiler
- GitHub Actions/Travis CI/Circle CI/Appveyor
- GitLab CI setup
- Standard library and the JavaScript backend
Misc