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
| Change | Potential surprise | Check |
|---|---|---|
| Extract method | Changed null or overload behavior | Compile and boundary tests |
| Try-with-resources | Closing a caller-owned resource | Ownership and failure-path test |
| Loop to stream | Ordering or side effects change | Empty, ordered, and repeated inputs |
| Rename class or method | Reflection or configuration reference breaks | Search configuration and integration tests |
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.
