Book refactoring full

From Horonomos-laps/Yana-AI · View interactive version

Book refactoring full is an AI agent skill from the GitHub repository Horonomos-laps/Yana-AI. Refactoring (Martin Fowler) — Full rules — comprehensive mandatory coding standards. Use when asked to apply Refactoring principles or review code against Refactoring standards.

How to use this skill

Skill file

OBEY Refactoring by Martin Fowler

Purpose

This repository follows the discipline of Refactoring in the sense of Martin Fowler:

improve the internal structure of code without changing its observable behavior.

All code generation, edits, and reviews must optimize for:

This file is a binding engineering policy: MUST is binding, SHOULD is a strong default, and MUST NOT is forbidden.


Primary Directive

When modifying existing code, do not start by rewriting large areas.

Start by making the next safe structural improvement that makes the desired change easier.

Prefer:

  1. establish a safety net
  2. make a preparatory refactoring
  3. make the functional change
  4. refactor again if needed

Reject changes that bundle large functional changes with unrelated structural churn.


What Counts as Refactoring

Refactoring here means:

Refactoring here does not mean:


Non-Negotiable Rules

  1. Preserve Behavior
  1. Work in Small Steps
  1. Keep the System Running
  1. Refactor Before and After Feature Work
  1. Use the Simplest Helpful Refactoring

Safety Rules

Tests and Verification

  1. Create or identify a safety net before risky refactoring.
  2. Prefer characterization tests when working on unclear existing behavior.
  3. If tests are absent, make the smallest changes possible and improve testability first.
  4. Keep refactoring and test updates aligned with preserved behavior.
  5. Never delete a failing test just to complete a refactoring.

Commit and Patch Discipline

  1. Separate structural edits from behavior changes whenever practical.
  2. Group related refactorings together.
  3. Avoid giant mixed commits that rename, move, redesign, and change logic all at once.
  4. Prefer reviewable sequences of transformations.

Preparatory Refactoring

Before implementing a feature, ask:

Do the preparatory refactoring before the feature change.


Code Smell Policy

When modifying code, actively look for these smells.

Duplicated Code

Long Functions

Long Parameter Lists

Global Data and Hidden Dependencies

Divergent Change

Shotgun Surgery

Feature Envy

Data Clumps and Primitive Obsession

Switch Statements and Conditionals

Temporary Fields and Weird Lifecycles

Middle Man and Speculative Generality


Preferred Refactoring Moves

Naming Refactorings

Extraction Refactorings

Movement Refactorings

Simplification Refactorings

Data Refactorings


Refactoring Catalog Index

Composing Methods

Moving Features

Organizing Data

Simplifying Calls and Conditionals

Generalization and Big Refactorings


Function-Level Rules

  1. One function should usually perform one coherent task.
  2. Keep abstraction level consistent inside a function.
  3. Remove hidden side effects unless the function's purpose is to cause them.
  4. Prefer guard clauses over deeply nested conditionals when that clarifies the happy path.
  5. Split phases like parsing, validation, computation, and I/O when they are mixed together.
  6. Keep variable scope tight.
  7. Delete dead code rather than comment it out.

Class and Module Rules

  1. A class or module should have a narrow reason to change.
  2. Separate policy from presentation, I/O, persistence, and framework details.
  3. Prefer composition of small focused units over god objects.
  4. Delete or inline abstractions that no longer pay for themselves.
  5. Do not create utils, helpers, or common modules as a default response to duplication.
  6. Organize modules around concepts and behavior, not leftover convenience.

Rules for Working with Conditionals

  1. Replace repeated branching on type or status with stronger modeling when useful.
  2. Use lookup tables for stable mapping logic.
  3. Replace nested if/else pyramids with guard clauses, extracted predicates, or strategies when that reduces branching complexity.
  4. Keep explicit conditionals when they are simple and honest.
  5. Never introduce polymorphism merely to avoid a small local conditional.

Data and Mutation Rules

  1. Encapsulate mutation.
  2. Narrow write access to the smallest useful surface.
  3. Replace ad hoc mutations with intention-revealing operations.
  4. Remove duplicated update logic by centralizing state transitions.
  5. Prefer immutable intermediate values when that simplifies reasoning.

Error Handling Rules

  1. Refactor error handling to make the main path visible.
  2. Keep cleanup, validation, and recovery logic from drowning core behavior.
  3. Standardize similar error paths when they duplicate structure.
  4. Preserve existing error semantics unless intentionally changing behavior.

Review Rules

When reviewing or generating changes, actively look for:


Forbidden Patterns

Do not generate or keep these patterns unless explicitly required and justified.

Big-Bang Rewrite

Mixed-Intent Patches

Abstracting Too Early

Refactoring Theater

Untested Structural Surgery


Code Generation Rules

When asked to modify existing code, use this default order:

  1. understand current behavior
  2. identify the friction for the requested change
  3. add or improve the safety net if needed
  4. perform preparatory refactoring
  5. implement the behavioral change
  6. perform follow-up cleanup
  7. stop when the design is clearly better

Preferred first moves:

Preferred avoidance:


Testing Rules

  1. Add characterization tests before risky edits when behavior is unclear.
  2. Keep tests focused on externally visible behavior.
  3. Update tests only when behavior intentionally changes.
  4. Do not couple tests to private implementation details more than necessary.
  5. Refactor tests too when they become noisy or duplicative.
  6. Keep test data expressive and minimal.

Stopping Rules

Stop refactoring when:


Review Checklist

Before finalizing any change, verify:

If any answer is no, revise before shipping.


Final Instruction

When uncertain, choose the next small, behavior-preserving transformation

that makes the requested change easier and the code easier to understand.

Reject approaches that gamble on large rewrites or mix too many intentions at once.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.