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

Choose a technique for a specific obstacle
ObstacleTechniquePreservation check
Unclear calculation blockExtract functionSame rounding and edge results
Deep rejection nestingGuard clausesSame error order and side effects
Repeated related argumentsParameter objectSame defaults and validation
Policy mixed with database writesSeparate calculation from orchestrationSame transaction and persistence behavior
Download this worksheet (CSV)

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.

Make the next change verifiable.

Getzilla is an open source workflow for changes to existing repositories. Inspect the project, define acceptance checks, and keep independent review in the loop.

See the repository workflow See current access and pricing