From 4b64c213f8b65ee5b51ee5892dd937dd698b8d1c Mon Sep 17 00:00:00 2001 From: clawbot Date: Fri, 20 Mar 2026 07:06:03 +0100 Subject: [PATCH] style: strengthen constructor naming and Params struct rules (#19) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per sneak's instruction: - Constructors **must** be `New()`, `From()`, or `NewThing()` (multi-type packages only) - Strongly discourage creative names (`Create`, `Make`, `Build`, `Init`) - Constructors **must** use a `Params` struct (or `ThingParams`) for 2+ arguments — no exceptions - Single obvious argument (`ctx`, bytes) is the only exception - `context.Context` does not count against the argument limit (already documented) Co-authored-by: user Co-authored-by: clawbot Reviewed-on: https://git.eeqj.de/sneak/prompts/pulls/19 Co-authored-by: clawbot Co-committed-by: clawbot --- prompts/CODE_STYLEGUIDE_GO.md | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/prompts/CODE_STYLEGUIDE_GO.md b/prompts/CODE_STYLEGUIDE_GO.md index 4b0a1a9..043e8f1 100644 --- a/prompts/CODE_STYLEGUIDE_GO.md +++ b/prompts/CODE_STYLEGUIDE_GO.md @@ -1,6 +1,6 @@ --- title: Code Styleguide — Go -last_modified: 2026-02-22 +last_modified: 2026-03-18 --- 1. Try to hard wrap long lines at 77 characters or less. @@ -136,8 +136,15 @@ last_modified: 2026-02-22 1. Provide a .gitignore file that ignores at least `*.log`, `*.out`, and `*.test` files, as well as any binaries. -1. Constructors should be called `New()` whenever possible. `modulename.New()` - works great if you name the packages properly. +1. Constructors **must** be called `New()`. `modulename.New()` works great if + you name the packages properly. If the constructor creates an instance from + an existing value or representation, `From()` (e.g. + `FromBytes()`, `FromConfig()`) is also acceptable. If the package contains + multiple types and `New()` is ambiguous, `NewThing()` is occasionally + acceptable — but prefer restructuring packages so each type gets its own + package and a plain `New()`. Do not invent creative constructor names like + `Create()`, `Make()`, `Build()`, `Open()` (unless wrapping an OS resource), + or `Init()`. If you see a constructor with a non-standard name, rename it. 1. Don't make packages too big. Break them up. @@ -149,9 +156,15 @@ last_modified: 2026-02-22 1. Use descriptive names for modules and filenames. Avoid generic names like `server`. `util` is banned. -1. Constructors should take a Params struct if they need more than 1-2 - arguments. Positional arguments are an endless source of bugs and should be - avoided whenever possible. +1. Constructors **must** take a `Params` struct (or `ThingParams` when + `NewThing()` is used), even for a single argument. Named fields in a Params + struct are always clearer than positional arguments. Positional arguments + for constructors are an endless source of bugs — they make call sites + unreadable, invite wrong-order errors that the compiler can't catch when + types coincide, and force every caller to update when a new field is added. + The only exception is when the single argument is stupidly obvious from + context — e.g. `featureflag.New(true)` or `thing.NewFromReader(r)`. When in + doubt, use a Params struct. 1. Use `context.Context` for all functions that need it. If you don't need it, you can pass `context.Background()`. Anything long-running should get and