- Home
- Skills
- Code Quality & Review
- behavior preserving refactor
behavior preserving refactor
Systematic code refactoring that establishes behavior baselines to ensure structural changes never break existing logic.
$1.96
Works with the AI Agents you already use
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
| Dimension | Baseline Status | Post-Refactor Status | Verification Method |
|---|---|---|---|
| Dependency Creation | Internal new Mailer() | Injected via Constructor | Source Code Audit |
| Error Handling | Throws MailDeliveryException | Throws MailDeliveryException | Unit Test (Mock Throw) |
| Side Effects | 1 SMTP connection per call | 1 SMTP connection per call | Integration Spy |
| Data Integrity | Matches templates/v1 | Matches templates/v1 | Characterization 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
- Update the
AppConfigor DI container to provide theMailerinstance. - Run the provided characterization tests in
tests/refactor/user-notification.spec.ts. - 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
- Analyze scope to define structural goals and identify which external contracts are relevant to the change.
- Create baselines by running existing tests or adding temporary characterization tests for missing coverage.
- Execute steps by creating new boundaries first, migrating callers, and then removing deprecated implementations.
- 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.
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
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
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
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