-
-
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
func
where possible. - Use
auto
instead ofany
. - Use
self
instead ofthis
for specifying the main object argument in routines. - Use
const
andlet
where possible. - Put your types near the top of the file.
- Use
import
for public code instead ofinclude
. - Use
include
for unittesting private code instead ofimport
. -
Use testament instead of
unittest
. - Use
runnableExamples
for code examples in the documentation. - Use
tuple
to 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.dup
if you need to call it out-of-place. - Prefer in-place functions, for example,
sort
instead ofsorted
where appropriate. - Use
options
for optional types and optional returns,Option
is lightweight. - Use
Natural
orPositive
as an argument or input type for non-negative integers. - Use
char
to 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
,expandMacros
and the compiler switch--expandArc
can be useful for debugging. - In macros prefer to operate on the AST instead of using
parseStmt
and string operations. - For designing new Macros, write the code you want to generate inside a
dumpAstGen
block and check the macro printed. - Use
string
to 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
func
andproc
even for OOP, do not usemethod
because most code can be done withoutmethod
. - Use
nimble init
or a "repo template" to start a new project. - Use code comments with
FIXME
,NOTE
,OPTIMIZE
,TODO
so 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
float
for money. - Do NOT use
discard
on 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
Natural
norPositive
as return type for integers, useint
oruint
as return type instead. - Do NOT use
try
blocks with lots ofexcept
branches, use explicit control flow instead. - Do NOT repeat
&
too much to concatenate strings, useadd
instead. - Do NOT repeat
&
too much to format strings, usestrformat
instead. - 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