agentleFS
SparduttiSpardutti/claude-skills/skills/code-structure/SKILL.md

code-structure

Skill3 starsChanged 3 months agoMITView on GitHub

MUST USE when creating or editing any code file in any language — deciding how to split files, functions, classes, and modules, or whether to extract a shared helper, base class, or utility from duplicated code. Enforces the Single Responsibility Principle (one reason to change, file/function size limits, separation of concerns) and Avoid-Hasty-Abstractions (AHA / Rule of Three — prefer duplication over the wrong abstraction).

8 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use evidence-driven-testing, greploop-apps and 3 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.
  • kellykampenSkill

    evidence-driven-testing

    Records visual proof while testing UI behavior — screen recording with structured test/assertion annotations — then posts the video and a results summary to the PR and tracker issue. Use whenever a UI change needs verifiable evidence that it works, instead of prose claims.

    Also in 4 projects that use this

  • michaelshimelesSkill

    greploop-apps

    Iteratively improves a PR (GitHub), MR (GitLab), or shelved changelist (Perforce) until Greptile gives it a 5/5 confidence score with zero unresolved comments. Identical to greploop, but triggers reviews by tagging @greptile-apps, which bypasses Greptile's file-count limit on huge PRs that the plain @greptile mention refuses to review. Use when the user wants to fully optimize a large PR/MR/CL against Greptile's code review standards.

    Also in 3 projects that use this

  • VercelSkill

    before-and-after

    Add existing screenshots or screen recordings to a GitHub pull request as a before/after or preview block. Use when a PR needs visual media attached to its description. Browser navigation and capture belong to agent-browser.

    Also in 3 projects that use this

  • undeadlistSkill

    new-feature

    Develop new features with test-first approach (TDD)

    Also in 3 projects that use this

  • MicrosoftSkill

    unslop

    Cut AI tells from any writing. Must always apply.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 8 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    All callers would break together, for the same reason, if the logic were wrong.

    line 56
  2. Distinctive:

    Inline a wrong abstraction: before trying to "fix" it with more parameters.

    line 91

“Common” is measured: kept by at least 6 in 10 of the 8 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

Code Structure — Responsibility & Abstraction

Two complementary disciplines decide how code is shaped. They pull in opposite directions, and good structure is the balance between them:

  • Single Responsibility (SRP) — when to split. One unit, one job, one reason to change.
  • Avoid Hasty Abstractions (AHA) — when not to extract. Duplication is cheap; the wrong abstraction is expensive.

The synthesis: split on responsibility, never on shape or line count alone. SRP tells you to separate two jobs that happen to sit in one file; AHA tells you not to merge two blocks that merely look alike.

Quick Reference — When to Load What

Working on…Read
Splitting a file/function/class, separating concerns, file sizeSINGLE-RESPONSIBILITY.md
Tempted to extract a helper/base class/utility from duplicated codeAVOID-ABSTRACTIONS.md

Hard Limits

Concrete, enforceable thresholds. Crossing one is a signal to split — not an automatic split, but a prompt to check for a second responsibility.

MetricMaxAction
File length200 linesSplit into smaller modules
Function/method body30 linesExtract helpers
Function parameters3Use an options/config object
Nesting depth3 levelsEarly returns or extracted helpers
Cyclomatic complexity5 branchesSimplify or split
Class dependencies5 importsThe class knows too much — split

The Split Test (SRP)

Describe the unit in one sentence. If you need "and", it has more than one responsibility.

"fetches the orders"                              → one job
"fetches the orders AND formats them for export"  → two jobs — split

A unit has one reason to change when it serves one actor. If the business team and the ops team would both edit the same file for unrelated reasons, that file has two responsibilities. See SINGLE-RESPONSIBILITY.md for separation of concerns (computation vs side effects, query vs command, policy vs mechanism).

The Extraction Test (AHA)

Similar-looking code is not a reason to extract. Before pulling out a shared helper, all of these must hold:

  1. The pattern appears 3+ times with identical intent — not just identical shape.
  2. You can name it for what it does, not how (calculateTax, not processWithFlags).
  3. No caller needs a boolean or mode parameter to bend it to their case.
  4. All callers would break together, for the same reason, if the logic were wrong.
# BAD: extracted on shape — a flag now leaks caller-specific behavior
def send(user, kind):
    if kind == "welcome": ...
    elif kind == "reset": ...

# GOOD: two purpose-named functions; duplication is a vocabulary, not debt
def send_welcome(user): ...
def send_password_reset(user): ...

Every boolean parameter hides two functions in a trench coat. If extraction needs a flag, you found two functions, not one abstraction. See AVOID-ABSTRACTIONS.md for the failure pattern and how to undo a wrong abstraction.

When NOT to Split

The two forces meet here. Keep code together when:

  • Two things always change together and serve the same actor (verify with git log, not vibes).
  • Splitting would add indirection with no testability gain.
  • The unit is under ~30 lines and reads clearly as-is.
  • Code is merely coincidentally similar — same shape, different intent. Leave it duplicated.

Rules

  1. Describe a unit in one sentence without "and" — if you can't, it has two jobs; split it.
  2. Separate computation from side effects, and queries from commands (CQS).
  3. Never exceed 200 lines/file or 30 lines/function — split before crossing, not after.
  4. Never nest deeper than 3 levels — use early returns.
  5. Never mix levels of abstraction in one function.
  6. Never extract on the second duplication — wait for the third, and only if intent (not just shape) matches.
  7. Never add a boolean/mode parameter to make a shared helper fit a new caller — split into purpose-named functions.
  8. Never extract a helper used in exactly one place.
  9. Split on responsibility, never on line count or visual similarity alone.
  10. Inline a wrong abstraction before trying to "fix" it with more parameters.
  11. Don't over-split — only split when there is a real second responsibility.

Reference Files

  • SINGLE-RESPONSIBILITY.md — read before writing a new file, function, class, or module, and when deciding how to split an existing one. The question is "how big may this get?", not "is this already too big?". Covers the hard-limit thresholds, separation of concerns (computation vs side effects, query vs command, policy vs mechanism), one-resource-per-class, levels of abstraction, early returns, pipeline-over-monolith, file organization, the SRP smell tests, and when not to split.
  • AVOID-ABSTRACTIONS.md — read when tempted to extract a helper, base class, or shared utility from duplicated code. Covers the Rule of Three, the boolean-flag failure pattern, BAD/GOOD pairs (mode flags, inheritance misuse, single-use helpers, over-generic utilities), shape vs intent, when extraction is correct, and how to undo a wrong abstraction.

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.