Avoiding global variables

This is a discussion on Avoiding global variables within the C++ Programming forums, part of the General Programming Boards category; I read that it's a good idea to keep the number of globals in your project as low as possible. ...

  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,789
    Have a search for the "Singleton" pattern

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Make use of function parameters, passing by (const) reference, or passing pointers, if necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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,789
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    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
    21,781
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,789
    >>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, 03:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21