Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Global variables.

  1. Sep 15, 2011 #1

    I just want to know, if you're writing a reasonably short program (~500 lines), and you're basically using a global variable as a const (user inputs it once, and you wont ever change it), then is it ok?
  2. jcsd
  3. Sep 15, 2011 #2


    User Avatar

    Staff: Mentor

    As far as I'm concerned, it's OK. As far as your instructor's or employer's coding standards are concerned, it may or may not be OK.
  4. Sep 16, 2011 #3


    User Avatar
    Science Advisor

    If you have some set of global variables, it might be an idea to put them into some kind of global data structure, or at least have them be accessible by a global interface of some sort.

    It might seem like overkill, but if you start using a tonne of these variables, and you need some way to differentiate between different versions of the software and have issues with compatibility, then by using different context data structures, you can manage problems that might arise.

    I acknowledge that you said your program is small, but in many cases small programs can become larger programs (hence my post).

    My suggestion for you is if you ever need a standardized way of managing global objects (i.e. data), then you should use a resource manager that allows you to create and manage data structures global to your process. It is overkill for your problem, but you might find if you ever have to make or deal with complex code repositories, it will help you immensely.
  5. Sep 16, 2011 #4
    Do you suggest something like this?

    class Number
    int x;

    int get_x();
    } ;

    And call the user to input the value in the constructor? I don't think x could be changed in this case once initialized.

    P.S. Keep in mind I don't have much programming experience.
  6. Sep 16, 2011 #5
    What exactly is the goal you are trying to achieve? If you want to learn object oriented programming then such a construct may be sensible for the sake of learning to program classes. In this case you will then notice that you merely replaced your global variable of type integer with a clumsy construct and a global variable of type "Number", and can then consider even more abstract constructs like singletons which make the trivial task of having a value present everywhere in your program even more complicated. If you want to write a small program that solves your problem at hand then stick to your global variables, they are perfectly fine unless someone who actually knows your problem/program (and has the power to fire you; see jtbell's comment) tells you otherwise.
  7. Sep 16, 2011 #6


    User Avatar
    Homework Helper

    I think he meant using a class if you have a significant number of global variables, not just a few global variables. The main advantage of using a function to access a variable is that it allows you to change the variable (within reason) without changing what that function returns. Maybe you change a variable from using english units to metric units, but the function continues to return english units for compatability with the existing code. If it's unlikely that you'll ever change the global variable, there's no need to use a function to access it.

    A resource manager only makes sense if you're using some common resource like windows user interface stuff, where a resource manager would exist. If you have some custom table(s) of constant data, it's probably best to implement it (them) as a global array(s) of const structures.

    As mentioned above, it depends on the standards of your class or your work environment and/or the particular situation. Personally, I don't like the idea of adding a parameter to access a variable to nearly every function in a program to avoid using a global variable, as it's uneeded overhead, and since the functions may use a different name for what should otherwise be a global variable, it makes it difficult to do a simple text search in a source tree to find out all the places a global variable is accessed.
  8. Sep 16, 2011 #7


    User Avatar
    Science Advisor

    Hi rcgldr

    The resource manager that I am talking provides an interface to access any global shared data either by its pointer, or by its interface.

    I agree with you that if you just have a tonne of constants, then you could just collect them up into a single data structure, but if the number of constants is large, then my view is that you should probably transform your program to take those constants and make them external data in a file which now makes them non-constant and derived from some data file. You could structure the interface so that you could not change the underlying data (which is pretty easy to do), if you really wanted to maintain the 'const

    The resource manager I am talking about is something that manages all of the global resources in your program and part of that deals with resource address translation, which is simply the act of getting a pointer to the object or its interface given some kind of tag or index (e.g. an integer value, or a string).

    To me personally, it doesn't make sense to have any program with a large amount of constant variables. You might as well make the whole thing data driven, and with regards to speed, yes I agree there will be overhead, but unless you need every clock cycle you can take, it becomes less of an issue since hardware is getting a lot better every year.
  9. Sep 16, 2011 #8


    User Avatar
    Homework Helper

    I've never used a "generic" resource manager. The only resource manager's I've worked with were used to generate user interface stuff, icons, menus, windows, ... . I wasn't aware that generic resource managers even existed.

    The situation I'm thinking of is one where you have one or more large tables of data. These could be handled via external files, but then you'd have to include the code to parse the files each time. Also if you needed to modify a table, it's much easier to text edit a source or include file than to hex edit some binary data file.

    If the table is often updated, then it would make sense to create a utility or to use some database type software to maintain the table, and to export the table in some program friendly format if the data needs to be handled by non-database software.

    I was also considering the overhead of coding time; the overhead of what I would consider undeeded extra coding done just to avoid using global variables in situations where it would make more sense to use global variables.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook