---
title: "Inline Temp"
type: "refactoring-technique"
slug: "inline-temp"
url: "http://localhost:3000/en/inline-temp.md"
category: "Composing Methods"
description: "Problem: You have a temporary variable that’s assigned the result of a simple expression and nothing more. Solution: Replace the references to the variable with the expression itself."
languages: ["java", "csharp", "php", "python", "typescript"]
---
# Inline Temp

> Problem: You have a temporary variable that’s assigned the result of a simple expression and nothing more. Solution: Replace the references to the variable with the expression itself.

## Problem

You have a temporary variable that’s assigned the result of a simple expression and nothing more.

## Solution

Replace the references to the variable with the expression itself.

## Why Refactor

Inline local variables are almost always used as part of [Replace Temp with Query](/replace-temp-with-query) or to pave the way for [Extract Method](/extract-method).

## Benefits

* This refactoring technique offers almost no benefit in and of itself. However, if the variable is assigned the result of a method, you can marginally improve the readability of the program by getting rid of the unnecessary variable.

## How to Refactor

1. Find all places that use the variable. Instead of the variable, use the expression that had been assigned to it.
2. Delete the declaration of the variable and its assignment line.
## Relations

**Helps you do**

- [Replace Temp with Query](/en/replace-temp-with-query.md)
- [Extract Method](/en/extract-method.md)

## Code Examples

### java

```java
// Before
boolean hasDiscount(Order order) {
  double basePrice = order.basePrice();
  return basePrice > 1000;
}

// After
boolean hasDiscount(Order order) {
  return order.basePrice() > 1000;
}
```

### csharp

```csharp
// Before
bool HasDiscount(Order order)
{
  double basePrice = order.BasePrice();
  return basePrice > 1000;
}

// After
bool HasDiscount(Order order)
{
  return order.BasePrice() > 1000;
}
```

### php

```php
// Before
$basePrice = $anOrder->basePrice();
return $basePrice > 1000;

// After
return $anOrder->basePrice() > 1000;
```

### python

```python
// Before
def hasDiscount(order):
    basePrice = order.basePrice()
    return basePrice > 1000

// After
def hasDiscount(order):
    return order.basePrice() > 1000
```

### typescript

```typescript
// Before
hasDiscount(order: Order): boolean {
  let basePrice: number = order.basePrice();
  return basePrice > 1000;
}

// After
hasDiscount(order: Order): boolean {
  return order.basePrice() > 1000;
}
```

