Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Are global variables "bad"?

by hangon (Deacon)
on Apr 22, 2009 at 06:15 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found