Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refactor global variables out of your code

The discussion around global variables and their misuse seems to hold a certain dogmatic tone to it. I am not here to dispute the "globals are bad" concept as it makes sense to me why they are bad. However I was wondering if people had some interesting code snippets that demonstrate exactly how to effectively refactor higher scoped variables and objects out of the code. In this question I am looking for examples or patterns of generic but useful solutions to the "I need to use a global variable here because it is easy" problem.

Here is a hypothetical and perhaps contrived example. I am using the global variable to keep track of the parameters sent to a function. And then if there is a failure that happens further down the chain I could go back and call the function again using parameters from the global variable.

public var myGlobalState:Object = new Object();

public function addPerson (name:String, person:Object, personCount:int, retryCount:int):void
{
     myGlobalState = null; // Clear out old values

     myGlobalState = new Object();
     myGlobalState.name = name;
     myGlobalState.person = person;
     myGlobalState.personCount = personCount;
     myGlobalState.retryCount = retryCount;

     person.userId = personCount + 1;
     person.name = name;

     savePerson(person);
}

public function savePerson (person:Object):void
{
    // Some code that attempts to save the person object properties to a database...
    // The process returns a status code for SUCCESS of FAILURE.

    // CODE TO SAVE TO DATABASE ....

    // Return status code...

    if (status == "fail")
    {
        // Retry at least once by calling the addPerson function again

        if (myGlobalState.retryCount < 3)
        {
            addPerson (myGlobalState.name, person, myGlobalState.personCount, myGlobalState.retryCount);
        }
    }
}
like image 543
Gordon Potter Avatar asked Sep 04 '09 11:09

Gordon Potter


People also ask

Can you modify global variables?

Functions can access global variables and modify them. Modifying global variables in a function is considered poor programming practice. It is better to send a variable in as a parameter (or have it be returned in the 'return' statement).

Can you modify global variables in C?

Global variables can be accessed and modified by any function in C. Global variables can only be defined before the main() function. We can not redefine the value of a global variable in global scope however, we could access a global variable in the global scope.

How do you destroy a global variable?

To delete a global variable from the global variable list, move the cursor to the line that contains the variable name and press the Delete function key.


1 Answers

I don't have a snippet, but I have a real world example. Linear calibration constants (mass spectrometry field) in an application were global and there was complicated code to store and restore the global calibration constants for different spectra. Usage of the two values was spread all over the program, and it was difficult to change or check that conversion between uncalibrated and calibrated mass values using the two constants was correct in all cases.

I refactored by encapsulating the two calibration constants in a class that had the responsibility of converting between uncalibrated and calibrated mass values. Functions to do the conversion were also introduced so it was centralised in one place in the program instead of being spread all over the program. This encapsulation later made it easy to introduce a new kind of calibration (not linear).

Instead of accessing the two global variables the class that represented a spectrum would instead have and use an instance of the new calibration class, each instance with its own set of calibration constants.

like image 176
Peter Mortensen Avatar answered Nov 06 '22 03:11

Peter Mortensen