Refactoring 006 - Rename Result Variables

Refactoring 006 - Rename Result Variables

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

Maxi Contieri
·Jun 15, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Problems Addressed
  • Related Code Smells
  • Steps
  • Sample Code
  • Type
  • Why code is better?
  • Tags
  • See also
  • Credits

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.

 
Share this

Impressum

Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)maximilianocontieri.com