Replace Method with Method Object

You have a long method that uses local variables in such a way that you cannot apply Extract Method

Turn the method into its own object so that all the local variables become fields on that object. You can then decompose the method into other methods on the same object.

class Order...
double price() {
  double primaryBasePrice;
  double secondaryBasePrice;
  double tertiaryBasePrice;
  // long computation;


image/svg+xml Order price() Price Calculator primaryBasePricesecondaryBasePricetertiaryBasePrice compute 1 return new PriceCalculator(this).compute();


Using a Static Method

You can do this refactoring with a static method, but in this case you don't need the first field pointing back to the original object.

Alternative Steps with Temps

Marnix Klooster suggested an alternative to the mechanics. Rather than creating fields in the Method Object for all the temps, leave the temps as temps and only turn them into fields as you need to in order to use Extract Method. It means you have to use a special form of Extract Method, but may help in reducing the scope of the temps. Of course you can use Reduce Scope of Variable afterwards on any temp that's only used in one method.