Thread: Accessing global constants

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Accessing global constants

    Do you guys think it's a good idea to always use :: before global constants, even if it's not necessary? I like it because it makes it clear that you are using a global variable, and it prevents you from accidentally using a local variable instead by mistake. I've seen some people prefix global variables with g_, but then you might as well use :: in my opinion.

    Then again, the same thing could be said for always using this-> to access member data, which a lot of people probably find to be quite ugly.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    That sounds like a terrible idea. Why should you deliberate ignore the benefits of namespaces? Namespaces were introduced precisely BECAUSE of name conflicts between globally visible symbols. Placing all global symbols in :: renders namespaces useless.

    If you have a hard time remembering that some variable is global, you could A) improve your memory, B) modify the name with a prefix or suffix, or C) eliminate the need for the global variable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Quote Originally Posted by brewbuck View Post
    Placing all global symbols in :: renders namespaces useless.
    How is that? If you have a variable x in A then use A::x instead.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would not say that it renders namespaces useless, but rather, it fails to make good use of namespaces.
    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

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I'm not writing code that will used by anyone else, so I don't care about polluting the global namespace.

    How exactly does it fail to make good use of namespaces then?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Memloop
    I'm not writing code that will used by anyone else, so I don't care about polluting the global namespace.

    How exactly does it fail to make good use of namespaces then?
    You are dumping names in the global namespace. If you are fine with that, so be it, it is your problem when you eventually realise your lack of foresight.
    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

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Using globals should be pretty rare, so if you find yourself using them enough to feel you need some sort of convention for them, then you are probably over using them.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Memloop View Post
    I'm not writing code that will used by anyone else, so I don't care about polluting the global namespace.

    How exactly does it fail to make good use of namespaces then?
    You perform the way you practice. Practice like nobody will ever find value in your code, and your code will always be valueless.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The only reason I could see for using namespaces in this particular case is if I'm linking against some poorly written library that is polluting the global namespace with commonly used names.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Memloop View Post
    Do you guys think it's a good idea to always use :: before global constants, even if it's not necessary? I like it because it makes it clear that you are using a global variable, and it prevents you from accidentally using a local variable instead by mistake. I've seen some people prefix global variables with g_, but then you might as well use :: in my opinion.

    Then again, the same thing could be said for always using this-> to access member data, which a lot of people probably find to be quite ugly.
    :: is ugly and bad form. I would fire anyone that used it, well, I'd give them a talking to first, and if they kept it up I'd fire them.

    I try to minimize my use of globals, if I need a lot of them, I use a global struct to contain them all. Then I only have to extern a single object to have access to all my global variables.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Quote Originally Posted by abachler View Post
    I use a global struct to contain them all.
    Is that global struct in a namespace? Otherwise, I don't see why accessing it with :: is bad form. It's a clear indication that the struct is indeed global. And if you're using it in a namespace, couldn't you just nest another namespace called "globals" or something within the main namespace instead of using a struct? The only difference would be using globals::constant instead of globals.constant.

    As far as I can tell there's no practical difference between prefixing your global variables with g_ and using ::. If there is, please enlighten me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats the best way to control a global class?
    By parad0x13 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2009, 05:17 PM
  2. TCL Scripting
    By johndejesus in forum Tech Board
    Replies: 5
    Last Post: 08-07-2009, 03:35 AM
  3. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  4. Initalizing global variables to constants
    By maxhavoc in forum C Programming
    Replies: 6
    Last Post: 06-21-2006, 11:25 AM