behavior preserving refactor

    1

    Systematic code refactoring that establishes behavior baselines to ensure structural changes never break existing logic.

    $1.96

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIDeepSeekDeepSeekMuseMuse+22 more

    behavior preserving refactor

    Example session with this skill installed

    Refactor the UserNotificationService to use dependency injection for the Mailer client instead of creating it internally. Ensure the email formatting and error handling remain identical.

    • Read your context and instructions
    • Compiled the behavior preserving refactor

    I have analyzed UserNotificationService and established a behavior baseline. The refactor moves Mailer instantiation to the constructor while preserving the existing retry logic and exception mapping.

    Refactor Analysis and Verification

    DimensionBaseline StatusPost-Refactor StatusVerification Method
    Dependency CreationInternal new Mailer()Injected via ConstructorSource Code Audit
    Error HandlingThrows MailDeliveryExceptionThrows MailDeliveryExceptionUnit Test (Mock Throw)
    Side Effects1 SMTP connection per call1 SMTP connection per callIntegration Spy
    Data IntegrityMatches templates/v1Matches templates/v1Characterization Test

    Updated Implementation

    // user-notification-service.ts
    export class UserNotificationService {
      private mailer: IMailer;
    
      constructor(mailer: IMailer) {
        this.mailer = mailer;
      }
    
      async notify(user: User, message: string): Promise<void> {
        try {
          // Behavior preserved: Using the same formatting logic as baseline
          const content = formatMessage(user.name, message);
          await this.mailer.send(user.email, content);
        } catch (error) {
          // Behavior preserved: Mapping internal errors to domain exceptions
          throw new MailDeliveryException(error.message);
        }
      }
    }
    

    Next steps

    1. Update the AppConfig or DI container to provide the Mailer instance.
    2. Run the provided characterization tests in tests/refactor/user-notification.spec.ts.
    3. Review the structural diff to confirm no unrelated formatting changes were introduced.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Refactoring legacy code without breaking production behavior is a high-risk task. This skill formalizes the process by establishing behavior baselines before any structural changes occur. It ensures that internal adjustments to modules, interfaces, and dependency boundaries remain transparent to callers. You get a systematic approach to decoupling large classes, implementing dependency injection, and removing duplication without the typical regression cycle.

    What it does

    • Define observable contracts identifying return values, side effects, and error types that must remain unchanged.
    • Establish behavior baselines using characterization tests to document existing logic before modification.
    • Decouple dependencies by moving internal object creation to injection paths or assembly layers.
    • Verify equivalence by comparing new and old implementations against identical boundary and error inputs.
    • Structural auditing providing evidence that architectural goals are met without regressing calling logic.

    How it works

    1. Analyze scope to define structural goals and identify which external contracts are relevant to the change.
    2. Create baselines by running existing tests or adding temporary characterization tests for missing coverage.
    3. Execute steps by creating new boundaries first, migrating callers, and then removing deprecated implementations.
    4. Validate output by separating structural checks from behavior regression reports.

    Frameworks & tools

    This skill is language-agnostic and works with any stack where unit testing and interface definitions are possible. It is particularly effective for refactoring in Java, C#, TypeScript, or Python projects involving complex business logic or legacy monoliths.

    Why this beats prompting it yourself

    Generic AI refactoring often creates "cleaner" code that subtly changes side effects or error handling. This skill enforces a "contract-first" methodology that prioritizes behavioral parity over aesthetic preferences, preventing hidden regressions in production logic.

    Use cases

    • Extracting logic from bloated classes into testable, independent modules.
    • Introducing dependency injection to replace hardcoded instances and improve testability.
    • Consolidating duplicated code while ensuring edge-case handling remains consistent.
    • Updating internal APIs while maintaining backward compatibility for existing callers.

    Known limitations

    Does not support automatic bug fixing during refactoring unless explicitly requested. Requires an environment where at least minimal baseline tests can be executed.

    Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Code Quality & Review.

    Refactoring and bug fixing must be authorized separately. Requires a testable environment to establish baselines. Does not support automated deployment or pushing code.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before buying a skill.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Money-back guarantee

    Doesn't do what its page promises? You get your money back.

    Trust & safety

    Security scan

    Security scan passed 25 days ago

    Permissions

    No special permissions declared or detected

    Listed25 days ago

    What's inside

    Frequently Asked Questions

    Unlike generic AI coding assistants that prioritize aesthetics, this skill uses a "contract-first" methodology, establishing behavior baselines and characterization tests to ensure your logic remains identical before and after the structural change.

    The skill is language-agnostic and works with any stack that supports unit testing and interface definitions, including Java, C#, TypeScript, and Python.

    Yes, this skill is specifically designed to handle legacy monoliths by identifying observable contracts and decoupling dependencies, making it easier to move toward a more modular architecture.

    You receive the full refactoring agent skill, including the systematic logic for baseline establishment, dependency decoupling, and equivalence verification to ensure your code remains production-ready.

    While the skill provides the structural evidence that architectural goals are met, it requires a local environment where at least minimal baseline tests can be executed to verify equivalence.

    This skill focuses strictly on structural improvement; it will not automatically fix existing bugs during a refactor unless you explicitly request a separate debugging task to avoid changing baseline behavior.

    New in Code Quality & Review