Kenneth Ismert
kismert at gmail.com
Fri Sep 23 15:52:12 CDT 2011
Good advice. The points I want to emphasize are: * Write privately, read publicly is inherently safer scheme than globals anybody can change * A small number of carefully defined globals are much more managable than the practice of indescriminately using globals by default * Coding for the smallest scope possible, while indisputably good practice, is also a good way to limit the harm abuse of globals can cause -Ken jwcolby: > > The only advantage I see to the wrapper is that you get to set a > breakpoint. > > A little more help that that. You can add code that logs the change and > the time of day, the workstation running the code or whatever you can think > of that might help you troubleshoot the issue. > > You can add code that limits the values that can go into the variable. > Perhaps the global is a date and the date should never be before D day, but > somehow it is being set to the date of black friday. Perhaps it is a > currency and should never be negative. Perhaps it is a long that should > only be between 1000 and 10000. Perhaps it is a date that must always be > between two other date variables. > > Properties exist because they allow you to do validations. Today (when you > define the global variable) you cannot imagine that you would ever need such > a thing, yet in six months you see six very good reasons. > > Properties can be read-only (from outside of the module) or write only > (from outside of the module). Globals are just a variable that anyone can > set at any time for any reason. > > I have lots of "globals" which can be set from inside of the module but > only read from outside (through a property). These are "global" in that > anything anywhere can read them, but not write them. > > There are lots of programmers that have thousands of variables that are > global, everything in sight. There are others that have a dozen carefully > selected. I have a dozen, carefully selected, but I do in fact have a dozen > or so. I use a lot of class and module private variables in the header area > (global to the class or module). > > The simple truth is that no variable should have scope beyond what is > needed for that variable. Scope exists for a reason. Global would not exist > if it were not occasionally necessary. Module and class "global" (private > in the header of the module and class) exist because they are needed. They > are still global but only to that module or class. If it were good practice > to make everything global then having private keywords would be unnecessary. > > John W. Colby > www.ColbyConsulting.com > >