Thread: Global variables? Bad! Yes, but to what extent?

  1. #1
    Registered User
    Join Date
    Mar 2002

    Global variables? Bad! Yes, but to what extent?

    I have not so much a question of code knowledge, but one for someone with lots of experience in managing a project.
    So far, most people I read from say that global variables are a bad idea that clog up your project as it grows. Would it, however, not be a good practice to keep one, and just one header file (named accordingly, like globals.h) to hold (for example) 5 global pointers to the 5 class instances that together control the flow of the program and that also need to be accessed from everywhere in the program?
    A bit more substantial: right now, I have a logfile class, and I need to be able to send a message to (call a function for) this class from everywhere in the program, but it seems idiotic to pass a pointer to every single function in the program to keep track of a single object. So, would a global pointer be a good idea here, or should they be avoided altogether?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    In that case, you probably would use a global. The problem with the overuse of globals is that they can make the code unreadable, if only because someone examining a function, say, would have to navigate thru the code just to figure out what is going on there. But no, in general, other than the rule of thumb that you should make the global names unique ( ie: not 'x' or something!!), using global variables sparingly is fine and sometimes preferable. BTW: Microsoft's Windows API is the King of Globals, if you hadn't already noticed...
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    If you ask me I have nothing against global variables.
    I'm still new to c++, but have around 8 years worth of experience developing other programming languages. Did CC++ (if you know what it is... better, what it was) for 2 years and been under VB crap ever since.

    Most say global variables are messy and work up space in memory that may as well be rarely used. But these same people are sometimes the ones who prefer to create a [1000] array just because they don't want to bother with creating a dynamic one.

    No matter what. They should be considered a feature of the programming language and understood as mildly annoying. Like you, I can live with 5 annoying critters on my code.
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  4. #4
    Registered User
    Join Date
    Mar 2002
    That's what I needed to know. Thanks!

  5. #5
    Join Date
    Aug 2001
    Groningen (NL)
    I try to avoid global variables as much as possible, sure, sometimes they are very useful. But just as with many features of programming languages, they be used in the wrong way.

    If you use too much global variables, it will make your code less readable and less maintainable.

    One important thing of software, in my opinion, is reuse of software. In order to make software very reusable, it must not rely on global variables which will maybe not present in other applications.

    So use global variables with care.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    local variables are meant to expire. if your variable will never expire for the life of the program, make it global if it doesn't impair its readability.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    ... and if I do use global vars, I try and name them in such a way that's it's obvious they are global. For example, I prefix them with gl_.


    This way, when someone is reading through a function, it (hopefully) makes it clearer where the variables are from.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dark side of global variables
    By MK27 in forum C Programming
    Replies: 18
    Last Post: 12-09-2008, 03:14 AM
  2. global variables.
    By vapanchamukhi in forum C Programming
    Replies: 5
    Last Post: 09-15-2008, 05:02 AM
  3. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  4. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  5. Global Variables?
    By Da-Spit in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2002, 08:05 AM