Programming and Middleground

Only the Sith deal in absolutes

Obi-Wan Kenobi

We’re a very vocal bunch, programmers. We have very precise opinions and will often fight for them until death ensues. OK, we stop before anything bad happens, but if you have been on any discussion forum, be it reddit or a more traditional forum, you will have noticed that often, people have very specific ideas on what is good or bad. Just Google composition over inheritance and you’ll see what I mean.

I will admit, I’m not sure of where I’m going with this article, but I want to talk about it because too often I will see absolute statements like:

  • You should never use inheritance
  • Never use singletons
  • Globals are evil, you should never use them
  • etc.

It worries me, like any absolute statement in fact, that we can be so convinced something should be completely ignored.

Nothing is Only Bad

At one point or another, anything that has been implemented in a language was thought to be a solution to a problem. No one decided to add globals to Java just because it would be fun. That being the case, globals do solve a problem. That fact is undeniable. Woah, woah, slow down! I can already hear you typing on your keyboard! Yes, I agree that for 98% of problems (totally not a made up percentage) globals are not the solution.

But they sometimes have their use and right now, as I type this, I honestly can’t think of a good example of when they are the perfect solution. So after a bit of googling (it’s a verb, ok?), I was reminded of PHP’s superglobals. The $_SERVER global to me is an example of a useful global variable. Sure, it could be an object that you get from whatever instance of whatever, but would it really do anything more than now? Not really. Plus, the information contained within it is configuration information, the server name, URI, etc. It’s useful to have access to anywhere without needing to pass an object down 21 objects just because the child of a child objects needs to know what the URI is.

Ignoring my very poor example on global variables, every programming paradigm has its use and closing ourselves to those solutions is, in my opinion, a terrible mistake.

Ignoring Entire Concepts Threatens Open-mindedness

That’s a pretty bold statement, but it does make for a nice header! More seriously though, one of the most important qualities for a developer to have, in my opinion, is a willingness to learn about new concepts, languages and ideas.

Things move so fast in technology, what I learned in college less than 10 years ago is already almost irrelevant. The basics are still valid, but you’d be hard pressed to find a Lingo developer anymore and if I still did PHP the way I was taught, I’m pretty sure I would be out of work. I have had to constantly read, learn and better my knowledge on almost everything I have ever learned; every developer has to do this to “stay in the game.”

I am then forced to ask myself about anyone who will openly and loudly proclaim that something should never ever be used. What does that say about their willingness to evaluate a solution and fairly consider it as the right solution? What happens if that person is against object-oriented programming and that happens to be the solution that would be the most cost effective for the problem at hand?

What if doing a prototype, that same developer refuses to use a global variable or two and instead implements a whole architecture to play sounds in a game when the sound manager could simply have been a global object?

Hammer for Nails, Screwdriver for Screws

In the end, it's just a matter of using the right tools.

In the end, it’s just a matter of using the right tools.

Now, I used globals a lot in this article and it’s for a good reason: they are very often the wrong answer. Any first year in a computer science major can tell you. I like using them as an example because there’s so few reasons to use them, they’re a good example of what I believe to be a better tenet when it comes to development: don’t abuse your tools.

Interfaces are awesome, don’t abuse them. Composition is awesome, don’t abuse it. MVC is awesome, don’t abuse it. Globals are… rarely useful, but don’t ignore them!

Functional programming has its use. Object-oriented programming has its flaws. It all comes down to knowing all the tools that are available to us, as developers, and to choose the right ones for the right job.

That’s also the reason why I had forgotten about superglobals earlier while writing this. I use them, but only when they really are the answer to a problem and this is rare enough that you’d be hard pressed to find more than a couple of instances where I use them directly in my code.

It IS vital to question ourselves and our methods, but banning a whole tool set because it can be bad in a lot of scenarios is also a dangerous thing to do.