Extract methods around cohesive domain work

Start with a calculation or decision that has explicit inputs and a clear result. Keep its visibility as narrow as the callers require. Check overload resolution, null handling, and mutable object access when moving code, because a method extraction can expose assumptions previously hidden in local control flow.

Illustrative scenario: an invoice service mixes tax selection with JDBC persistence. Extract the selection logic and test domestic, exempt, and missing-category inputs. Keep the persistence transaction unchanged during this step; moving transaction boundaries at the same time would introduce a separate integration risk.

Preserve resources, exceptions, and mutation

When simplifying resource handling, identify who owns each stream, connection, or reader. Java's try-with-resources closes resources declared for that statement, so do not wrap a caller-owned resource casually. Check exception behavior as well as the successful result when replacing manual cleanup code.

Replacing loops with streams can change readability, evaluation timing, and handling of mutation. Preserve ordering where callers rely on it, and avoid introducing side effects into pipelines for cosmetic brevity. Test empty collections, null assumptions, and repeated invocation before deciding the replacement improves the original code.

Review compatibility and evidence

Use the repository's configured JDK and build tool to verify the change. Check public method signatures, serialization expectations, reflection-based access, and framework configuration if symbols move or names change. Compiler success cannot establish that external callers or configuration strings still refer to the intended code.

Keep each commit focused enough to inspect mechanically. Record any deliberate change to exceptions, visibility, or resource ownership as a behavior or interface change rather than hiding it inside cleanup. A useful result is a clearer domain boundary with unchanged contract tests and integration behavior.

Working sheet

Java-specific refactoring checks
ChangePotential surpriseCheck
Extract methodChanged null or overload behaviorCompile and boundary tests
Try-with-resourcesClosing a caller-owned resourceOwnership and failure-path test
Loop to streamOrdering or side effects changeEmpty, ordered, and repeated inputs
Rename class or methodReflection or configuration reference breaksSearch configuration and integration tests
Download this worksheet (CSV)

Example to adapt

Illustrative example. Run it against your own fixtures and supported tool versions.

// Illustrative extraction; both versions keep int arithmetic unchanged.
// Before
static int totalBefore(int[] prices) {
    int total = 0;
    for (int price : prices) total += price;
    return total;
}
// After: the loop has a named responsibility.
static int totalAfter(int[] prices) {
    return sumPrices(prices);
}
static int sumPrices(int[] prices) {
    int total = 0;
    for (int price : prices) total += price;
    return total;
}
// Verify empty input, multiple prices, and the existing null contract.

Questions about refactor Java code without changing its contract

Should every Java loop become a stream?

No. Use the form that makes ordering, mutation, and failure behavior easiest to understand for the task.

Is a successful compile enough?

No. Compilation checks types and syntax, while contract, configuration, and integration behavior need additional evidence.

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