Refactoring 006 - Rename Result Variables

Refactoring 006 - Rename Result Variables

'Result' is a very bad generic name. Just Fix it

TL;DR: Use the last call as a semantic guide.

Problems Addressed

  • Bad naming on variables

Related Code Smells

Steps

  1. Name the variable with the same name as the last function call.

Sample Code

Before

function doubleFavoriteNumber(n) {
    return this.favoriteNumber * n;
}

var result = doubleFavoriteNumber(2);

// Many lines after we have no idea what does 
// result holds

// var result ???

After

function doubleFavoriteNumber(n) {
    return this.favoriteNumber * n;
}

const favoriteNumberDoubled = doubleFavoriteNumber(2);

// Many instructions after

// We can use favoriteNumberDoubled knowing its semantics

Type

[X] SemiAutomatic

As with many name heuristics, we can replace the variable with another refactor rename variable

Why code is better?

A variable scope can last a lot.

Assignment and usage might be very far away from each other.

Tags

  • Naming

See also

What is in a name?

Credits

Image by HeungSoon on Pixabay


This article is part of the Refactoring Series.