Like Tree2Likes

Creating new variables causes drawing to completely stop working

This is a discussion on Creating new variables causes drawing to completely stop working within the C Programming forums, part of the General Programming Boards category; Originally Posted by ulillillia Why are global variables bad? They make it more difficult to reason about your program since ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    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.
    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

  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 12:56 PM.

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    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.

Page 2 of 2 FirstFirst 12
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, 04: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, 06:44 AM
  3. gameports stop working
    By tyler4588 in forum Tech Board
    Replies: 3
    Last Post: 05-26-2005, 11: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, 11:34 PM

Tags for this Thread


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