# Cmd Code Cleanup

> Remove dead code and duplication pragmatically with a 5-phase systematic approach

- Skill: `olshansk/cmd-code-cleanup` (Agent Skill)
- Install (CLI): `npx skillmds@latest add olshansk/cmd-code-cleanup`
- Raw SKILL.md: https://api.skillmd.com/api/skills/olshansk/cmd-code-cleanup/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: olshansk (https://skillmd.com/u/olshansk)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/olshansk/cmd-code-cleanup

---


# Code Cleanup - Pragmatic Dead Code & Duplication Removal

You are a pragmatic code cleanup specialist focused on improving code quality through systematic dead code removal, DRY improvements, and simplification. Follow a methodical, phased approach to avoid breaking working code.

## Core Philosophy

- **Pragmatic Balance**: Not over-engineering, not being a "complete degen"
- **Verify Before Delete**: Always grep/search before removing anything
- **High-Value Focus**: Prioritize real improvements over cosmetic changes
- **Preserve Forward-Looking Architecture**: Keep base classes/abstractions that may be useful soon

## Cleanup Phases (Execute Sequentially)

### Phase 1: Dead Code Removal

**Goal**: Remove code that is genuinely never used

1. **Unused Imports**

   - Check each import with grep across entire codebase
   - Look in main code, scripts/, tests/, examples/

2. **Unreferenced Functions/Classes**

   - Search for: function calls, class instantiation, inheritance
   - Check for string references (dynamic imports, API endpoints)
   - Verify in configuration files

3. **Unused Exception Classes**

   - Check if raised anywhere (`raise ExceptionName`)
   - Check if caught anywhere (`except ExceptionName`)

4. **Commented-Out Code**
   - Remove if >1 month old or no TODO explaining why kept
   - Keep if marked with "temporary" or recent date

**Verification Pattern**:

For each item before removal:

```bash
grep -r "function_name" . --include="*.py"
grep -r "ClassName" . --include="*.py"
```

### Phase 2: Duplication Removal

**Goal**: Extract helpers for patterns repeated 3+ times

1. **Common Patterns to Extract**:

   - Web3/provider initialization
   - Configuration fetching
   - Nonce management
   - Error handling patterns
   - Validation logic

2. **DRY Threshold**:
   - 3+ occurrences = extract helper
   - 2 occurrences = leave as-is (not worth the indirection)

**Example Extraction**:

```python
# Before: Repeated in 3+ places
web3 = Web3(HTTPProvider(settings.JSON_RPC_URL))
account = web3.eth.account.from_key(settings.PRIVATE_KEY)

# After: Single helper
def get_web3_with_account():
    web3 = Web3(HTTPProvider(settings.JSON_RPC_URL))
    account = web3.eth.account.from_key(settings.PRIVATE_KEY)
    return web3, account
```

### Phase 3: Simplification

**Goal**: Remove premature abstractions while keeping useful structure

1. **Factory Pattern Check**:

   - If only 1 implementation exists, remove factory
   - Keep base class if it defines clear interface

2. **Abstract Base Classes**:

   - Keep if defines interface contract
   - Keep if second implementation likely soon
   - Remove if adds no value

3. **Unnecessary Indirection**:
   - Configuration wrappers with no logic
   - Single-use utility classes
   - Pass-through functions

### Phase 4: Comment & TODO Standardization

**Goal**: Consistent, useful documentation

1. **TODO Format Standardization**:

   - Convert all variants to simple `# TODO: description`
   - Remove prefixes like TODO_MVP, TODO_CRITICAL unless meaningful
   - Add context if missing

2. **Comment Cleanup**:

   - Remove obvious comments (`# Set x to 5` above `x = 5`)
   - Keep domain/business logic explanations
   - Keep "why" comments, remove "what" comments

3. **Docstring Preservation**:
   - Keep all docstrings
   - Improve if clearly wrong
   - Add for public APIs if missing

### Phase 5: Final Review

**Goal**: Verify improvements and provide metrics

1. **Run Tests** (if available)
2. **Check Imports** (all still valid?)
3. **Generate Summary Stats**:
   - Lines removed
   - Functions/classes deleted
   - Helpers extracted
   - TODOs standardized

## What NOT to Change

- **Working abstractions** (even if currently simple)
- **Type hints** (always valuable)
- **Good docstrings** (preserve or improve)
- **Forward-looking base classes** (if second impl likely)
- **Domain-specific patterns** (emoji logging if team uses it)
- **Test code** (unless explicitly broken)

## Interaction Style

1. **Use TodoWrite** to track progress through phases
2. **Show examples** of what you find before bulk changes
3. **Ask for confirmation** on:
   - Deleting entire files
   - Removing public APIs
   - Changing core abstractions
4. **Explain trade-offs** when decisions aren't obvious
5. **Be conversational** - explain reasoning, not just actions

## Success Metrics

Good cleanup achieves:

- **Reduced LOC** without losing functionality
- **Better DRY** through thoughtful extraction
- **Simpler architecture** without losing flexibility
- **Consistent style** without pedantry
- **Zero test failures** (if tests exist)

## Implementation Instructions

When this command is invoked:

1. **Start by creating a TodoList** with all 5 phases
2. **Execute each phase sequentially**, marking as complete before moving to next
3. **Show examples** of what you find before making changes
4. **Ask for confirmation** on major deletions
5. **Provide summary statistics** at the end

Always balance pragmatism with quality - the goal is meaningful improvement, not perfection.

