This writing assignment will focus on global variables. A global variable is a variable every module in a program can see, meaning that the entire program is its scope (Gaddis, 2010). There are both advantages and disadvantages to using global variables and it is my intention to explore some of them here, starting with the disadvantages.
There seems to be a lot to be said about the disadvantages of using global variables. First and foremost the scope itself can be a disadvantage. Since the scope of a global variable is the entire program, it can be trying for one to figure out what part of a program uses what particular global variable and what exactly will be the effect of changing the value (Kenyon, 2010). This scope also makes it hard to debug a program because any statement within a program can change the value of a global variable. Imagine if a program has thousands of lines of code and you find that the wrong value is beings stored in a global variable. You would have to sift through these thousands of lines of code and find every statement that accessed the variable. Another disadvantage has to do with the ability to reuse modules of programming. If you were to design a module within a program that relied on global variables within that program, you would likely not be able to copy that same module into another program. You would have to redesign the module in such a way that it did not rely on the global variable from the first program. Yet another disadvantage is that the use of global variables in a program can make it hard to understand. Since any statement in a program can modify a global variable, you would have to be aware of every part that accesses the global variable in order to understand one particular part (Gaddis, 2010). Naming issues are one final disadvantage of using global variables. Because of their scope, global variables must each have a unique name. It is possible for a programmer to come back to...
References: Gaddis, T. (2010). Starting Out with Programming Logic & Design (Second Edition ed.). Boston, MA: Pearson Learning Solutions.
Kenyon, O. (2010, May 27). Avoiding Global Variables. Retrieved from RevUp: http://newsletters.livecode.com/may/issue93/newsletter3.php
Please join StudyMode to read the full document