Nim code organization

From planetis-m/skills · View interactive version

Nim code organization is an AI agent skill from the GitHub repository planetis-m/skills. Organize Nim modules and multi-step workflows with explicit state ownership, cohesive boundaries, narrow exports, and structured orchestration. Use when refactoring a large Nim file, splitting logic across modules, designing parser-style stateful code, or simplifying nested helpers and hidden mutabl

How to use this skill

Skill file

Nim Code Organization

Choose the smallest structure that exposes the real workflow.

Rules

State ownership

a named object and pass it explicitly.

Helper placement

an invariant easier to state.

static[bool] or a static enum and branch with when.

calling convention is required.

Module boundaries

related types or pieces of state.

or reuse boundary.

Control flow

way to divide orchestration steps.

Incremental APIs

the object's fields so it survives between calls.

resource or ordering constraint spans calls; keep a one-shot operation a single proc.

assert s.opened) at the start of each next-style proc.

Workflow

  1. Trace the data flow.

Identify which values evolve, which steps share them, and which invariants must hold

between steps.

  1. Choose the smallest state scope.

Use locals for one clear proc, a plain object for operations preserving one invariant,

and a reference only for intentional identity or shared lifetime.

  1. Shape the helpers.

Extract meaningful operations. Keep local incidental logic near its caller.

  1. Choose module boundaries.

Group code by responsibility and contract, then split only at a real dependency,

lifecycle, or reuse boundary.

  1. Define the public surface.

Export the minimum caller-facing types and operations.

  1. Choose dispatch.

Use direct calls and case for closed behavior. Introduce runtime dispatch for an open

subtype-based design.

State Scope

| Situation | Default shape |

| --- | --- |

| One linear operation | Local values in one proc |

| Several operations share an invariant | Plain state object passed by var |

| One caller owns a short captured operation | Nested proc |

| Several phases share a named operation | Module-level proc |

| Identity or shared lifetime is required | ref object |

Common Mistakes

| Mistake | Why it is wrong |

| --- | --- |

| Hiding state shared by several phases in nested captures | The mutation and cross-phase invariants disappear from proc signatures |

| Creating a state type for a short linear calculation | The extra type adds ceremony without clarifying ownership or invariants |

| Extracting every small block into a top-level helper | The reader must jump between names that do not represent meaningful operations |

| Splitting modules by file size alone | File length does not identify a responsibility, dependency, lifecycle, or reuse boundary |

| Using a method hierarchy for a closed set of kinds | Static data plus case keeps the possible behaviors visible to the compiler and reader |

| Exporting orchestration helpers or state by default | It turns implementation details into a public compatibility obligation |

| Starting with ref object for locally owned state | It introduces aliasing and identity that the workflow does not require |

References

that share a stock invariant, contrasted with a local calculation.

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.