Community Code Simplification Best Practices
Comprehensive code simplification guide for AI agents and LLMs. Contains 47 rules across 8 categories, prioritized by impact from critical (context discovery, behavior preservation) to incremental (language idioms). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics.
Core Principles
- Context First: Understand project conventions before making any changes
- Behavior Preservation: Change how code is written, never what it does
- Scope Discipline: Focus on recently modified code, keep diffs small
- Clarity Over Brevity: Explicit, readable code beats clever one-liners
When to Apply
Reference these guidelines when:
- Simplifying or cleaning up recently modified code
- Reducing nesting, complexity, or duplication
- Improving naming and readability
- Applying language-specific idiomatic patterns
- Reviewing code for maintainability issues
Rule Categories by Priority
Quick Reference
1. Context Discovery (CRITICAL)
ctx-read-claude-md[blocked] - Always read CLAUDE.md before simplifyingctx-detect-lint-config[blocked] - Check for linting and formatting configsctx-follow-existing-patterns[blocked] - Match existing code style in file and projectctx-project-over-generic[blocked] - Project conventions override generic best practices
2. Behavior Preservation (CRITICAL)
behave-preserve-outputs[blocked] - Preserve all return values and outputsbehave-preserve-errors[blocked] - Preserve error messages, types, and handlingbehave-preserve-api[blocked] - Preserve public function signatures and typesbehave-preserve-side-effects[blocked] - Preserve side effects (logging, I/O, state changes)behave-no-semantics-change[blocked] - Forbid subtle semantic changesbehave-verify-before-commit[blocked] - Verify behavior preservation before finalizing
3. Scope Management (HIGH)
scope-recent-code-only[blocked] - Focus on recently modified code onlyscope-minimal-diff[blocked] - Keep changes small and reviewablescope-no-unrelated-refactors[blocked] - No unrelated refactorsscope-no-global-rewrites[blocked] - Avoid global rewrites and architectural changesscope-respect-boundaries[blocked] - Respect module and component boundaries
4. Control Flow Simplification (HIGH)
flow-early-return[blocked] - Use early returns to reduce nestingflow-guard-clauses[blocked] - Use guard clauses for preconditionsflow-no-nested-ternaries[blocked] - Never use nested ternary operatorsflow-explicit-over-dense[blocked] - Prefer explicit control flow over dense expressionsflow-flatten-nesting[blocked] - Flatten deep nesting to maximum 2-3 levelsflow-single-responsibility[blocked] - Each code block should do one thingflow-positive-conditions[blocked] - Prefer positive conditions over double negativesflow-optional-chaining[blocked] - Use optional chaining and nullish coalescingflow-boolean-simplification[blocked] - Simplify boolean expressions
5. Naming and Clarity (MEDIUM-HIGH)
name-intention-revealing[blocked] - Use intention-revealing namesname-nouns-for-data[blocked] - Use nouns for data, verbs for actionsname-avoid-abbreviations[blocked] - Avoid cryptic abbreviationsname-consistent-vocabulary[blocked] - Use consistent vocabulary throughoutname-avoid-generic[blocked] - Avoid generic namesname-string-interpolation[blocked] - Prefer string interpolation over concatenation
6. Duplication Reduction (MEDIUM)
dup-rule-of-three[blocked] - Apply the rule of threedup-no-single-use-helpers[blocked] - Avoid single-use helper functionsdup-extract-for-clarity[blocked] - Extract only when it improves claritydup-avoid-over-abstraction[blocked] - Prefer duplication over premature abstractiondup-data-driven[blocked] - Use data-driven patterns over repetitive conditionals
7. Dead Code Elimination (MEDIUM)
dead-remove-unused[blocked] - Delete unused code artifactsdead-delete-not-comment[blocked] - Delete code, never comment it outdead-remove-obvious-comments[blocked] - Remove comments that state the obviousdead-keep-why-comments[blocked] - Keep comments that explain why, not whatdead-remove-todo-fixme[blocked] - Remove stale TODO/FIXME comments
8. Language Idioms (LOW-MEDIUM)
idiom-ts-strict-types[blocked] - Use strict types over any (TypeScript)idiom-ts-const-assertions[blocked] - Use const assertions and readonly (TypeScript)idiom-rust-question-mark[blocked] - Use ? for error propagation (Rust)idiom-rust-iterator-chains[blocked] - Use iterator chains when clearer (Rust)idiom-python-comprehensions[blocked] - Use comprehensions for simple transforms (Python)idiom-go-error-handling[blocked] - Handle errors immediately (Go)idiom-prefer-language-builtins[blocked] - Prefer language and stdlib builtins
Workflow
- Discover context: Read CLAUDE.md, lint configs, examine existing patterns
- Identify scope: Focus on recently modified code unless asked to expand
- Apply transformations: Use rules in priority order (CRITICAL first)
- Verify behavior: Ensure outputs, errors, and side effects remain identical
- Keep diffs minimal: Small, focused changes that are easy to review
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions [blocked] - Category structure and impact levels
- Rule template [blocked] - Template for adding new rules


