Kenneth Ismert
kismert at gmail.com
Thu Sep 22 17:17:49 CDT 2011
> Gustav Brock: > You make it sound as if global variables happen > to be changed by magic. They don't, because you - > the programmer - decides what (and not who) can > set the value and when. Yes, a good recap of the 'discipline' argument for using globals. But discipline is much easier to enforce for yourself than to impose on others. When there are multiple programmers on a project, opportunities for confusion multiply. Once discipline breaks down, and the implicit rules for using particular globals get ignored or misunderstood, you are toast. So, my question to you is, are there simple programmatic lines of defense one can use to protect globals against obvious kinds of misuse? I think the answer is yes, and put forth two concepts. Maybe this is just a theoretical question, but I find thinking about it worthwhile. > If nothing can change a global variable ... what > should its purpose be? Use a constant in that case. There are plenty of cases where you want 'read-once' or 'setup-once' globals, that read local machine settings, setup a global object for persistent use, etc. But in the end, I largely agree with you. Globals are only bad when they are misused. But enforcing discipline can be tricky. -Ken