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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay