Thread: Avoiding global variables

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    Avoiding global variables

    I read that it's a good idea to keep the number of globals in your project as low as possible.
    now what would be good ways to avoid global variables?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Have a search for the "Singleton" pattern

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Make use of function parameters, passing by (const) reference, or passing pointers, if necessary.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Fordy View Post
    Have a search for the "Singleton" pattern
    Is this really much better than globals?
    It's also sometimes known as the anti-pattern by people who don't like it.
    It has been criticized by experts too, IIRC.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Is this really much better than globals?
    It's also sometimes known as the anti-pattern by people who don't like it.
    It has been criticized by experts too, IIRC.
    It is a perfectly legit pattern and is also in the gang of four design patterns book. There are times, like any other pattern, where it is used when another pattern would work just as well. However, I would not claim singleton is a bad design pattern.

    It has been criticized by experts too, IIRC.
    Fortunately said experts don't see my code so I could care less. Exclusionary thinkers annoy me to no end.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by Elysia View Post
    Is this really much better than globals?
    It's also sometimes known as the anti-pattern by people who don't like it.
    It has been criticized by experts too, IIRC.

    I couldn't care less about such "experts". It works well and solves some problems of resource lifetime that global objects can suffer from.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. Well, I was just raising concern.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If we return to the context of the original question:
    Quote Originally Posted by FpaFtw
    I read that it's a good idea to keep the number of globals in your project as low as possible.
    There are several reasons for this, and some of them have to do with avoiding global state, e.g., it may be more difficult to reason about the program and reusability may be impaired. In such cases, applying the singleton pattern to get rid of a global variable does not actually make an improvement. On the other hand, if the global variable is supposed to be a singleton, then applying the singleton pattern to enforce this makes sense.
    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

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>In such cases, applying the singleton pattern to get rid of a global variable does not actually make an improvement. On the other hand, if the global variable is supposed to be a singleton, then applying the singleton pattern to enforce this makes sense.

    That's actually a good point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM