agentleFS
bmarshall511bmarshall511/Trellis/.claude/skills/clean-code/SKILL.md

clean-code

Skill0 starsChanged 3 months agoMITView on GitHub

Use when writing or reviewing any code, when deciding whether to extract a shared abstraction, when a file is growing large, or when the same logic appears in more than one place.

  • Deletes or force-pushes

108 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use intelligent-routing, nextjs-react-expert and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • SKB3002Skill

    intelligent-routing

    Automatic agent selection and intelligent task routing. Analyzes user requests and automatically selects the best specialist agent(s) without requiring explicit user mentions.

    Also in 5 projects that use this

  • SKB3002Skill

    nextjs-react-expert

    React and Next.js performance optimization from Vercel Engineering. Use when building React components, optimizing performance, eliminating waterfalls, reducing bundle size, reviewing code for performance issues, or implementing server/client-side optimizations.

    Also in 3 projects that use this

  • xfstudioSkill

    bash-pro

    Master of defensive Bash scripting for production automation, CI/CD

    Also in 4 projects that use this

  • xfstudioSkill

    code-refactoring-tech-debt

    You are a technical debt expert specializing in identifying, quantifying, and prioritizing technical debt in software projects. Analyze the codebase to uncover debt, assess its impact, and create acti

    Also in 4 projects that use this

  • flying-diceSkill

    clean-code-review

    Multi-agent clean code audit — each principle gets its own agent

    Also in 3 projects that use this

  • goca-seSkill

    batch-operations

    Apply a change safely across many files — refactors touching more than 5 files, library or API migrations, project-wide renames of symbols/imports/paths, or adding boilerplate to many files. Use when "do it all at once" risks context overflow or silent failures.

    Also in 3 projects that use this

  • +4 more from xfstudio

The file

We split this skill into the rules it gives an agent

10 rules. 0 are common, 9 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 101 edited versions we read keep these.

  • 9

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Three occurrences, then extract: Not two.

    line 13
  2. Distinctive:

    Ask: *when this changes, must every copy change together?* Yes means it is one thing.

    line 26
  3. Distinctive:

    When you catch yourself writing a list of things that count, ask what they have in common, and encode that instead.

    line 56
  4. Very specific:

    No abbreviations except genuinely universal ones.

    line 70
  5. Distinctive:

    Never let a name become inaccurate.

    line 72
  6. Distinctive:

    One reason to exist: If you cannot name a function without "and", it is doing two things.

    line 77
  7. Distinctive:

    Write a comment when the reason is not visible from the code: a non-obvious constraint, a workaround for a specific bug, a deliberate …

    line 94
  8. Distinctive:

    Do not write a comment that restates the line beneath it.

    line 97
  9. Distinctive:

    Dependencies point one way: When a feature imports from another feature which imports back, neither can be understood, tested, or removed …

    line 108
  10. Distinctive:

    Separate refactoring from behaviour change: Do one or the other in a change, never both — otherwise a reviewer cannot tell which edits were …

    line 133

“Common” is measured: kept by at least 6 in 10 of the 101 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Clean code

The measure is not elegance. It is whether the next person — often you, six months on, with none of today's context — can read it, change it safely, and know they didn't break something.

Duplication

Three occurrences, then extract. Not two.

Two similar pieces of code are frequently coincidence. They look alike today and diverge next week, because they answer to different requirements. Extract at two and you get an abstraction with a boolean parameter, then two, then a shape nobody can follow that serves neither caller well.

The wrong abstraction costs more than the duplication it replaced, because duplication is easy to delete and a bad abstraction is load-bearing.

Duplicated knowledge is different from duplicated text. A tax rate, a validation rule, a status name — one authoritative definition, always, from the first occurrence. Two functions that happen to have the same five lines but answer to different rules are not duplication.

Ask: when this changes, must every copy change together? Yes means it is one thing. No means it is two things that currently look alike.

Enumerations go stale. Properties do not.

The most common way a correct-looking rule quietly stops being correct is that it was written as a list of the cases its author imagined, rather than as the property that actually distinguishes them.

The list is right on the day it is written. Then someone adds a case its author never pictured, and the rule silently stops covering it — while still reporting success, because from its own point of view nothing is wrong.

Three real examples from this codebase, all found by other people using it:

