Skills

Software Code Refactoring

Improve production code quality while preserving all existing test behavior.

Install

npx skills add stencila/stencila --skill software-code-refactoring -g -a claude-code -y
Repository
stencila/stencila914
Category
Testing
Updated
3d ago
License
Apache-2.0
Context cost
Mediumabout 3,000 tokens
Runs scripts
No, instructions only

What it does

Improve production code quality while preserving all existing test behavior. Commonly used for the Refactor phase of TDD red-green-refactor, but applicable to any codebase with tests. Use when production code works but needs cleanup, reducing duplication, improving naming, simplifying complexity, aligning with codebase style, extracting functions or types, or removing dead code. Discovers conventions, applies safe transformations, verifies compilation after each change, and produces a structured summary. Works with any language or framework.

Overview

Improve existing production code that already has passing tests. This skill is commonly used for the "refactor" phase of a red-green-refactor cycle, but works equally well as a standalone code-quality pass. Given passing production code and its tests, it discovers the codebase's conventions, identifies refactoring opportunities, applies safe transformations, and verifies that all tests continue to pass.

The core principles are:

  • Behavior preservation is non-negotiable. Every refactoring must leave all existing tests passing. If a change breaks a test, revert it immediately and try a different approach.
  • Discover first, prescribe only as fallback. Adapt to whatever language, framework, and conventions the codebase already uses. Align refactored code with these conventions rather than imposing external style preferences.
  • Small, verifiable steps. Apply one refactoring at a time. Verify compilation/parsing after each change. This makes failures easy to diagnose and revert.
  • Public API stability. Do not change public function signatures, type names, module exports, or other externally visible interfaces unless explicitly requested. Internal restructuring is the focus.

The refactoring must:

  • Preserve all existing test behavior (all tests that passed before must still pass)
  • Follow the codebase's existing conventions (naming, module layout, error handling, coding style)
  • Not change the public API of any module, crate, or package
  • Compile or parse without errors or warnings where possible
  • Improve code quality in at least one measurable dimension (duplication, complexity, readability, naming, style consistency)

Required Inputs

This skill requires the following information to operate:

Read the whole SKILL.md on GitHub

More in Testing