Thread: Creating new variables causes drawing to completely stop working

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ulillillia
    Why are global variables bad?
    They make it more difficult to reason about your program since an apparently unrelated function call (or other piece of code) could change a global variable. This increase in coupling across various parts of your program also makes it harder to reuse parts that may otherwise be separate.
    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

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    They make it more difficult to reason about your program since an apparently unrelated function call (or other piece of code) could change a global variable. This increase in coupling across various parts of your program also makes it harder to reuse parts that may otherwise be separate.
    Not to mention problems with name collisions, misspelling, reuse, etc.

    I have a Windows API program I'm working on right now, it involves 12 files, passing the 10,000 lines of code mark and in that I have less than 20 globals and those are window handles, instance handles, and the programs settings struct. These pretty much have to be globalized so that code in other files can access the controls in the main window. Everything else is either local or passed into functions. (And fwiw... many programmers would class this as a "small" project.)
    Last edited by CommonTater; 09-15-2011 at 11:56 AM.

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ulillillia View Post
    Why are global variables bad?
    Read this: Global Variables Are Bad
    I'm using as few as I can.
    Unless you're using zero, this is probably not true.
    The bulk of the global variables is image data. These are in the form of arrays.
    Image data and arrays don't get special status.
    About 100 to 150 of the others are actual variables, nearly all of which initialized.
    Good, so you got about 100 to 150 of the definitions correct.
    I use globals when multiple functions need to reference them.
    This is also known as the "I'm too lazy to pass them" argument. Declare them locally, pass them to functions as needed. Group related stuff in structs if need be, to keep it manageable. At the very least, create a singleton object with global data inside that, so access to it is well controlled.
    I sometimes use it for debugging, for conditional breakpoints.
    Debugging is actually easier without globals, and conditional breakpoints work just as well, or better, when only using local variables.
    There are other such cases as well.
    I'd be curious what other such cases are good cause to break one of the oldest tenets of good programming practice.
    I've got about 3 times more local variables than I do globals.
    So you have 25% of your variables to move to local functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sort isnt completely working
    By jamort in forum C++ Programming
    Replies: 6
    Last Post: 08-09-2010, 03:05 AM
  2. Creating a window causes a trackbar to stop working?
    By kidburla in forum Windows Programming
    Replies: 2
    Last Post: 09-20-2007, 05:44 AM
  3. gameports stop working
    By tyler4588 in forum Tech Board
    Replies: 3
    Last Post: 05-26-2005, 10:38 AM
  4. keyboard stop working at Dos
    By arian in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2004, 02:32 PM
  5. Pause in OpenGL, but don't stop drawing... ?
    By Arker in forum Game Programming
    Replies: 4
    Last Post: 10-16-2003, 10:34 PM

Tags for this Thread