Extract method refactoring moves a coherent chunk of operations into a separately named method so the caller reads as a clearer story. The extracted name should reveal the chunk’s purpose, reducing the decoding work required of a reader. section 2.3.5
if (isEligible(user)) { ... }
A complex eligibility condition becomes a single named question. The caller can focus on the decision instead of reconstructing its Boolean details. section 2.3.5
Extract when code becomes cognitively heavy: a long method, deep nesting, a complex or repeated expression, or a group of operations that expresses one business concept. Extraction also helps keep a caller at a uniform level of abstraction. A method name that remains hard to choose is a signal that the chunk may contain more than one responsibility. section 2.3.5 section 2.5
Use the size and role of a chunk to decide where a method boundary belongs:
- Keep an action-level method narrow: it performs a low-level operation such as validating one field. section 2.3.5 section 2.5
- Build a scene-level method from actions and modest supporting logic, such as a small number of conditionals, a loop, or error handling. section 2.5
- Let the original, chapter-level method coordinate scene-level methods into the main flow. section 2.5
if (isUserEligible(user)) {
handleUserPermissions(user);
}
Moving nested permission handling behind a purpose-revealing call flattens the visible control flow. The outer method states the main decision; the helper owns the detailed branch. section 2.3.5
Do not extract mechanically. Leave code in place when the caller is already short, readable, and at one level of abstraction; when the new method would merely wrap one simple chunk; or when it would not improve readability. Extraction is most valuable in complex business logic, maintainability-focused work, and legacy code, but may be unnecessary for simple logic, prototypes, or performance-critical low-level sections. section 2.5
Rewrite a long method as a readable story
Start with a method of more than 50 lines. Read it for recognizable chunks, extract low-level actions, group them into scenes, and let the original method coordinate those scenes. Then name methods and variables by their roles, keep necessary variables near their use, and verify that the result ends with a clear success or failure outcome and cleaned-up resources. section 2.5
A useful heuristic is simple: extract a method when its name makes the caller easier to understand. The result should expose the main story while leaving each helper responsible for one recognizable step. section 2.3.5 section 2.5
