$npx -y skills add tody-agent/codymaster --skill cm-clean-codeCode hygiene gate — detect and eliminate dead code, duplicates, naming mess, and code smells. TRIZ-powered. Run after features, before PRs, during debt sprints.
| 1 | # Clean Code — Code Hygiene Gate |
| 2 | |
| 3 | ## TL;DR |
| 4 | - **Use after** a feature lands, before PR review |
| 5 | - **Detects**: dead code, duplicates, naming smells, dependency drift |
| 6 | - **Powered by** TRIZ contradictions and Boy Scout rule |
| 7 | - **Next**: cm-code-review |
| 8 | |
| 9 | > **Code that works is not enough. Code must be CLEAN.** |
| 10 | > Inspired by Clean Code (Robert C. Martin) + Refactoring (Martin Fowler) + TRIZ. |
| 11 | |
| 12 | ## When to Use |
| 13 | |
| 14 | **ALWAYS when:** |
| 15 | - After completing a feature (mandatory hygiene pass before PR) |
| 16 | - After `cm-reactor` migration (cleanup dead code from migration) |
| 17 | - Before code review (`cm-code-review`) — clean FIRST, review AFTER |
| 18 | - During technical debt sprints |
| 19 | - When code smells are detected (see Detection section) |
| 20 | - After AI-generated code sessions (AI tends to leave mess) |
| 21 | - When file grows beyond 300 lines |
| 22 | |
| 23 | **Run automatically after:** |
| 24 | - `cm-execution` completes a task batch |
| 25 | - `cm-reactor` Phase 5 (post-migration cleanup) |
| 26 | - `cm-tdd` Refactor phase (Red → Green → **Refactor**) |
| 27 | |
| 28 | **Skip when:** |
| 29 | - Quick hotfix (patch first, clean later — but schedule the cleanup!) |
| 30 | - Prototype/spike code (will be thrown away) |
| 31 | |
| 32 | ## TRIZ Principles Applied |
| 33 | |
| 34 | | # | Principle | How Applied | |
| 35 | |---|-----------|-------------| |
| 36 | | **#1** | Segmentation | Break large files/functions into focused units | |
| 37 | | **#10** | Prior Action | Clean BEFORE it rots — don't wait for tech debt sprint | |
| 38 | | **#6** | Universality | One function should serve one purpose (SRP) | |
| 39 | | **#27** | Cheap Short-living | Quick small cleanups > expensive large refactors | |
| 40 | | **#2** | Taking Out | Extract what doesn't belong — separate concerns | |
| 41 | |
| 42 | ## The 7-Point Hygiene Checklist |
| 43 | |
| 44 | Run this checklist on every file touched. Each point has auto-detect criteria: |
| 45 | |
| 46 | ``` |
| 47 | ┌───┬──────────────────────┬──────────────────────────────┬────────────────────────┐ |
| 48 | │ # │ Check │ Auto-Detect │ Action │ |
| 49 | ├───┼──────────────────────┼──────────────────────────────┼────────────────────────┤ |
| 50 | │ 1 │ Dead Code │ Unused exports, unreachable │ DELETE — don't comment │ |
| 51 | │ │ │ branches, commented-out code │ out, DELETE │ |
| 52 | ├───┼──────────────────────┼──────────────────────────────┼────────────────────────┤ |
| 53 | │ 2 │ Unused Imports │ Import not used in file │ REMOVE import line │ |
| 54 | ├───┼──────────────────────┼──────────────────────────────┼────────────────────────┤ |
| 55 | │ 3 │ Magic Numbers │ Literal numbers in logic │ EXTRACT to named const │ |
| 56 | │ │ & Strings │ Repeated string literals │ │ |
| 57 | ├───┼──────────────────────┼──────────────────────────────┼────────────────────────┤ |
| 58 | │ 4 │ Naming │ Single-letter vars (not i,j) │ RENAME to describe │ |
| 59 | │ │ │ Abbreviations, inconsistent │ intent clearly │ |
| 60 | ├───┼──────────────────────┼──────────────────────────────┼────────────────────────┤ |
| 61 | │ 5 │ Single Responsibility│ Function does 2+ things │ EXTRACT into separate │ |
| 62 | │ │ (SRP) │ Class has 5+ public methods │ focused units │ |
| 63 | ├───┼──────────────────────┼──────────────────────────────┼────────────────────────┤ |
| 64 | │ 6 │ DRY (Don't Repeat) │ Similar code blocks in 2+ │ EXTRACT shared logic │ |
| 65 | │ │ │ places │ into reusable function │ |
| 66 | ├───┼──────────────────────┼──────────────────────────────┼────────────────────────┤ |
| 67 | │ 7 │ Nesting Depth │ if/for/while nested > 3 │ EXTRACT, early return, │ |
| 68 | │ │ │ levels deep │ guard clauses │ |
| 69 | └───┴──────────────────────┴──────────────────────────────┴────────────────────────┘ |
| 70 | ``` |
| 71 | |
| 72 | ## The Process |
| 73 | |
| 74 | ### Phase 1: SCAN — Detect Code Smells |
| 75 | |
| 76 | > **Goal:** Find what's dirty before cleaning. |
| 77 | |
| 78 | **Automated scan (file-by-file):** |
| 79 | |
| 80 | ``` |
| 81 | For each file modified in current task: |
| 82 | |
| 83 | 1. SIZE CHECK: |
| 84 | IF lines > 300 → FLAG "Large file — consider splitting" |
| 85 | IF any function > 50 lines → FLAG "Long function — extract methods" |
| 86 | |
| 87 | 2. IMPORT CHECK: |
| 88 | Scan imports → cross-reference with usage in file body |
| 89 | Unused import → FLAG for removal |
| 90 | |
| 91 | 3. DEAD CODE CHECK: |
| 92 | Commented-out code blocks → FLAG for deletion |
| 93 | Functions not called anywhere → FLAG (verify with codeintell) |
| 94 | Unreachable code after return/throw → FLAG |
| 95 | |
| 96 | 4. DUPLICATION CHECK: |
| 97 | Similar code blocks (>5 lines identical/near-identical) → FLAG |
| 98 | Copy-paste patterns → FLAG |
| 99 | |
| 100 | 5. NAMING CHECK: |
| 101 | Single-char variables (except loop vars i,j,k) → FLAG |
| 102 | Inconsistent casing (camelCase vs snake_case in same file) → FLAG |
| 103 | Generic names (data, result, temp, item, value, obj) → FLAG |
| 104 | |
| 105 | 6. COMPLEXITY CHECK: |
| 106 | Nesting > 3 levels → FLAG |
| 107 | Function with > 4 para |