software-code-refactoring

Refactor production code while preserving all existing test behavior.

902|58|Updated May 31, 2012
One-click install
npx skills add https://github.com/stencila/stencila --skill software-code-refactoring
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: software-code-refactoring
Source: https://github.com/stencila/stencila/tree/main/.stencila/skills/software-code-refactoring
Command: npx skills add https://github.com/stencila/stencila --skill software-code-refactoring

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Production code that works but has accumulated duplication, unclear naming, or unnecessary complexity is hard to maintain. This Skill improves code quality through safe, incremental refactorings while guaranteeing that every existing test continues to pass.

Core Features & Use Cases

  • Convention Discovery: Analyzes the codebase's language, build system, naming patterns, and idioms before making any change, so refactored code matches existing style.
  • Safe Transformations: Applies one refactoring at a time — extracting functions, renaming, flattening conditionals, removing dead code — verifying compilation and tests after each step and reverting any change that breaks behavior.
  • Structured Summary: Produces a report of every refactoring applied, skipped, or reverted, with the quality dimension each one improves.
  • Use Case: During the refactor phase of a TDD red-green-refactor cycle, point the Skill at a module with passing tests and receive cleaned-up code with zero behavior changes and a full verification report.

Quick Start

Refactor the production code in src/parser/expression.rs to reduce duplication and improve naming, keeping all tests passing with cargo test -p parser.

Frequently Asked Questions about software-code-refactoring

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

FAQPage Schema
How do I refactor code without breaking existing tests?

Apply one refactoring at a time and run the test suite after each change. If a test fails, revert that specific refactoring immediately rather than modifying the test, since a failure means the transformation changed behavior.

How to safely refactor code during the TDD refactor phase?

In the refactor phase of red-green-refactor, keep the public API unchanged and focus on internal improvements like extracting duplicated logic, renaming unclear variables, and flattening nested conditionals. Verify compilation after each step and run the full test command at the end.

Does this refactoring approach work with Python, Rust, and TypeScript?

Yes, it works with any language or framework. It discovers the build system and conventions from files like Cargo.toml, package.json, or pyproject.toml, then uses the appropriate check command such as cargo check, tsc --noEmit, or py_compile.

What happens when a refactoring breaks a test?

The change that caused the failure is reverted immediately, and tests are re-run to confirm the revert is clean. The failed refactoring is recorded in the summary with an explanation of why it was unsafe, and a safer alternative may be applied instead.

Can refactoring change a module's public API?

No, public function signatures, type names, and module exports are never changed unless explicitly requested. Only internal restructuring is performed, though beneficial public API changes can be noted as recommendations in the summary.

When should I not refactor code?

Skip refactoring when the code is already clean and follows codebase conventions, when changes would degrade readability, or when performance-sensitive hot paths could be affected. Unnecessary changes add risk without measurable improvement.