python-refactor
Systematic code refactoring skill that transforms complex, hard-to-understand code into clear, well-documented, maintainable code while preserving correctness. Applies structured refactoring patterns with validation. TRIGGER WHEN: users request "readable", "maintainable", or "clean" code, during code reviews flagging comprehension issues, for legacy code modernization, or in educational/onboarding contexts DO NOT TRIGGER WHEN: the task is outside the specific scope of this component.
What this skill does
# Python Refactor Transform complex Python into clear, maintainable code while preserving correctness. Phased workflow with safety-by-design and continuous validation. For deep references (anti-patterns, OOP principles, cognitive complexity, regression prevention), see the `references/` directory. ## When to invoke - Explicit "human", "readable", "maintainable", "clean", or "refactor" request - Code review flags comprehension or maintainability issues - Legacy code modernization - Onboarding / educational contexts - Complexity metrics exceed thresholds - **Red flags**: file > 500 lines with scattered functions and global state, multiple `global` statements, no clear module/class organization, configuration mixed with business logic ## Do NOT invoke when - Code is performance-critical and profiling shows perf optimization is needed first - Code is scheduled for deletion or replacement - External dependencies require upstream contributions instead - User explicitly requested perf optimization over readability ## Core principles (priority order) 1. **Prefer structured OOP for complex code** -- shared state, multiple concerns, scattered globals = restructure into classes/modules. (But: simple modules with pure functions, click/argparse CLIs, and functional pipelines DON'T need to be forced into classes.) 2. **Clarity over cleverness** -- explicit beats implicit 3. **Preserve correctness** -- all tests pass, behavior identical 4. **Single Responsibility** -- one thing per class/function (SOLID) 5. **Self-documenting structure** -- code = what, comments = why 6. **Progressive disclosure** -- reveal complexity in layers 7. **Reasonable performance** -- never sacrifice >2× without explicit approval ## Hard constraints - **SAFETY BY DESIGN** -- mandatory migration checklists for destructive changes. CREATE → SEARCH → MIGRATE → VERIFY → only then REMOVE. NEVER remove before 100% migration verified. - **STATIC ANALYSIS FIRST** -- `flake8 --select=F821,E0602` (or `ruff check --select=F821`) BEFORE tests. Catches NameErrors immediately. - **PRESERVE BEHAVIOR** -- all existing tests pass after. - **NO PERF REGRESSION** -- never degrade > 2× without explicit approval. - **NO API CHANGES** -- public APIs unchanged unless explicitly requested + documented. - **NO OVER-ENGINEERING** -- simple stays simple. - **NO MAGIC** -- no framework magic, no metaprogramming unless absolutely necessary. - **VALIDATE CONTINUOUSLY** -- static analysis + tests after each logical change. ## Regression prevention (MANDATORY) **Refactoring must NEVER introduce regressions.** Read `references/REGRESSION_PREVENTION.md` before any session. Before each session: - Test suite passes 100% - Coverage ≥ 80% on target code (write tests FIRST if not) - Golden outputs captured for critical edge cases - Static analysis baseline saved After EACH micro-change (not at the end -- every single one): - `flake8 --select=F821,E999` → 0 errors - `pytest -x` → all passing - Spot check 1 edge case for unchanged behavior If ANY check fails: **STOP → REVERT → ANALYZE → FIX APPROACH → RETRY**. ANY REGRESSION = TOTAL FAILURE. ## Workflow (4 phases) ### Phase 1: Analysis 1. Read the entire codebase section. 2. Identify readability issues using `references/anti-patterns.md` (script-like/global-state, God Objects, nested conditionals, long functions, magic numbers, cryptic names). 3. Assess architecture against `references/oop_principles.md` (proper classes/modules, encapsulated state, separated responsibilities, SOLID, DI vs hard-coded deps). 4. Measure current metrics with `scripts/measure_complexity.py` or `scripts/analyze_multi_metrics.py`. 5. Run linting analysis (see Tooling below). 6. Check test coverage; identify gaps to fill BEFORE refactoring. 7. Document with `assets/templates/analysis_template.md`. **Output**: prioritized list of issues by impact and risk. ### Phase 2: Planning 1. **Classify each change**: - **Non-destructive** (rename, docs, type hints) → low risk - **Destructive** (remove globals, delete functions, replace APIs) → high risk 2. **For DESTRUCTIVE changes -- migration plan is MANDATORY**: - Search ALL usages of each element to be removed - Document every usage (file, line, type) - **No complete migration plan = cannot proceed with the destructive change** 3. Risk assessment per change (Low/Medium/High) 4. Dependency map -- what depends on this code? 5. Test strategy -- what tests are needed? what might break? 6. Order changes safest → riskiest 7. Document expected metric improvements **Output**: refactoring plan, sequenced changes, migration plans, test strategy, rollback plan. ### Phase 3: Execution #### Non-destructive (safe anytime) 1. Rename for clarity 2. Extract magic numbers/strings to named constants 3. Add/improve docs and type hints 4. Add guard clauses to reduce nesting #### Destructive (STRICT PROTOCOL) 1. **CREATE** new structure (no removal) -- write new classes/functions + tests 2. **SEARCH** for ALL usages of the element being removed 3. **CREATE** migration checklist documenting every found usage 4. **MIGRATE** one usage at a time, checking off the list, running static analysis + tests after each 5. **VERIFY** complete migration -- re-run searches, should find zero old references 6. **REMOVE** old code only after 100% migration verified #### Execution rules - NEVER skip the migration checklist for destructive changes - Run static analysis BEFORE tests - One pattern at a time -- never mix multiple refactoring patterns in a single change - Atomic commits -- each migration step gets its own commit - Stop on ANY error (static analysis OR test failure) → immediate fix/revert #### Recommended order 1. Transform script-like code to proper architecture (`references/examples/script_to_oop_transformation.md`) 2. Rename for clarity 3. Extract magic numbers/strings to constants/enums 4. Improve docs + type hints 5. Extract methods to reduce function length 6. Simplify conditionals with guard clauses 7. Reduce nesting depth 8. Final review: separation of concerns ### Phase 4: Validation 1. **Static analysis FIRST**: ```bash flake8 <file> --select=F821,E0602 # undefined names/variables -- MUST be 0 flake8 <file> --select=F401 # unused imports flake8 <file> # full quality check ``` 2. **Full test suite** → 100% pass required. 3. **Architecture validation**: global state eliminated/encapsulated, proper modules/classes, separated responsibilities, SOLID compliance. 4. **Before/after metrics** with `scripts/measure_complexity.py` or `scripts/analyze_multi_metrics.py`. 5. **Performance regression check** with `scripts/benchmark_changes.py` for hot paths. 6. **Summary report** using `assets/templates/summary_template.md`. 7. **Flag for human review** if: perf degraded > 10%, public API signatures changed, test coverage decreased, significant architectural changes. ## Refactoring patterns (catalog summary) Full catalog with examples in `references/patterns.md`. Key patterns: - **Guard Clauses** -- early returns instead of nested conditionals - **Extract Method** -- split large functions into focused units (resets the nesting counter -- most powerful for cognitive complexity) - **Dictionary Dispatch** -- replace if-elif chains with lookup tables - **Match Statement** (Py 3.10+) -- counts as +1 total, not per branch - **Named Boolean Conditions** -- extract complex booleans into named variables - **Encapsulate Global State** -- move globals into classes with proper encapsulation - **Group Related Functions** -- organize scattered functions into classes by responsibility - **Create Domain Models** -- replace primitive dicts with dataclasses + enums - **Apply Dependency Injection** -- replace hard-coded deps with injected ones For cognitive complexity calculation rules and reduction strategies, see `references/cognitive_complexity_guide.md`. ### Naming conventions - **Variables*
Related in Design
contribute
IncludedLocal-only OSS contribution command center. Auto-refreshes the user's in-flight PR and issue state on invoke so conversations start with full context — no need to brief Claude on what's in flight. Helps the user find issues to contribute to on GitHub, builds per-repo dossiers of what each upstream expects (CLA, DCO, branch convention, AI policy, draft-first, review bots, issue templates), runs deterministic gates before any external action so AI-assisted contributions don't reach maintainers as slop. State is markdown-only: candidate files at ~/.contribute-system/candidates/, repo dossiers at ~/.contribute-system/research/, append-only event log at ~/.contribute-system/log.jsonl. No database, no cloud calls. Use when the user asks about their PRs / issues / contributions, wants to find new work to take on, claim an issue, build/refresh a repo's dossier, or draft a Design Issue or PR. Trigger with "/contribute", "what's my PR status", "find a contribution", "claim issue X", "draft a Design Issue for Y", "refresh dossier for Z".
architectural-analysis
IncludedUser-triggered deep architectural analysis of a codebase or scoped subtree across eight modes — information architecture, data flow, integration points, UI surfaces, interaction patterns, data model, control flow, and failure modes. This skill should be used when the user asks to "diagram this codebase," "map the architecture," "show the data flow," "give me an ERD," "trace control flow," "find the integration points," "verify the layout pattern," "audit the UX architecture," or any similar request whose primary deliverable is mermaid diagrams plus cited reports under docs/architecture/. Dispatches haiku/sonnet sub-agents in parallel for per-mode exploration, then verifies every citation mechanically before any node lands in a diagram. Not for one-off prose explanations of code (use code-explanation) or for high-level system design from scratch (use system-design).
mcp
IncludedModel Context Protocol (MCP) server development and tool management. Languages: Python, TypeScript. Capabilities: build MCP servers, integrate external APIs, discover/execute MCP tools, manage multi-server configs, design agent-centric tools. Actions: create, build, integrate, discover, execute, configure MCP servers/tools. Keywords: MCP, Model Context Protocol, MCP server, MCP tool, stdio transport, SSE transport, tool discovery, resource provider, prompt template, external API integration, Gemini CLI MCP, Claude MCP, agent tools, tool execution, server config. Use when: building MCP servers, integrating external APIs as MCP tools, discovering available MCP tools, executing MCP capabilities, configuring multi-server setups, designing tools for AI agents.
react-native-skia
IncludedDesign, build, debug, and optimise high-polish animated graphics in React Native or Expo using @shopify/react-native-skia, Reanimated, and Gesture Handler. Use when the user wants canvas-driven UI, shaders, paths, rich text, image filters, sprite fields, Skottie, video frames, snapshots, web CanvasKit setup, or performance tuning for custom motion-heavy elements such as loaders, hero art, cards, charts, progress indicators, particle systems, or gesture-driven surfaces. Also use when the user asks for fluid, glow, glass, blob, parallax, 60fps/120fps, or GPU-friendly animated effects in React Native, even if they do not explicitly say "Skia". Do not use for ordinary form/layout work with standard views.
plaid
IncludedProduct Led AI Development — guides founders from idea to launched product. Six capabilities: Idea (discover a product idea), Validate (pressure-test the idea against fatal flaws, problem reality, competition, and 2-week MVP feasibility), Plan (vision intake + document generation), Design (translate image references into a design.md spec), Launch (go-to-market strategy), and Build (roadmap execution). Use when someone says "PLAID", "plaid idea", "help me find an idea", "product idea", "idea from my business", "idea from my expertise", "plaid validate", "validate my idea", "pressure-test", "is this idea good", "find fatal flaws", "validate the problem", "plan a product", "define my vision", "generate a PRD", "product strategy", "plaid design", "design from image", "translate image to design", "create design.md", "extract design tokens", "plaid launch", "go-to-market", "launch plan", "GTM strategy", "launch playbook", "plaid build", "build the app", "start building", or "execute the roadmap".
nextjs-framer-motion-animations
IncludedAdds production-safe Motion for React or Framer Motion animations to Next.js apps, including reveal, hover and tap micro-interactions, whileInView, stagger, AnimatePresence, layout and layoutId transitions, reorder, scroll-linked UI, and lightweight route-content transitions. Use when the user asks to add, refactor, or debug Motion or Framer Motion in App Router or Pages Router codebases, especially around server/client boundaries, reduced motion, LazyMotion, bundle size, hydration, or route transitions. Avoid for GSAP-style timelines, WebGL or 3D scenes, heavy scroll storytelling, or CSS-only effects unless Motion is explicitly requested.