Nim style guide

From planetis-m/skills · View interactive version

Nim style guide is an AI agent skill from the GitHub repository planetis-m/skills. Write clear, consistent Nim code in a simple stdlib-aligned style, covering imports, naming, proc vs func vs template choices, local variables, constructors, formatting, and control flow. Use when writing new Nim code or reviewing a Nim module for readability, consistency, and low-noise style decisi

How to use this skill

Skill file

Nim Style Guide

Rules

Formatting

when an operand contains an operator, as in a .. -3.

range[0..<n] is invalid.

Imports

Naming

name already exists, suffix the in-place form with In, as in replaceIn.

contains, add, cmp, len, cap, items, pairs, incl, and excl.

effects or is not O(1).

Proc, Func, Template, Macro

control-flow abstraction is required.

Calls, Locals, And Types

(TypeName(field: value)) over field-by-field assignment.

strings/seqs use ""/@[], default(T) as fallback.

Parsing-Sensitive Whitespace

These whitespace choices change how Nim parses code:

Line Continuation

Nim has no line-continuation character. Break expressions after an operator

or comma, or within open delimiters such as (), [], and {}. Indent the

continued line further than the statement that started it.

For constructs with a body, double indentation can distinguish continuation

lines from the body.

Multiline Strings

concatenation for multiline values.

value is identical; the content aligns with the next line.

See references/multiline_strings.md.

Control Flow

Workflow

  1. Pick the callable kind.

Start with proc. Switch to func for a useful purity contract, template

for call-site substitution, and macro for syntax transformation. Choose

method for runtime dispatch.

  1. Write imports and names.

Use std/... imports, narrow imports when practical, and keep names in normal Nim casing.

  1. Shape the control flow.

Use structured control flow. Return directly when a loop finds its result.

  1. Clean up locals and constructors.

Use let by default, keep locals near first use, and keep reusable helpers at module scope.

  1. Remove noise.

Remove unused imports, dead helpers, column alignment, and stretched call formatting.

Common Mistakes

| Mistake | Why it is wrong |

|---------|-----------------|

| Using method as the default callable kind | It adds runtime dispatch where a plain proc or func would usually be clearer. |

| Hiding reusable helpers inside another proc | It makes the helper harder to reuse and easier to turn into an accidental closure. |

| Writing one argument per line by default | It adds vertical noise without adding structure. |

| Using var for values that never mutate | It hides which locals actually change. |

| Assuming {.pure.} requires Type.value | Pure enum values may be unqualified. |

| Turning every branch into an early return in a multi-step proc | It makes the normal path harder to scan. |

| Using continue | A structured branch keeps the loop invariant visible. |

References

locals, and constructors.

dedenting.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.