October 24, 2006
A Global No-No
I must hang my head in shame. I'm guilty of the very sin that Jack Ganssle denounces in his piece "A Pox on Globals." A certain piece of code I have written (which shall remain nameless) is peppered with the sort of oft-accessed and dangerously exposed variables that Jack decries. Dozens of them. All over the place. Not surprisingly, I am the only one willing or perhaps even able to to modify the code. Often, I'm the only one willing to even use the code.
And were I to ever leave, someone would inevitably get stuck doing the task that this code is meant to automate. Poor SOB. I can almost guarantee that, rather than try to maintain this monster, they would do the job by hand until they could devise their own automation.
Depending on global variables is like leaving a loose threads hanging off a sweater: You try to pull on any one of them, and the sweater quickly turns back into a pile of yarn. These are basic principles of encapsulation—we're talking computer science 101, folks. So why is it so easy to keep making these mistakes? For me, it's about the nearly always mistaken perception that my code is a temporary stop gap, and that spending twice as long designing it as writing it is a waste of time. Let me tell you, it is NEVER a waste of time. Take all your assumptions about how long you think your code will be in use, and multipy them by at least a factor of 10. Then imagine all the modifications and maintenance that you'll do in that time frame. The design time isn't looking like such a burden now, is it?
Posted by Kevin Carlson at 01:04 PM Permalink
|