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
| Priority |
Category |
Impact |
Prefix |
Rules |
| 1 |
Context Discovery |
CRITICAL |
ctx- |
4 |
| 2 |
Behavior Preservation |
CRITICAL |
behave- |
6 |
| 3 |
Scope Management |
HIGH |
scope- |
5 |
| 4 |
Control Flow Simplification |
HIGH |
flow- |
9 |
| 5 |
Naming and Clarity |
MEDIUM-HIGH |
name- |
6 |
| 6 |
Duplication Reduction |
MEDIUM |
dup- |
5 |
| 7 |
Dead Code Elimination |
MEDIUM |
dead- |
5 |
| 8 |
Language Idioms |
LOW-MEDIUM |
idiom- |
7 |
Quick Reference
1. Context Discovery (CRITICAL)
ctx-read-claude-md - Always read CLAUDE.md before simplifying
ctx-detect-lint-config - Check for linting and formatting configs
ctx-follow-existing-patterns - Match existing code style in file and project
ctx-project-over-generic - Project conventions override generic best practices
2. Behavior Preservation (CRITICAL)
behave-preserve-outputs - Preserve all return values and outputs
behave-preserve-errors - Preserve error messages, types, and handling
behave-preserve-api - Preserve public function signatures and types
behave-preserve-side-effects - Preserve side effects (logging, I/O, state changes)
behave-no-semantics-change - Forbid subtle semantic changes
behave-verify-before-commit - Verify behavior preservation before finalizing
3. Scope Management (HIGH)
scope-recent-code-only - Focus on recently modified code only
scope-minimal-diff - Keep changes small and reviewable
scope-no-unrelated-refactors - No unrelated refactors
scope-no-global-rewrites - Avoid global rewrites and architectural changes
scope-respect-boundaries - Respect module and component boundaries
4. Control Flow Simplification (HIGH)
flow-early-return - Use early returns to reduce nesting
flow-guard-clauses - Use guard clauses for preconditions
flow-no-nested-ternaries - Never use nested ternary operators
flow-explicit-over-dense - Prefer explicit control flow over dense expressions
flow-flatten-nesting - Flatten deep nesting to maximum 2-3 levels
flow-single-responsibility - Each code block should do one thing
flow-positive-conditions - Prefer positive conditions over double negatives
flow-optional-chaining - Use optional chaining and nullish coalescing
flow-boolean-simplification - Simplify boolean expressions
5. Naming and Clarity (MEDIUM-HIGH)
name-intention-revealing - Use intention-revealing names
name-nouns-for-data - Use nouns for data, verbs for actions
name-avoid-abbreviations - Avoid cryptic abbreviations
name-consistent-vocabulary - Use consistent vocabulary throughout
name-avoid-generic - Avoid generic names
name-string-interpolation - Prefer string interpolation over concatenation
6. Duplication Reduction (MEDIUM)
dup-rule-of-three - Apply the rule of three
dup-no-single-use-helpers - Avoid single-use helper functions
dup-extract-for-clarity - Extract only when it improves clarity
dup-avoid-over-abstraction - Prefer duplication over premature abstraction
dup-data-driven - Use data-driven patterns over repetitive conditionals
7. Dead Code Elimination (MEDIUM)
dead-remove-unused - Delete unused code artifacts
dead-delete-not-comment - Delete code, never comment it out
dead-remove-obvious-comments - Remove comments that state the obvious
dead-keep-why-comments - Keep comments that explain why, not what
dead-remove-todo-fixme - Remove stale TODO/FIXME comments
8. Language Idioms (LOW-MEDIUM)
idiom-ts-strict-types - Use strict types over any (TypeScript)
idiom-ts-const-assertions - Use const assertions and readonly (TypeScript)
idiom-rust-question-mark - Use ? for error propagation (Rust)
idiom-rust-iterator-chains - Use iterator chains when clearer (Rust)
idiom-python-comprehensions - Use comprehensions for simple transforms (Python)
idiom-go-error-handling - Handle errors immediately (Go)
idiom-prefer-language-builtins - 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 - Category structure and impact levels
- Rule template - Template for adding new rules
Reference Files
| File |
Description |
| references/_sections.md |
Category definitions and ordering |
| assets/templates/_template.md |
Template for new rules |
| metadata.json |
Version and reference information |
1---2name: code-simplifier-23description: Community Code Simplification Best Practices4---5# Community Code Simplification Best Practices67Comprehensive 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.89## Core Principles10111. **Context First**: Understand project conventions before making any changes122. **Behavior Preservation**: Change how code is written, never what it does133. **Scope Discipline**: Focus on recently modified code, keep diffs small144. **Clarity Over Brevity**: Explicit, readable code beats clever one-liners1516## When to Apply1718Reference these guidelines when:19- Simplifying or cleaning up recently modified code20- Reducing nesting, complexity, or duplication21- Improving naming and readability22- Applying language-specific idiomatic patterns23- Reviewing code for maintainability issues2425## Rule Categories by Priority2627| Priority | Category | Impact | Prefix | Rules |28|----------|----------|--------|--------|-------|29| 1 | Context Discovery | CRITICAL | `ctx-` | 4 |30| 2 | Behavior Preservation | CRITICAL | `behave-` | 6 |31| 3 | Scope Management | HIGH | `scope-` | 5 |32| 4 | Control Flow Simplification | HIGH | `flow-` | 9 |33| 5 | Naming and Clarity | MEDIUM-HIGH | `name-` | 6 |34| 6 | Duplication Reduction | MEDIUM | `dup-` | 5 |35| 7 | Dead Code Elimination | MEDIUM | `dead-` | 5 |36| 8 | Language Idioms | LOW-MEDIUM | `idiom-` | 7 |3738## Quick Reference3940### 1. Context Discovery (CRITICAL)4142- [`ctx-read-claude-md`](references/ctx-read-claude-md.md) - Always read CLAUDE.md before simplifying43- [`ctx-detect-lint-config`](references/ctx-detect-lint-config.md) - Check for linting and formatting configs44- [`ctx-follow-existing-patterns`](references/ctx-follow-existing-patterns.md) - Match existing code style in file and project45- [`ctx-project-over-generic`](references/ctx-project-over-generic.md) - Project conventions override generic best practices4647### 2. Behavior Preservation (CRITICAL)4849- [`behave-preserve-outputs`](references/behave-preserve-outputs.md) - Preserve all return values and outputs50- [`behave-preserve-errors`](references/behave-preserve-errors.md) - Preserve error messages, types, and handling51- [`behave-preserve-api`](references/behave-preserve-api.md) - Preserve public function signatures and types52- [`behave-preserve-side-effects`](references/behave-preserve-side-effects.md) - Preserve side effects (logging, I/O, state changes)53- [`behave-no-semantics-change`](references/behave-no-semantics-change.md) - Forbid subtle semantic changes54- [`behave-verify-before-commit`](references/behave-verify-before-commit.md) - Verify behavior preservation before finalizing5556### 3. Scope Management (HIGH)5758- [`scope-recent-code-only`](references/scope-recent-code-only.md) - Focus on recently modified code only59- [`scope-minimal-diff`](references/scope-minimal-diff.md) - Keep changes small and reviewable60- [`scope-no-unrelated-refactors`](references/scope-no-unrelated-refactors.md) - No unrelated refactors61- [`scope-no-global-rewrites`](references/scope-no-global-rewrites.md) - Avoid global rewrites and architectural changes62- [`scope-respect-boundaries`](references/scope-respect-boundaries.md) - Respect module and component boundaries6364### 4. Control Flow Simplification (HIGH)6566- [`flow-early-return`](references/flow-early-return.md) - Use early returns to reduce nesting67- [`flow-guard-clauses`](references/flow-guard-clauses.md) - Use guard clauses for preconditions68- [`flow-no-nested-ternaries`](references/flow-no-nested-ternaries.md) - Never use nested ternary operators69- [`flow-explicit-over-dense`](references/flow-explicit-over-dense.md) - Prefer explicit control flow over dense expressions70- [`flow-flatten-nesting`](references/flow-flatten-nesting.md) - Flatten deep nesting to maximum 2-3 levels71- [`flow-single-responsibility`](references/flow-single-responsibility.md) - Each code block should do one thing72- [`flow-positive-conditions`](references/flow-positive-conditions.md) - Prefer positive conditions over double negatives73- [`flow-optional-chaining`](references/flow-optional-chaining.md) - Use optional chaining and nullish coalescing74- [`flow-boolean-simplification`](references/flow-boolean-simplification.md) - Simplify boolean expressions7576### 5. Naming and Clarity (MEDIUM-HIGH)7778- [`name-intention-revealing`](references/name-intention-revealing.md) - Use intention-revealing names79- [`name-nouns-for-data`](references/name-nouns-for-data.md) - Use nouns for data, verbs for actions80- [`name-avoid-abbreviations`](references/name-avoid-abbreviations.md) - Avoid cryptic abbreviations81- [`name-consistent-vocabulary`](references/name-consistent-vocabulary.md) - Use consistent vocabulary throughout82- [`name-avoid-generic`](references/name-avoid-generic.md) - Avoid generic names83- [`name-string-interpolation`](references/name-string-interpolation.md) - Prefer string interpolation over concatenation8485### 6. Duplication Reduction (MEDIUM)8687- [`dup-rule-of-three`](references/dup-rule-of-three.md) - Apply the rule of three88- [`dup-no-single-use-helpers`](references/dup-no-single-use-helpers.md) - Avoid single-use helper functions89- [`dup-extract-for-clarity`](references/dup-extract-for-clarity.md) - Extract only when it improves clarity90- [`dup-avoid-over-abstraction`](references/dup-avoid-over-abstraction.md) - Prefer duplication over premature abstraction91- [`dup-data-driven`](references/dup-data-driven.md) - Use data-driven patterns over repetitive conditionals9293### 7. Dead Code Elimination (MEDIUM)9495- [`dead-remove-unused`](references/dead-remove-unused.md) - Delete unused code artifacts96- [`dead-delete-not-comment`](references/dead-delete-not-comment.md) - Delete code, never comment it out97- [`dead-remove-obvious-comments`](references/dead-remove-obvious-comments.md) - Remove comments that state the obvious98- [`dead-keep-why-comments`](references/dead-keep-why-comments.md) - Keep comments that explain why, not what99- [`dead-remove-todo-fixme`](references/dead-remove-todo-fixme.md) - Remove stale TODO/FIXME comments100101### 8. Language Idioms (LOW-MEDIUM)102103- [`idiom-ts-strict-types`](references/idiom-ts-strict-types.md) - Use strict types over any (TypeScript)104- [`idiom-ts-const-assertions`](references/idiom-ts-const-assertions.md) - Use const assertions and readonly (TypeScript)105- [`idiom-rust-question-mark`](references/idiom-rust-question-mark.md) - Use ? for error propagation (Rust)106- [`idiom-rust-iterator-chains`](references/idiom-rust-iterator-chains.md) - Use iterator chains when clearer (Rust)107- [`idiom-python-comprehensions`](references/idiom-python-comprehensions.md) - Use comprehensions for simple transforms (Python)108- [`idiom-go-error-handling`](references/idiom-go-error-handling.md) - Handle errors immediately (Go)109- [`idiom-prefer-language-builtins`](references/idiom-prefer-language-builtins.md) - Prefer language and stdlib builtins110111## Workflow1121131. **Discover context**: Read CLAUDE.md, lint configs, examine existing patterns1142. **Identify scope**: Focus on recently modified code unless asked to expand1153. **Apply transformations**: Use rules in priority order (CRITICAL first)1164. **Verify behavior**: Ensure outputs, errors, and side effects remain identical1175. **Keep diffs minimal**: Small, focused changes that are easy to review118119## How to Use120121Read individual reference files for detailed explanations and code examples:122123- [Section definitions](references/_sections.md) - Category structure and impact levels124- [Rule template](assets/templates/_template.md) - Template for adding new rules125126## Reference Files127128| File | Description |129|------|-------------|130| [references/_sections.md](references/_sections.md) | Category definitions and ordering |131| [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules |132| [metadata.json](metadata.json) | Version and reference information |