Thread: A Case For Global Variables.

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    104

    A Case For Global Variables.

    I was in the process of initializing the single member of a structure in my header file.The brace initialization failed because I got the syntax wrong, but that is not the point. The error made me wonder why was I using a structure in the first place, when a single static const global array would do the job just fine, and for the case, I'd dare to say even better; although the static part is questionable since this is a header file. I have to look up how different storage specifiers affect globals in both source and headers.

    We are indoctrinated with the idea that global variables are evil, so much that we forget they exist at all. I think this is a mistake. They're a tool in our arsenal, and while is true that if used improperly they can lead to problems and headaches, completely barring their use should not be the answer.

    I think a structure's purpose is to group together similar data in a both a logical and practical manner, such that future handling of this data is easier and convenient. I'd argue that creating a structure that contains a single read-only member does not make sense, at least not from an imperative programming point of view. And such in this case, a global variable is warranted, and a better alternative.

    What do you guys think?
    I welcome the discussion, feel free to change my mind, etc.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    No expert has "forgotten" about global variables. No expert is "completely barring their use". If you are "indoctrinated with the idea that global variables are evil" that is your personal problem. Of course they are a "tool in our arsenal".

    We tell beginners not to use them because they cannot determine when it's appropriate and they tend to use them as a crutch to avoid passing variables. They even sometimes declare what should be local variables as globals (the global i index variable is the most idiotic).

    Global program parameters set from the command line are very useful. You can hide them in a struct to vastly lessen the chance that they are accidentally altered or shadowed.
    Code:
    struct Params {
        ...
    } params;
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I also note that a global constant is a rather different creature from a global variable. Global variables introduce global state such that in every function call, you might have this sneaking suspicion that the global state might be changed when you didn't expect it. Global constants don't add to global state, so they don't make it any more difficult to reason about your program than a local constant (other than maybe when you're trying to manually look up the constant's value).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    We tell beginners not to use them because they cannot determine when it's appropriate and they tend to use them as a crutch to avoid passing variables.
    That seems perfectly reasonable. I guess it depends on the instructor. I was never given an option, I was just told they were the evil incarnate and never to use them without exception.
    @laserlight, You are correct of course.
    I try to be a const nazi whenever I can, const'ing everything that I'm not going to modify. Surprisingly this gets me in trouble sometimes; a recent example: const time_t tm = time(&tm); made the compiler cry out. I retained the const but had to cast it away when passing it to time()

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Dren View Post
    I try to be a const nazi whenever I can, const'ing everything that I'm not going to modify. Surprisingly this gets me in trouble sometimes; a recent example: const time_t tm = time(&tm); made the compiler cry out. I retained the const but had to cast it away when passing it to time()
    In this specific case, you can do "const time_t tm = time(NULL);" and forget about casting away the const.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with switch case messing up case variables
    By mp59 in forum C Programming
    Replies: 3
    Last Post: 07-02-2018, 12:12 PM
  2. global variables
    By Lisyus in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2011, 08:10 PM
  3. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  4. global variables in c++
    By DoMeN in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2007, 04:47 PM
  5. Static or global in this case?
    By Ganoosh in forum Windows Programming
    Replies: 6
    Last Post: 07-14-2005, 11:08 PM

Tags for this Thread