# Composing Methods

> Techniques that split, merge and reshape method bodies so each method does one readable thing and local variables stop blocking change.

- kind: category
- category: refactoring
- canonical: https://aipatterns.dev/refactoring/techniques/composing-methods
- inspired-by: https://refactoring.guru/refactoring/techniques/composing-methods

## 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](https://aipatterns.dev/refactoring/techniques/extract-method.md): move a fragment into its own well-named method and call it.
- [Inline Method](https://aipatterns.dev/refactoring/techniques/inline-method.md): replace calls to a method whose body is as clear as its name with the body itself.
- [Extract Variable](https://aipatterns.dev/refactoring/techniques/extract-variable.md): name a hard-to-read subexpression by assigning it to a local variable.
- [Inline Temp](https://aipatterns.dev/refactoring/techniques/inline-temp.md): remove a variable that only holds a trivial expression and replace its uses with the expression.
- [Replace Temp with Query](https://aipatterns.dev/refactoring/techniques/replace-temp-with-query.md): turn a computed local into a method so other methods can reuse it and extraction gets easier.
- [Split Temporary Variable](https://aipatterns.dev/refactoring/techniques/split-temporary-variable.md): give each distinct use of a reused local variable its own variable.
- [Remove Assignments to Parameters](https://aipatterns.dev/refactoring/techniques/remove-assignments-to-parameters.md): copy a parameter to a local instead of reassigning it.
- [Replace Method with Method Object](https://aipatterns.dev/refactoring/techniques/replace-method-with-method-object.md): turn a long method with tangled locals into a class whose fields are those locals.
- [Substitute Algorithm](https://aipatterns.dev/refactoring/techniques/substitute-algorithm.md): 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](https://aipatterns.dev/refactoring/techniques/extract-method.md). 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](https://aipatterns.dev/refactoring/techniques/split-temporary-variable.md) and [Replace Temp with Query](https://aipatterns.dev/refactoring/techniques/replace-temp-with-query.md). If that still fails, use [Replace Method with Method Object](https://aipatterns.dev/refactoring/techniques/replace-method-with-method-object.md).
- **One expression is dense:** [Extract Variable](https://aipatterns.dev/refactoring/techniques/extract-variable.md). If the value is useful elsewhere in the class, prefer a query method.
- **Indirection without meaning:** [Inline Method](https://aipatterns.dev/refactoring/techniques/inline-method.md) or [Inline Temp](https://aipatterns.dev/refactoring/techniques/inline-temp.md). 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](https://aipatterns.dev/refactoring/techniques/remove-assignments-to-parameters.md). In languages that allow `final` or `const` parameters, mark them so after the change.
- **The whole thing can be done in fewer lines, often with a standard library call:** [Substitute Algorithm](https://aipatterns.dev/refactoring/techniques/substitute-algorithm.md). 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.
