Refactoring
Purpose
Improve code quality without changing external behavior. Ensure refactoring is safe and verifiable.
Before You Start
- Ensure tests pass: Run
cargo testto establish a baseline. - Understand the code: Read the code thoroughly before making changes.
- Plan the refactoring: Identify what needs to change and why.
Refactoring Techniques
Extract Function
- Move inline logic into a named function when it has a clear single responsibility.
- Name the function after what it does, not how it does it.
Simplify Conditionals
- Replace nested
if/elsechains with early returns or pattern matching. - Use
matchinstead of longif/else ifchains when comparing against constants. - Extract complex boolean expressions into named variables or helper functions.
Reduce Duplication
- Identify repeated patterns and extract them into shared functions or macros.
- Be careful not to over-abstract — sometimes duplication is clearer than wrong abstraction.
Improve Types
- Replace
Stringwith&strwhere ownership isn't needed. - Use
enuminstead of string constants for known variants. - Use
Option<T>instead of sentinel values like empty strings.
Module Restructuring
- Group related functionality into modules.
- Keep module boundaries clean — avoid circular dependencies.
- Use
pub(crate)instead ofpubwhen possible to limit API surface.
Verification Checklist
After refactoring:
-
cargo testpasses -
cargo clippyhas no new warnings -
cargo fmt --checkpasses - No dead code introduced
- Public API is unchanged (or document breaking changes)
Guidelines
- Make small, incremental commits — one logical change per commit.
- Prefer readability over cleverness.
- Don't mix refactoring with feature changes in the same commit.
- If unsure whether a change is safe, ask the user.
- Document any non-obvious design decisions in comments.


