Extract a responsibility with a clear name
Look for a block that performs one coherent operation and needs a comment to explain its purpose. Extract it into a function whose inputs and result express that responsibility. Avoid extracting arbitrary line ranges that still depend on many surrounding variables and obscure the original control flow.
Illustrative before-and-after: an invoice loop repeatedly calculates line totals inline; after extraction, calculateLineTotal takes quantity, unit price, and discount. Keep rounding order unchanged. Test a discounted line and a fractional quantity before deciding whether later arithmetic changes belong in a separate bug-fix task.
Simplify control flow without losing conditions
Guard clauses can make rejection paths visible before the main operation. Move one condition at a time and preserve the order of side effects and exceptions. Conditions that look independent may depend on earlier validation, so an apparent readability improvement can otherwise change which error callers receive.
Use a parameter object when several values consistently represent one concept, such as an address or date interval. Define its validation and mutability deliberately. Grouping unrelated arguments merely hides a long parameter list; it does not create a useful domain concept or simplify the caller's responsibilities.
Separate policy from external effects
Move calculations and decisions away from storage, HTTP calls, or notification delivery when that produces a clearer boundary. Return an explicit result that the orchestration layer can act on. This can make policy checks faster and easier to understand without introducing a framework or an unnecessary hierarchy.
After each transformation, compare the public contract and inspect the diff for moved side effects, changed error order, or accidental mutation. Use a realistic follow-up task to assess the result. If a small rule change still requires editing many modules, the responsibility boundary may need further thought.
Working sheet
| Obstacle | Technique | Preservation check |
|---|---|---|
| Unclear calculation block | Extract function | Same rounding and edge results |
| Deep rejection nesting | Guard clauses | Same error order and side effects |
| Repeated related arguments | Parameter object | Same defaults and validation |
| Policy mixed with database writes | Separate calculation from orchestration | Same transaction and persistence behavior |
Questions about practical code refactoring techniques
Should every long function be split?
Split by responsibility and understandable boundaries. Length alone does not identify the right transformation.
Can I change behavior during refactoring?
Make intended behavior changes explicit and separately verifiable. Mixing them silently makes review and diagnosis harder.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
