From: Steve Litt Date: 2006-01-09T12:50:03+09:00 Subject: Re: global variables are bad?? On Sunday 08 January 2006 09:33 pm, John Maclean wrote: > Chaps, > > I'm not sure were I have read this but I recall a page or two saying that > global variables are bad for some reason. I've overcome my main hurdle, of > understanding some of the basics of OO. > > There's loads of good code in the following directory:- > /usr/share/doc/ruby-tcltk-1.8.4/tk/sample/demos-en/ > which I have used to get me up to speed on widgets and such. > > > I've seen loads of global variables in > /usr/share/doc/ruby-tcltk-1.8.4/tk/sample/demos-en/... > care to guide a novice on why global variables are used here? Hi John, The global vars in the code you quoted were used not as variables, but as constants. One wouldn't want to pass all those vars in and out of every subroutine. As used in the code you quoted, I see nothing wrong with them, because in spirit, if not in the letter of the law, they were used as constants, never again to be modified. When I have tons of config info like that I usually wrap it in a single object, which might be global or local and passed, but it's all in one place. I find that easier to deal with for config info, even if it's intended to be constant. Global variables used as variables can be a debugging nightmare because every single line of code has write access to them, meaning that if a variable "goes bad", it's very hard to find where it "went bad". You basically need to trace through the whole program. Global variables also make a program much less modular. In my opinion they're the data equivalent of "spaghetti programming" (http://en.wikipedia.org/wiki/Spaghetti_code). SteveT Steve Litt http://www.troubleshooters.com slitt@troubleshooters.com