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);
}
}
}
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With