---
title: "Replace Temp with Query"
type: "refactoring-technique"
slug: "replace-temp-with-query"
url: "http://localhost:3000/en/replace-temp-with-query.md"
category: "Composing Methods"
description: "Problem: You place the result of an expression in a local variable for later use in your code. Solution: Move the entire expression to a separate method and return the result from it. Query the method instead of using a variable. Incorporate the new method in other methods, if necessary."
languages: ["java", "csharp", "php", "python", "typescript"]
---
# Replace Temp with Query

> Problem: You place the result of an expression in a local variable for later use in your code. Solution: Move the entire expression to a separate method and return the result from it. Query the method instead of using a variable. Incorporate the new method in other methods, if necessary.

## Problem

You place the result of an expression in a local variable for later use in your code.

## Solution

Move the entire expression to a separate method and return the result from it. Query the method instead of using a variable. Incorporate the new method in other methods, if necessary.

## Why Refactor

This refactoring can lay the groundwork for applying [Extract Method](/extract-method) for a portion of a very long method.

The same expression may sometimes be found in other methods as well, which is one reason to consider creating a common method.

## Benefits

* Code readability. It’s much easier to understand the purpose of the method `getTax()` than the line `orderPrice() * 0.2`.
* Slimmer code via deduplication, if the line being replaced is used in multiple methods.

## How to Refactor

1. Make sure that a value is assigned to the variable once and only once within the method. If not, use [Split Temporary Variable](/split-temporary-variable) to ensure that the variable will be used only to store the result of your expression.
2. Use [Extract Method](/extract-method) to place the expression of interest in a new method. Make sure that this method only returns a value and doesn’t change the state of the object. If the method affects the visible state of the object, use [Separate Query from Modifier](/separate-query-from-modifier).
3. Replace the variable with a query to your new method.
## Relations

**Similar techniques**

- [Extract Method](/en/extract-method.md)

**Eliminates smells**

- [Long Method](/en/smells/long-method.md)
- [Duplicate Code](/en/smells/duplicate-code.md)

## Code Examples

### java

```java
// Before
double calculateTotal() {
  double basePrice = quantity * itemPrice;
  if (basePrice > 1000) {
    return basePrice * 0.95;
  }
  else {
    return basePrice * 0.98;
  }
}

// After
double calculateTotal() {
  if (basePrice() > 1000) {
    return basePrice() * 0.95;
  }
  else {
    return basePrice() * 0.98;
  }
}
double basePrice() {
  return quantity * itemPrice;
}
```

### csharp

```csharp
// Before
double CalculateTotal() 
{
  double basePrice = quantity * itemPrice;
  
  if (basePrice > 1000) 
  {
    return basePrice * 0.95;
  }
  else 
  {
    return basePrice * 0.98;
  }
}

// After
double CalculateTotal() 
{
  if (BasePrice() > 1000) 
  {
    return BasePrice() * 0.95;
  }
  else 
  {
    return BasePrice() * 0.98;
  }
}
double BasePrice() 
{
  return quantity * itemPrice;
}
```

### php

```php
// Before
$basePrice = $this->quantity * $this->itemPrice;
if ($basePrice > 1000) {
  return $basePrice * 0.95;
} else {
  return $basePrice * 0.98;
}

// After
if ($this->basePrice() > 1000) {
  return $this->basePrice() * 0.95;
} else {
  return $this->basePrice() * 0.98;
}

...

function basePrice() {
  return $this->quantity * $this->itemPrice;
}
```

### python

```python
// Before
def calculateTotal():
    basePrice = quantity * itemPrice
    if basePrice > 1000:
        return basePrice * 0.95
    else:
        return basePrice * 0.98

// After
def calculateTotal():
    if basePrice() > 1000:
        return basePrice() * 0.95
    else:
        return basePrice() * 0.98

def basePrice():
    return quantity * itemPrice
```

### typescript

```typescript
// Before
calculateTotal(): number {
  let basePrice = quantity * itemPrice;
  if (basePrice > 1000) {
    return basePrice * 0.95;
  }
  else {
    return basePrice * 0.98;
  }
}

// After
calculateTotal(): number {
  if (basePrice() > 1000) {
    return basePrice() * 0.95;
  }
  else {
    return basePrice() * 0.98;
  }
}
basePrice(): number {
  return quantity * itemPrice;
}
```

