7
$\begingroup$

I was taught in my freshman college computer programming course that using global variables is almost always a bad idea. However, I have found that when designing Fortran programs for very complex simulations, such a practice seems to make things easier; for instance, I never have to worry about passing variables to various subroutines.

My question is am I missing something? Will my bad programming practice eventually come back to harm me?

$\endgroup$
  • 4
    $\begingroup$ In my experience, it is not inherently bad (otherwise it would be removed), it's just misused by programmers. It is quite easy to create bugs (and difficult to debug) if global variables are changed within functions. My advice is to keep global variables constant and to pass variable fields as function arguments. $\endgroup$ – nluigi Dec 18 '15 at 21:26
  • 8
    $\begingroup$ Will my bad programming practice eventually come back to harm me? If you complete your studies quickly enough you should find that your bad programming practice harms a later generation of programmers trying to make head or tail of your code. That's how it goes in Fortran-land. $\endgroup$ – High Performance Mark Dec 18 '15 at 22:56
  • $\begingroup$ There is a related question on Programmers.SE at programmers.stackexchange.com/a/148109, those reasons apply equally well to computational science. $\endgroup$ – Kirill Dec 19 '15 at 3:40
10
$\begingroup$

Benefits of organizing your code to avoid global variables include better control over how variables change. Nothing worse than hunting down how a global variable is erroneously getting changed or stepped on. Especially important for large complex systems which pull in libraries or are published as a library for use in other people's code. Imagine the nightmare of you pulling in a library which has global variables with same names as your global variables ... yikes !!!

Possible solutions include creating an enclosing construct (language specific : objects, classes or structs come to mind) to store these variables and to only allow changes to these variables by making calls to the construct's exposed functions.

$\endgroup$
  • 4
    $\begingroup$ I did some work with a fortran code that had 1500 lines of common blocks that were included in every single subroutine, so everything except for loop indexes were effectively global. Finding a bug was a multi-month process every time. $\endgroup$ – Godric Seer Dec 18 '15 at 23:55
  • $\begingroup$ Thanks for your advice, I'll now modularize my program to eliminate global variables. $\endgroup$ – Craig Feinstein Dec 21 '15 at 17:02
4
$\begingroup$

You'll almost certainly get bitten unless they're read-only or unless your code is trivial, in which case it probably wouldn't be scientific computing.

Don't try to save typing when you're programming; try to save development and debugging time.

$\endgroup$
4
$\begingroup$

Your view is colored by the fact that you are using Fortran (I assume Fortran 77) where you have only two ways to access variables: (1) they are global variables in some COMMON block, or (2) they have been passed as arguments (typically scalar) to the current function.

Since 1977, this has been addressed in many ways in all more modern programming languages. An example is the use of structures in C/C++ (composite types or records in other languages) that allow you to group many variables into one and only pass a reference to one structure to a function as an argument. Or, more elegantly in most object oriented programming languages, to group things together in classes and then have a reference or pointer to such a class object passed implicitly to every member function. These techniques have made dealing with the problem you see unimportant and eliminate the incentive to use global variables.

The reasons not to use global variables have already been given in other answers. My personal favorite among the reasons is that one of the best strategies for reducing the potential for bugs is to limit the scope of variables. A variable that lives only in a very small part of the program can not create trouble elsewhere. So my goal is often to declare any variable as late as possible and destroy it as early as possible, simply because that makes it so much easier to read and understand code. On the other hand, global variables violate this goal in the most egregious possible way.

$\endgroup$
  • $\begingroup$ I'm actually using the latest version of Fortran, which has more advanced structures - modules, pointers, etc. $\endgroup$ – Craig Feinstein Dec 22 '15 at 17:33
  • $\begingroup$ I see. In that case, you can of course get away with not having to pass many variables to your subroutines as long as their part of your object. $\endgroup$ – Wolfgang Bangerth Dec 23 '15 at 2:53
1
$\begingroup$

Anything that can be done with a global variable can be done without a global variable.

But as code grows, global variables take on a life of their own and in some very bad cases will totally demodularize the code so that understanding any particular subroutine requires complete understanding of the entire codebase; changes to globals could conceivably happen anywhere at any time.

Therefore I think learning to code without globals such that it is instinct and not seen as the harder alternative is the best approach.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.