AGENT SKILL

writing-clean-code

Use when refactoring or restructuring code, splitting a file or module that has grown too large, deciding whether to extract a helper or inline it, naming things, removing dead or unused code, drawing module or package boundaries, handling errors, writing comments, applying DDD or hexagonal (ports and adapters) architecture, or checking a language's idiomatic style.

License 许可
MIT
所属仓库
rahulmutt/devkit
最近更新
2026-10-19
Stars
★ 0
Forks
0

01 前言 Front Matter

name
writing-clean-code
description
Use when refactoring or restructuring code, splitting a file or module that has grown too large, deciding whether to extract a helper or inline it, naming things, removing dead or unused code, drawing module or package boundaries, handling errors, writing comments, applying DDD or hexagonal (ports and adapters) architecture, or checking a language's idiomatic style.

02 资源文件 6

03 SKILL.md 正文

Writing Clean Code

Code should be easy for both humans and coding agents to maintain and evolve long-term. Optimize every authoring decision for the next reader, who is often an agent with a bounded context window — legibility and locality serve both audiences at once.

Principles — each with its counter-pull

Each principle names the failure it prevents and the over-application it guards against, so neither humans nor agents apply it mechanically.

Abstract real duplication, not speculative duplication

DRY when the same decision repeats; tolerate incidental similarity. Prefer the rule of three over premature generalization — wait for a third repetition before reaching for an abstraction. Counter-pull: a wrong abstraction costs more than the duplication it removed.

Inline the single-use and simple; name the reused or complex

Collapse a variable or helper used once that adds no clarifying value; extract when it is reused or when the name documents intent. The test is does the name earn its keep, not line count.

One purpose per unit

A module, function, or file should have a single reason to change. Growing size is the smell that surfaces a violation, not the violation itself.

Prefer small, modular units

Compose behavior from many small, independently replaceable units rather than a few large coupled ones — a unit you can understand, test, and swap without holding the rest in your head. Counter-pull: don't shatter one coherent behavior into a dozen trivial units that only ever change together — premature decomposition couples through indirection what was clear inline.

Domain-driven design

Name things in the domain's language; let module boundaries follow domain boundaries (bounded contexts), not technical layers alone. Counter-pull: don't impose heavy tactical DDD patterns on a thin or CRUD domain.

Explicit boundaries and dependency direction

Sub-components talk through narrow, well-defined interfaces; dependencies point inward toward the domain. See references/hexagonal-architecture.md.

Optimize for the next reader (human or agent)

Locality of behavior, explicit over clever, names that make comments redundant. Counter-pull: don't chase brevity at the cost of legibility — a terse line the next reader must decode is not optimizing for them.

Handle errors where you can act on them

Validate at the boundary, then let the core trust its inputs. Catch an error only where something useful can happen — retry, fall back, add context, or report it to the user — and otherwise let it propagate with context attached. Never swallow an error silently. Counter-pull: don't wrap every call in defensive try/catch or re-validate data the boundary already checked; noise hides the handling that matters.

Comments say why, code says what

Comment the non-obvious — the reason, the constraint, the rejected alternative, the workaround's upstream issue. Counter-pull: a comment restating the code is a second copy that drifts; rename or restructure until it's redundant, then delete it.

Prefer small, isolated files

One unit per file; keep files small enough to hold in a context window, and isolated enough that one file's internals aren't entangled with another's. Counter-pull: don't fragment so far that following one behavior means opening ten files — locality matters as much as size.

Every line is a liability

Every line added is a line to read, test, and maintain — the cheapest code is the code you don't write, so reach for the simplest thing that works (YAGNI). When code is reachable from no code path and you are absolutely sure, delete it; dead code misleads the next reader. Counter-pull: "unused" is easy to get wrong — check for reflection, dynamic dispatch, public API surface, feature flags, and external callers before deleting. When unsure, leave it and flag it.

Coding style

Style is mostly language-specific — follow the language's canonical style guide and let its formatter and linter be the source of truth, rather than hand-policing what a formatter already enforces. See the per-language references below for the canonical guide, formatter, linter, and key idioms for each supported language.

Boundaries with sibling skills

  • Formatter / linter installation → developer-environment (mise-pinned, devenv.nix fallback). This skill names the style guide and idiom and never gives install commands.
  • Counterpart skill for validating code: testing-practices (this skill is the authoring counterpart).

References

04 同仓库更多 Skills

rahulmutt
rahulmutt/devkit

using-devkit

Use when starting any conversation or new engineering task in a repo with devkit installed — establishes that devkit skills exist and must be checked before acting.

★ 0
rahulmutt
rahulmutt/devkit

testing-practices

Use when writing, adding, or reviewing tests; choosing between unit, integration, snapshot/golden, property, fuzz, mutation, e2e/UI, or formal methods; when a test is flaky or the suite or CI is slow; or when deciding what runs in pre-commit, PR CI, or nightly.

★ 0
rahulmutt
rahulmutt/devkit

security-practices

Use when code touches a trust boundary — HTTP endpoints, webhooks, file uploads, auth, sessions, permissions, SQL or shell built from input, deserialization, service-to-service calls — when handling secrets, API keys, or .env files, on a CVE or vulnerability alert, when writing a threat model, or when choosing secret, SCA, SAST, or container scanners.

★ 0
rahulmutt
rahulmutt/devkit

navigable-codebases

Use when writing or updating a README, AGENTS.md, CLAUDE.md, GEMINI.md, ARCHITECTURE.md, or onboarding docs; when setting up a new repository; when adding or naming task-runner commands (mise tasks, Makefile, justfile, npm scripts); or when getting-started steps are missing, stale, or slow for newcomers.

★ 0
rahulmutt
rahulmutt/devkit

developer-environment

Use when installing or upgrading any tool, runtime, CLI, or library — brew, apt, npm i -g, pip install, cargo install, nvm, pyenv, asdf — editing mise.toml or .tool-versions, bumping or pinning versions, adding a dependency, hitting "command not found", setting up a dev or CI toolchain, reviewing Renovate or Dependabot version bumps, or considering Bazel.

★ 0
rahulmutt
rahulmutt/devkit

commits-and-releases

Use when writing commit messages or splitting work into commits; when cutting, versioning, or tagging a release; when editing a CHANGELOG or bumping a version number; when setting up or fixing release-please, semantic-release, or changesets; or when a release has the wrong version or is missing changelog entries.

★ 0