Thread: GLobal variables

  1. #1
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176

    Unhappy GLobal variables

    Most people say global variables are bad, (true) but my book says cout<<"" and cin>> are global variables.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    And eventually you realize C++ is a huge mess .

    They also say that you shouldn't make operators do things that aren't related to how the operator works with native types, but what does outputting and inputting data (<< and >>) have to do with left and right bitshifts?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A global variable that serves many programs could hardly be called bad. What is bad is overusing or misusing them, usually in a single program.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    adding to what Sebastiani said...

    global variables are not recommended because it is a hard error to debug if an error arises. global constants are fine though, because the program can't change to value.

    for instance, if a function in a program changes the value and it isn't supposed to, then this error is usually difficult to find and correct. however, if it is a global constant, the value cannot be changed, and therefore, reduces the possibility of error.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: GLobal variables

    Originally posted by fuh
    Most people say global variables are bad, (true) but my book says cout<<"" and cin>> are global variables.
    Yes they're global variables, but cin and cout are part of the standard libraries which means they were written by people much smarter and better at programming than you or I (or at least, I hope that's true). Really, cin and cout are more of the exception, not the rule. My programming teacher said that in all of his years of programming, only once has he used a global variable, and that's because there was no other way he could do it.

    In short, don't use global variables unless there is absolutely no other way to do 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.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Re: GLobal variables

    Originally posted by joshdick
    only once has he used a global variable, and that's because there was no other way he could do it.
    There are always other ways

    I'd say that cout and cin are global variables mostly because of ease of use. I would prefer if they weren't globals, but in this case it really doesn't matter much. As joshdick said, the fact that it's in the standard libraries guarantees that it's properly debugged so that you don't have to worry about it much. It's just something you learn to accept. Global variables are always discouraged, but as long as you're smart about it you can limit the problems they cause. Still, don't use them... ever.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Re: Re: GLobal variables

    Originally posted by Polymorphic OOP
    There are always other ways
    Hey, man, I don't know the particulars of my teacher's situation. You'll have to take that up with him.
    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.

  8. #8
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86

    Re: GLobal variables

    Originally posted by fuh
    but my book says cout<<"" and cin>> are global variables.
    Hmm... I dont like to think of them as global variables. I like to think of them as pre-defined functions that're lots of fun to use.
    - Dean

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Re: GLobal variables

    Originally posted by LordVirusXXP
    Hmm... I dont like to think of them as global variables. I like to think of them as pre-defined functions that're lots of fun to use.
    Nope, they're global objects not functions. Well cout and cin are. << and >> are just functions.

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Then again most of programming is about solving problems, not theoretical best solutions. I've opted for global variables in situations where other methods would have worked but globals were the quickest and easiest. I'm comfortable enough with programming now that I feel I can make these decisions; as opposed to blindly listening to what-to-do and what-not-to-do.

    Admit it, not every component of every program you've make is a elegant masterpiece-- if it is, i'd bet you haven't done much.

    void main(), global variables, goto... they all have a place in my universe.

    see if you can spot the joke

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    Admit it, not every component of every program you've make is a elegant masterpiece-- if it is, i'd bet you haven't done much.
    That's pretty arrogant. Not every program has to be an elegant masterpiece to avoid using global variables. Though I do agree that it's not a great idea to just blindly accept "don't ever use them." It's really something you have to work with to understand.

    Originally posted by Eibro
    void main(), global variables, goto... they all have a place in my universe.
    I hope that "void main()" was that joke you were referring to ...

  12. #12
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Oh screw you guys, cin and cout are not global. They're only global in namespace std!

  13. #13
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    Oh screw you guys, cin and cout are not global. They're only global in namespace std!
    Ha, yeah, I always laugh when people use that excuse -- as if namespaces remedy the problem

  14. #14
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    It's really something you have to work with to understand.
    Yeah, that was pretty much my point. Plus, global variables are only one tip of the iceburg.

    I hope that "void main()" was that joke you were referring to ...
    What?

  15. #15
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    What?
    I was refering to what you wrote in your earlier reply in white -- "see if you can spot the joke"

    And I said that I hoped you were referring to void main() because if you weren't then you do realize that I would have to kill you

Popular pages Recent additions subscribe to a feed

Similar Threads

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