global variables - okay sometimes...?

This is a discussion on global variables - okay sometimes...? within the C++ Programming forums, part of the General Programming Boards category; this may not be the right board, but i didn't know what was... where should general programming topics go? anyway... ...

  1. #1
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176

    global variables - okay sometimes...?

    this may not be the right board, but i didn't know what was... where should general programming topics go? anyway...

    i started working on a pong game in allegro (i just needed a semi simple project, so i would actually finish something i started ) and started wondering if maybe global variables were okay sometimes.. for example:
    i have a struct for a paddle (containing coordinates, score, height and width...), a struct for a ball (coordinates, speed, etc), and a buffer to draw things to.. these all get used in many functions (drawing, moving, collission detection, etc...).
    so, wouldn't it be easier to just make them all global, rather than passing them around to all the different functions? i fail to see the advantage of making them local to main, and passing them around...
    it seems like it could even improve readability in some places... for example, i have a ballHit function, which checks if the ball has hit a wall, a paddle, or gotten past a paddle... if everything is local to main, i have to pass that function the ball, and two paddles... it seems like it would be better with global variables... anyone have any insights?
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  2. #2
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    Personally, I don't see much wrong with global variables at all.

    And technically, as long as the program compiles and works correctly, then whatever code you use is fine.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I agree. I don't see anything wrong with global variables as long as the code remains understandable. Unless you plan on having multiple balls bouncing around in there, I don't see a problem with keeping it global. That's my take on it, anyway.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    i suppose they're not a big deal but a lot of people don't like them.

    They are easy to get around though - just pass the variable to any function that would use it as a reference.

  5. #5
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    It is not just that global variables can be a little bit harder to read it is also that they are shared data. One function can change the variable in a way that is invisible to another function. This creates bugs that hard usually hard to find.

    Remember this is C++ not C, they are not nessicary and are usually not used because they are not nessicary.

  6. #6
    ttt
    Guest
    global variables make large programmers harder to debug, and in small programs it's okay, but still not recommended.

    It's like saying you should just write everything in main, just because it compiles.

    Anyways it's okay, but not something you should get a habit of.

  7. #7
    ttt
    Guest

    Talking

    large programs, not large programmers

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    It sounds like you're writing this program in order to learn more about programming and improve your skills. If that is the case, I woudln't suggest getting into bad habits like using global variables. As others have already said, they make larger programs very difficult to debug. However, if your aim is merely to make yet another pong clone, then whatever works works.

    I think a programmer who is still learning should get used to certain things like using only local variables. What is easy is not always best, and what is best is not always easy.

    Hmmm, I think that was a quote from someone or other, but I can't think who
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Hmmm, I think that was a quote from someone or other, but I can't think who
    No matter who said it, it is still true.

  10. #10
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by Munkey01
    No matter who said it, it is still true.
    Yeah, but if no one else said it first, I'm laying claim to it
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  11. #11
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    thanks for the input, everyone.
    this is more like another pong clone than improving my skills. i'm fairly good at programming.. not great, but good enough to know what not to do (most of the time ). i normally avoid global variables, and i think this is the first time i've used them (since i learned the difference, anyway). i'm only using them here because i think it will make the program more readable/simpler/something...
    i'm not really going for really neat and tidy code here anyway... if i were, i would probably make some classes and things.
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  12. #12
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Heh... a pong program sounds like a neat thing to make... I might try it for my own learning purposes (only I'll either use structures or classes).

    As for global variables... about the only thing I use them for is constants (like a constant number for the pong's ascii character, etc).

  13. #13
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    global variables/ cumbersome passing of many parameters - this is just itching for a class. Classes will encapsulate the data from the outside world, while giving free access to it's member functions. you've got your ball and paddle structs, well why not make a class called board or game, and have the ball and paddles be data members and have public functions for collision, hitting the ball, changing direction/speed etc. These functions will have access to the ball and paddle variables (like they were global) but they won't be accessible from outside the class. Voila! no cumbersome var passing, and no global variables.

    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  14. #14
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    And technically, as long as the program compiles and works correctly, then whatever code you use is fine.
    Until you have to maintain it later.

    I don't see anything wrong with global variables as long as the code remains understandable.
    Global variables can lead to being functions and classes being very dependent on eachother, this is not really what you want.

    and in small programs it's okay, but still not recommended.
    I agree, learn to avoid global variables, that habit will help you a lot when creating and maintaining larger software later. This does not only count for object oriented languages where things like encapsulation are important, but also in procedural languages and even functional languages data hiding is an important concept.

  15. #15
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    i finished it *claps* i think this is my first finished game, so it feels pretty good.. even if it is pong

    i ended up using local variables and passing them around, because that's the way i started the project, and didn't feel like going to global variables, as it ment changing every functions parameter list, and probably a bunch of bugs from things i forgot to change.

    >>this is just itching for a class.

    i know. i may go back and do it with classes, but it's kind of doubtful... i'll probably never look at the game again after this. it was really just something simple so that i could finish it (and i did.. yay ).

    >>Until you have to maintain it later.

    i won't

    future projects i'll most likely do with classes.

    thanks for the input everyone.
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variables.
    By vapanchamukhi in forum C Programming
    Replies: 5
    Last Post: 09-15-2008, 06:02 AM
  2. Replies: 5
    Last Post: 08-06-2008, 10:59 AM
  3. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 02:59 AM
  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, 05:37 PM

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