2008-03-20

5 Things: Globals

Lions and tigers and globals! Oh my! For practically every programmer, the later is the most terrifying, especially since we will likely never encounter the former in the wild. The reality is that we've all encountered them, most of us have used them at some point (it seemed like a good idea at the time), and we've double cursed their existance later on.

I cannot stress strongly enough that global variables are made of evil and hate! By definition globals invariably result in [side effects]. This was painfully illustrated recently when a minor external change had a major impact on a, seemingly, unrelated system. As a result some reporting was skewed, though not catostrophically, with data that was not quite what was expected. A flurry of emails and a day's worth of investigation later the two lines (one really) that caused the problem were discovered (by yours truely). The fundamental culprit?

$id = $author->prefix;

That's right. As it turns out, for legacy support reasons, $id is a global variable that comes from... somewhere else. We've got good people and it naturally seemed safe to use $id (I'd have thought so), and it should have been.

In this case the impact was serious but not disastrous, in others it could have been. Regardless it clearly illustrates why you should not use globals! If there is some compelling reason, like legacy code or held-at-gun-point, it's critical to be aware of what variables are global. To that end a simple [naming convention] could have easily prevented this: $g_id for example. In a perfect world we don't use globals, we use scoped variables and [namespaces]. They are dirty, we ostracize them and relegate them to old Basic programs where they can:

goto 666

5 Things: About Programming, Prelude

Over the years I've developed a set of best practices that I use. More than anything these are things that in my opinion "just make sense." In this five part series I'll be discussing my approach to:

  1. Conditionals (test right, test left)
  2. OO design (do it, divide it, armor it)
  3. Globals (don't, just don't)
  4. General style and naming (var/class names, tabs, blocks)
  5. Version control (it's free and worth twice that)
I won't presume to say that these will come to fruition in order, at once, or even quickly. As I write them I'll post them. If I'm especially inspired I'll write them more quickly, and then... post them. In the (likely) event that there is a construct similar to: [some topic] these are place holders for links to upcoming posts, probably part of the series (or to remind me of something in another potential series).

Being upfront, there will be a bias toward languages like PHP because that's what I've been developing in for the last few years. I don't like to get into the holy wars revolving around various languages and which is better. Take away what you will and leave your "Perl/Ruby/Python/Java is better" opinions at the door please. As always, some people may disagree with me, that's your right. Some people may disagree vehemently, also your right. Some people may disagree, call me a stupid git and get otherwise abusive. To those people see the credo, "Sod off you little wanker!". 'Nuf said.

2008-03-03

Google's anit virus/spam/bot filter goes insane!

I'm a huge Google supporter and I hope to be able to use them for searches again in the near future. Meanwhile my search for "dungeons and dragons" (yep just a big kid) was flagged as a potential "automated requests from a computer virus or spyware application".



Oh well, just a quick observation and now back to work. I sure hope I don't have to search for anything, I might have to use that "other" search engine. :-)