DEV Community

Cover image for Refactoring 006 - Rename Result Variables
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

5 2

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 ???
Enter fullscreen mode Exit fullscreen mode

After

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

const favoriteNumberDoubled = doubleFavoriteNumber(2);

// Many instructions after

// We can use favoriteNumberDoubled knowing its semantics
Enter fullscreen mode Exit fullscreen mode

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay