agentleFS
jmxt3jmxt3/gitscape.ai/.agents/skills/code-simplification/SKILL.md

code-simplification

Skill33 starsChanged 4 months agoApache-2.0View on GitHub

Simplifies code for clarity. Use when refactoring code for clarity without changing behavior. Use when code works but is harder to read, maintain, or extend than it should be. Use when reviewing code that has accumulated unnecessary complexity.

We split this skill into the rules it gives an agent

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

  • 0

    Common

    Most of the 72 edited versions we read keep these.

  • 8

    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:

    Before simplifying any code, answer: why was it written this way?

    line 27
  2. Distinctive:

    Read the function end-to-end

    line 33
  3. Distinctive:

    Read its callers — what do they expect?

    line 34
  4. Distinctive:

    Read its tests — what behavior is verified?

    line 35
  5. Very specific:

    Check git history: git log -p -- path/to/file.py

    line 36
  6. Distinctive:

    If the code lacks tests, add tests before simplifying.

    line 41
  7. Distinctive:

    Run them, confirm they pass.

    line 50
  8. Distinctive:

    Run the full test suite after each simplification:

    line 169
  9. Distinctive:

    If any test fails, revert that specific simplification and reconsider.

    line 175

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

Overview

Reduce complexity while preserving exact behavior. The goal is code that a new engineer can read and understand without asking the author questions. Simplification is not optimization — do not change performance characteristics unless that is the explicit goal.

When to Use

  • Code works but is harder to read than it should be
  • A function is doing more than one thing
  • Nested conditionals make the control flow hard to follow
  • Variable names are generic (data, result, temp)
  • The same logic appears in multiple places
  • A reviewer needed to ask what the code does

When NOT to use: Code that is intentionally complex for performance reasons (document with a comment instead of simplifying).

The Chesterton's Fence Rule

"Don't remove a fence you don't understand."

Before simplifying any code, answer: why was it written this way? If you can't answer that, you risk removing a safeguard. Read the git history, grep for related tests, and understand the intent before changing anything.

The Simplification Process

Step 1: Understand Before Touching

  • Read the function end-to-end
  • Read its callers — what do they expect?
  • Read its tests — what behavior is verified?
  • Check git history: git log -p -- path/to/file.py
  • Do not change anything yet

Step 2: Cover First

If the code lacks tests, add tests before simplifying. You need a safety net:

def test_generate_skill_name_sanitizes_input():
    assert generate_skill_name("my repo") == "my-repo"
    assert generate_skill_name("My Repo!") == "my-repo"
    assert generate_skill_name("  spaces  ") == "spaces"

Run them, confirm they pass. Now you can simplify safely.

Step 3: Apply Simplifications Incrementally

Apply one simplification at a time, running tests after each:

Deep Nesting → Guard Clauses

# BAD: Pyramid of doom
def process_skill(skill):
    if skill is not None:
        if skill.get("name"):
            if len(skill["name"]) > 0:
                return skill["name"].lower()
        return None
    return None

# GOOD: Guard clauses
def process_skill(skill):
    if not skill:
        return None
    if not skill.get("name"):
        return None
    return skill["name"].lower()

Long Functions → Extracted Helpers

# BAD: One function doing three jobs
def export_skill(repo: str) -> bytes:
    # Fetch files
    files = []
    for branch in ["main", "master"]:
        try:
            contents = github_client.get_contents(repo, branch)
            files = contents
            break
        except Exception:
            continue
    
    # Filter relevant files
    relevant = [f for f in files if f.path.endswith((".md", ".py", ".ts"))]
    
    # Build zip
    buf = io.BytesIO()
    with zipfile.ZipFile(buf, "w") as zf:
        for f in relevant:
            zf.writestr(f.path, f.decoded_content)
    return buf.getvalue()

# GOOD: Each function does one thing
def export_skill(repo: str) -> bytes:
    files = fetch_repo_files(repo)
    relevant = filter_skill_files(files)
    return build_zip(relevant)

def fetch_repo_files(repo: str) -> list:
    for branch in ["main", "master"]:
        try:
            return github_client.get_contents(repo, branch)
        except Exception:
            continue
    return []

def filter_skill_files(files: list) -> list:
    return [f for f in files if f.path.endswith((".md", ".py", ".ts"))]

def build_zip(files: list) -> bytes:
    buf = io.BytesIO()
    with zipfile.ZipFile(buf, "w") as zf:
        for f in files:
            zf.writestr(f.path, f.decoded_content)
    return buf.getvalue()

Generic Names → Descriptive Names

# BAD
def process(d, r):
    result = []
    for item in d:
        if item["type"] == r:
            result.append(item)
    return result

# GOOD
def filter_files_by_type(files: list[dict], file_type: str) -> list[dict]:
    return [f for f in files if f["type"] == file_type]

Duplicated Logic → Shared Functions

# BAD: Same validation in three places
def create_skill(name: str):
    if not name or len(name) > 100:
        raise ValueError("Invalid name")
    ...

def update_skill(name: str):
    if not name or len(name) > 100:
        raise ValueError("Invalid name")
    ...

# GOOD
def validate_skill_name(name: str) -> None:
    if not name or len(name) > 100:
        raise ValueError("Skill name must be 1-100 characters")

def create_skill(name: str):
    validate_skill_name(name)
    ...

Step 4: Verify Nothing Changed

Run the full test suite after each simplification:

pytest tests/ -v

If any test fails, revert that specific simplification and reconsider. Simplification must not change behavior.

Step 5: Review the Result

After simplifying, ask:

  • Can a new engineer read this function without asking questions?
  • Is the control flow obvious?
  • Does every name say what the thing is, not what it does mechanically?
  • Are there any functions over 30 lines that could be split?

The Rule of 500

A file over 500 lines is a sign it's doing too much. Consider splitting. For GitScape:

  • api/app/skill_builder.py — if over 500 lines, split into name_sanitizer.py, file_filter.py, assembler.py
  • web/components/SkillExport.tsx — if over 500 lines, split by concern

Common Rationalizations

RationalizationReality
"I understand the code"You wrote it. Someone else reads it cold at 11 PM during an incident.
"Extracting functions adds overhead"Modern runtimes inline trivially. The overhead is unmeasurable. The clarity is immediate.
"The code is complex because the problem is complex"Complex problems have simple solutions and complex solutions. Always look for the simpler one.
"I shouldn't touch it — it works"Working code that no one understands becomes legacy code that no one can change safely.

Red Flags

  • Functions over 30 lines
  • Nesting deeper than 3 levels
  • Variables named data, result, temp, info
  • Comments that explain what the code does (rewrite instead)
  • Duplicated logic (copy-paste is a red flag)
  • Boolean parameters that change the function's behavior (create_skill(name, dry_run=True))

Verification

After simplification:

  • All tests still pass (no behavior change)
  • The diff is a net reduction in lines (usually)
  • No function is over 30 lines
  • No variable is named data, result, or temp
  • Duplicated logic has been extracted to a shared function
  • No comments that explain what the code does (only why)

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.