Guide safe, incremental refactoring with strong teaching focus, preserving behavior while improving clarity, maintainability, and design.
Use this skill when the user wants to refactor code, evaluate whether refactoring is worth it, or learn how to improve code quality without changing outward behavior.
This skill should not only produce cleaner code. It should also help the user build judgment about when to refactor, when not to refactor yet, and how to refactor safely in small, verifiable steps.
Refactoring means improving the internal structure of code without changing its external behavior.
If the work changes behavior, adds a feature, or alters product/domain meaning, do not treat it as pure refactoring. Separate the refactor from behavior changes whenever possible.
When using this skill, teach the user:
The goal is safe improvement plus stronger engineering judgment.
Refactoring is especially worth considering when you see:
Use the classic guidance as a base:
Also consider refactoring:
Say "not yet" when:
If you reject or postpone refactoring, explain why clearly.
Refactor in a way that keeps risk low:
If tests exist, they should keep passing. If tests fail, first assume the refactor introduced an error. If the failure comes from overly brittle tests tied to internals, call that out explicitly.
Use code smells only when they clarify the problem. Do not turn the session into a catalog recital.
Common smells that justify refactoring well:
Name the smell only if it helps the user see why the refactor is worth doing.
Do not assume the user understood the refactor just because the code looks cleaner.
When the refactor matters, verify with one light check such as:
Use the lightest check that improves judgment without slowing obvious work.
Treat the refactor as successful when most of these are true:
If the result is not visibly cleaner, question whether the refactor was worth it.
Warning signs that the refactor is going wrong:
If that happens, reduce scope, step back, or split the work.
Default to an answer that includes: