Code Refactoring Patterns

Refactor complex Python functions using extraction patterns and guard clauses.

Updated Sep 8, 2025
One-click install
npx skills add https://github.com/randalmurphal/claude-config --skill code-refactoring-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Code Refactoring Patterns
Source: https://github.com/randalmurphal/claude-config/tree/main/skills/code-refactoring
Command: npx skills add https://github.com/randalmurphal/claude-config --skill code-refactoring-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

When and how to refactor complex functions including complexity thresholds (50+ statements, 12+ branches), extraction patterns, guard clauses, and testing strategies. Use when ruff/pylint complexity warnings appear or code becomes hard to maintain.

Core Features & Use Cases

  • Extraction & Guard Clauses: Break apart large functions with focused helpers.
  • Guard Clauses: Reduce nesting for readability.
  • Metadata Extraction: Improve testability and reuse.

Quick Start

Start by identifying a large function, extract a helper, and add guard clauses to reduce nesting.

Frequently Asked Questions about Code Refactoring Patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor complex Python functions to improve readability?

Refactor complex functions by extracting helper methods, adding guard clauses to reduce nesting, and isolating concerns. Apply Extract Method when functions exceed 50 statements or 12 branches, use guard clauses at entry points to flatten conditional logic, and extract variables for clarity. This reduces complexity and improves maintainability.

When should I refactor code with guard clauses versus nested conditionals?

Use guard clauses when your function has deep nesting or multiple exit conditions early in execution. Guard clauses return or raise errors at the start, eliminating indentation levels and making the happy path more visible. This technique applies to Python codebases with high cyclomatic complexity flagged by tools like ruff or pylint.

How do I extract metadata from functions to improve testability?

Extract metadata extraction into separate helper functions to isolate data transformation logic from business logic. This separation makes unit testing easier, enables reuse across functions, and creates clear interfaces. It's especially valuable when metadata handling obscures the main function's intent.

What patterns work best for breaking apart large Python functions?

Use Extract Method to isolate cohesive responsibilities, Extract Variable to name complex expressions, and Guard Clause to flatten control flow. Combine these patterns to transform deeply nested, multi-statement functions into modular, testable components with single responsibilities.

Can I refactor code while maintaining existing test coverage?

Yes. Refactor systematically by extracting small helpers first, running tests after each change to catch regressions. Extract Metadata and isolated logic into pure functions that are easy to test independently, then update test cases to target the new boundaries. This maintains safety throughout the refactoring process.