Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Are global variables "bad"?

by hangon (Deacon)
on Apr 22, 2009 at 06:15 UTC ( #759191=note: print w/replies, xml ) Need Help??

in reply to Are global variables "bad"?

Global variables are not bad. People who use global variables badly are bad.

Your queasy feeling may be in response to a conditioned knee-jerk negative reaction to global variables that seems to pervade this field. I don't know where it all started, but my first CS professor pounded the idea into our heads that globals are unequivocally bad. Experience taught a different lesson.

Fortunately BrowserUK, JavaFan and others provide a voice of reason in this thread. Its not the globals themselves, but how and where you use them that can cause problems. Properly applied globals can be powerful and avoid unnecessary complexity. But like other aspects of programming, proper usage requires some degree of thought and discipline.

Replies are listed 'Best First'.
Re^2: Are global variables "bad"?
by jpearl (Scribe) on Apr 22, 2009 at 13:42 UTC
    You are definitely correct about this. My first CS prof did the exact same thing. Its compounded by the fact that I've used global structures at times when they were certainly *not* appropriate. In some ways I am a little bit grateful to have a healthy level of distrust of global variables, since its making me question initially if they are the right fit for the problem. And of course it started this thread with all these great responses by you and everyone else as you mentioned.
Re^2: Are global variables "bad"?
by gwadej (Chaplain) on Apr 22, 2009 at 16:55 UTC

    Perhaps a better way to say it is not that globals are bad, but that globals are easy to use badly.

    I often find that I am suspicious when I run across global variables (and now Singletons) in code that is larger than a single script. I know (from much painful experience) that the globals are likely to be the focus of surprising behavior from changes due to unrelated code.

    Personally, I learned this not from any CS classes, but from being bitten a few times too many.

    G. Wade

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://759191]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-10-16 01:08 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (82 votes). Check out past polls.