Global Variables

Global variable makes code difficult to maintain:

If you are using global variable in your module and there is a bug in code causing unexpected value in the global variable then one has to look into all the modules which are using the global variables in order to find the bug location. If we consider this in real world situation then different modules may be coded by different programmers. A bug related to global variable data corruption may require many programmers to look into there code in order to find the real cause.

Using global variable also effects the code reusability. One must code in such a way that it is possible to identify a code chunk which is independent to other code and interact with other code elements through a well defined interfaces (don’t go for meaning of interface as “interface keyword”, it could be a c++ interface, a groups of methods or something else).

Environment variables are there, what I can do:

Yes they are. You can always use data from it and even save data into it. This is a traditional way to provide environment specific information to your program. Environment variables are there from old Unix days. If you really need and want to access the environment variables (or indeed any global variable) try to control its access throw a function call. This Way you can expose a global variable as read only variable or can put constrain on global variable such as PATH variable can only have 256 characters. Also you may have heard about the environment (env.) variable attack by hackers. The dummy flow for the same is as follows: execute a program which is accessing env. variable without any protection; find a point in code (thorough reverse engineering, if source code is not available) where you can change value of a environment variable (say, trusted path env. Variable); set it with some new value to open backdoor for hackers; hack the system in dark.

Lessons learnt from past:

C was designed to be full of global variables. Every thing was global. Every method can access every other method. A mathematical puzzle for understanding the same: a program comprising of n function exists; calculate possible number of ways in which one function can access functionality provided by any other method? (For example, A can access B directly and also can access B indirectly as A-C-B)

Sounds bad to me! One always has more ways to make a mistake.

Why I think .NET is good:

Its not about .NET, it’s about modern languages which are always better and are made after learning from past experience. Java also has implemented a similar philosophy for global variables.

Just like “C” was better then “B” and “C++” was better then both. Modern languages have adopted to live without global things!

But then C++ came and it changed the number of possible ways. Functions related to customer are accessible to object of type customer only (not really but I guess this example explain its intensions)

In .NET world nothing is global, nothing!

Environment variables are accessed using a class provided by .NET library providers. The class takes care of all kind of security and data corruptions loopholes in the code. Class won’t suggest a solution but it will simply throw an exception, which means that your program terminates (if exception not handled) instead of corrupting a data, affecting other applications or opening a backdoor.

I got your point; there is no good thing about global variables:

Oh! I apologize for making you doubtless.

They are bad at many points in real world programming, they can also be good. For example follow the following link to know more. The point remains: if you are using a global variable then you must have a valid reason for this.

References:

http://www.delorie.com/gnu/docs/gcc/gcc_74.html

http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/gcc/explicit-reg-vars.html

http://www.psc.edu/general/software/packages/gcc/manual/gcc_88.html#SEC91

http://www.psc.edu/general/software/packages/gcc/manual/gcc_121.html#SEC124

http://www.psc.edu/general/software/packages/gcc/manual/gcc_36.html#SEC39

http://www.psc.edu/general/software/packages/gcc/manual/gcc_toc.html