🎁 Get the FREE AI Skills Starter GuideSubscribe →
BytesAgainBytesAgain
🦀 ClawHub

Scratch Refactoring For Code Understanding

by @quochungto

Guide a developer through throwaway refactoring — restructure code freely without tests to understand it, then DISCARD. Use whenever a developer says 'I don'...

Versionv1.0.0
When to Use
TriggerAction
- Passive reading (scrolling through code) is not producing a clear mental model
- The code is too tangled to trace by eye — hidden coupling, deep nesting, mystery variables
- You are onboarding to a codebase area with no documentation and no author to ask
- A planned change is blocked because the intent of the existing code is unknown
Scratch refactoring is a **comprehension technique**, not a production technique. It is a complement to characterization tests (which pin down behavior) and effect sketches (which map change propagation). When you need to understand structure before you can even write a characterization test, scratch refactoring is the right first step.
Do not use this skill if:
- The code is already understood — skip directly to characterization testing
- You are mid-change on a production branch — a scratch exploration on a dirty working tree risks mixing exploration with production edits
💡 Examples

Example 1: Opaque Algorithm in a Billing System

A developer needs to modify a 300-line calculateInvoice() method but cannot determine which lines handle tax calculation versus line-item accumulation versus discount application.

Scratch session: 1. Create branch scratch/understand-calculate-invoice. 2. Learning goal: "What are the phases of this method, and where does each phase begin and end?" 3. Extract blocks into named methods: accumulateLineItems(), applyVolumeDiscounts(), calculateTaxByJurisdiction(), formatInvoiceOutput(). 4. Learning notes record: three-phase structure (accumulate → discount → tax), tax phase reads a hidden configuration object, discount logic has a special case for government accounts. 5. Delete branch. Learning notes survive.

Result: The developer now knows exactly where to add the new billing adjustment — in the accumulation phase, before discounts — and what tests to write first (a characterization test for the government-account discount case).

Example 2: Class with Tangled Responsibilities

A team inheriting a CustomerManager class (800 lines, no tests) cannot determine what it owns versus what it delegates.

Scratch session: 1. Create branch scratch/understand-customer-manager. 2. Learning goal: "What responsibilities does this class actually own?" 3. Rename fields and methods to reflect discovered purpose. Group by responsibility. Notice that 40% of the methods only touch a subscription sub-object — extract a SubscriptionHandler conceptually in the scratch. 4. Learning notes record: three hidden responsibilities (identity management, subscription lifecycle, notification dispatch). The subscription methods are cohesive enough to become their own class. 5. Delete branch.

Result: The team's real refactoring plan is now grounded: introduce a SubscriptionHandler class with characterization tests, extracted from CustomerManager using the Extract Class technique. The scratch session revealed the seam without committing premature structure.

View on ClawHub
TERMINAL
clawhub install bookforge-scratch-refactoring-for-code-understanding

🧪 Use this skill with your agent

Most visitors already have an agent. Pick your environment, install or copy the workflow, then run the smoke-test prompt above.

🔍 Can't find the right skill?

Search 60,000+ AI agent skills — free, no login needed.

Search Skills →