Written as a listThe property underneath
Six copies of a frontmatter parserThere is one way to read frontmatter
A list of two files an approval hashesEverything a design renders against is an input
A regex matching "load the x and y skills"Names are tokens; grammar varies
reset\s+--hard, requiring the two adjacentShell flags interleave in any order

The fourth is the one that cost most. git reset -q --hard is the same command as git reset --hard, but the pattern required the tokens to be adjacent, so the -q form ran unblocked and destroyed an hour of uncommitted work. The rule had been tested — against the one spelling its author typed.

The third is the clearest. The pattern had an optional group for a second name, which consumed it without capturing it — so it checked one of two references and reported that everything resolved. It handled exactly the sentence its author had in mind.

When you catch yourself writing a list of things that count, ask what they have in common, and encode that instead. When you genuinely cannot — some enumerations are irreducible — say where the list came from, and add a check that it has not drifted from its source.

The tell: you are extending a list to fix a bug. Extending it fixes today's case and leaves tomorrow's.

Naming

Naming is the highest-leverage thing in this document. Most unreadable code is well-structured code with names that lie.

  • Name what it is, not what it does internally. activeSubscribers, not filterUsersLoop
  • Say the unit: timeoutMs, priceInCents, maxRetries
  • Booleans read as assertions: isExpired, hasAccess, shouldRetry
  • No abbreviations except genuinely universal ones. res could be response, result, resource, or reserved
  • A long clear name beats a short cryptic one every time
  • Never let a name become inaccurate. A renamed concept must be renamed everywhere — a stale name is worse than no name, because it is trusted

Functions

One reason to exist. If you cannot name a function without "and", it is doing two things.

Keep the levels of abstraction consistent. A function that orchestrates three high-level steps should not also contain string parsing. Mixed levels are what makes code exhausting to read.

Return early. Handle the edge cases and get out. Deeply nested conditionals hide the main path.

Prefer arguments to hidden state. A function whose result depends only on what it was given can be tested, moved, and reasoned about. One that reads ambient state cannot.

Boolean parameters are a smell. render(true) tells the reader nothing. It usually means two functions wearing a trench coat.

Comments

Code says what. Comments say why.

Write a comment when the reason is not visible from the code: a non-obvious constraint, a workaround for a specific bug, a deliberate choice that looks wrong, an ordering that matters.

Do not write a comment that restates the line beneath it. Do not leave commented-out code — that is what version control is for. Do not write a comment that will go stale; if it must be true, assert it in a test.

The best comment is usually a better name.

Structure

Group by feature, not by kind. Everything belonging to one capability lives together. Directories named after technical layers force you to open five folders to understand one thing, and they make it impossible to see what a change touches.

Dependencies point one way. When a feature imports from another feature which imports back, neither can be understood, tested, or removed alone. Push shared things down into something both depend on.

Keep the public surface small. Export what callers need. Everything else stays private so it can be changed without an audit.

Errors

Fail loudly and early. An error swallowed silently becomes a bug report about wrong data six weeks later, with nothing to go on.

Handle it where you can do something about it. Catching an error only to log and rethrow adds noise. Catch it where there is a decision to make.

Error messages are for whoever reads them at 3am. Include what was being attempted and with what. "Request failed" is useless. Never expose internals to end users, and never hide them from logs.

Never catch everything and continue. A bare catch-all around a block hides the failure you most need to see.

Changing existing code

Match what is already there. Consistency beats your preference. A file written in one style and edited in another is harder to read than either style alone.

Separate refactoring from behaviour change. Do one or the other in a change, never both — otherwise a reviewer cannot tell which edits were meant to alter behaviour, and neither can you.

Leave it a little better. Fix the name you had to decipher. Do not rewrite the module while you are there — an unrelated large refactor buried in a small change is how regressions ship.

Delete dead code. Unreferenced functions, unused flags, commented-out blocks, TODOs from years ago. It is all in version control. Every line kept "just in case" is a line someone has to read and wonder about.

Reviewing your own work before calling it done

  • Would someone with no context understand this?
  • Does every name still mean what it says?
  • Is anything here duplicated knowledge rather than duplicated text?
  • Is there an abstraction extracted from only two cases?
  • Are the errors handled where something can be done about them?
  • Is there anything left in that is not being used?
  • Does this match the surrounding code?

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.