How to refactor global variables from your code

The discussion of global variables and their misuse seems to have a certain dogmatic tone. I am not here to challenge the concept of "globals are bad," because it makes sense to me why they are bad. However, I was wondering if people have interesting snippets of code that demonstrate how to efficiently reorganize higher regions and objects from code. In this question, I am looking for examples or patterns of common but useful solutions for "I need to use a global variable here because it's easy."

Here is a hypothetical and possibly contrived example. I use a global variable to track the parameters sent to the function. And then, if a failure occurs that happens further down the chain, I can go back and call the function again using the parameters of 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); } } } 
+4
source share
3 answers

I have no fragment, but I have an example of the real world. The linear calibration constants (mass spectrometry field) in the applications were global, and there was complex code for storing and restoring global calibration constants for different spectra. The use of two values โ€‹โ€‹was common over the program, and it was difficult to change or verify that the conversion between uncalibrated and calibrated mass values โ€‹โ€‹using two constants were correct in all cases.

I will reorganize by encapsulating the two calibration constants in the class, which is responsible for converting uncalibrated and calibrated mass values. Functions to perform the conversion was also introduced, so it was centralized in one place in the program, and not distributed throughout the program. This encapsulation later facilitated the introduction of a new type of calibration (non-linear).

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

+10
source

A quick fix would be to add all of your global variables inside one huge object, possibly with several children to separate data groups. With all these variables in one object, you only need one global variable to store this object. Then all your code will refer to the variables in this object, and not to global variables.

The next step is to get rid of this single global object. And it should be easier than getting rid of several hundred global variables. This can be done by changing it to an additional parameter that you pass to other methods.

As soon as all the global data disappears, you can think about reorganizing your code, trying to optimize this general object, for example. dividing it into several smaller objects. But, moving everything inside one object, you all simplify the management.

+4
source

The answer usually lies in the architecture of your program. You can design so that global variables are absolutely necessary, and you can design them the way you never need. Usually, you end up with a better and cleaner architecture in a later scenario, plus avoiding all the usual problems of creating unit tests for methods that rely on global variables, etc.

This question will also help.

PS In your specific scenario, a global variable is not needed at all - you can easily pass it as an argument to the addPerson method.

+1
source

All Articles