Category
Composing Methods
Techniques that split, merge and reshape method bodies so each method does one readable thing and local variables stop blocking change.
What these techniques solve
Most refactoring starts inside a single function. Long bodies, temporary variables that hold three different things, and parameters reassigned halfway down make code hard to read and hard to move. These techniques work at that level: they change how a method is cut into pieces without changing what it does.
Use them first. Clean method boundaries are what make every other refactoring possible.
Members
- Extract Method: move a fragment into its own well-named method and call it.
- Inline Method: replace calls to a method whose body is as clear as its name with the body itself.
- Extract Variable: name a hard-to-read subexpression by assigning it to a local variable.
- Inline Temp: remove a variable that only holds a trivial expression and replace its uses with the expression.
- Replace Temp with Query: turn a computed local into a method so other methods can reuse it and extraction gets easier.
- Split Temporary Variable: give each distinct use of a reused local variable its own variable.
- Remove Assignments to Parameters: copy a parameter to a local instead of reassigning it.
- Replace Method with Method Object: turn a long method with tangled locals into a class whose fields are those locals.
- Substitute Algorithm: replace a method body with a clearer algorithm that produces the same results.
How to choose within this category
- A method is long or needs comments to explain its parts: start with Extract Method. Name each piece after what it does, not how.
- Locals prevent extraction because too many of them flow in and out: first apply Split Temporary Variable and Replace Temp with Query. If that still fails, use Replace Method with Method Object.
- One expression is dense: Extract Variable. If the value is useful elsewhere in the class, prefer a query method.
- Indirection without meaning: Inline Method or Inline Temp. They are the inverses of the two extractions above; use them to undo a bad split before re-cutting it.
- A parameter changes value inside the body: Remove Assignments to Parameters. In languages that allow
finalorconstparameters, mark them so after the change. - The whole thing can be done in fewer lines, often with a standard library call: Substitute Algorithm. Have tests that pin current behavior before you swap it.
Watch performance claims: replacing a temp with a query repeats the computation. That rarely matters, but measure in hot loops.
See also
- Moving Features between ObjectsTechniques that relocate methods, fields and whole classes so each responsibility lives next to the data it uses.
- Simplifying Conditional ExpressionsTechniques that flatten, name, merge and replace conditionals so branching logic reads as a list of plain decisions.
- Long MethodA function that does several jobs in a row and has grown too long to read, name or test as one unit.
- Duplicate CodeThe same logic appears in two or more places, so every fix and change must be made several times and the copies drift apart.
Original writing and examples. Topic coverage follows Refactoring.Guru, which explains this topic with its own illustrations.