It's Good to Know How to Make a Wheel
Published: 02/16/2009
Programming, Rant, Code
I’ve been reading a lot of blogs about reinventing the wheel lately so, in a bit of “Me too! Me too!”, and because it’s an issue I feel passionate about, I have to weigh in.
The idea of not recreating something that’s already been done is especially poignant in programming because our job requires invention. We get a task that involves the development of some piece of code that, hopefully, fixes a problem or meets some need. And there is almost always a deadline. Usually, one that is far from reasonable much less realistic.
With circumstances like that it makes sense you’d want to save as much time as possible by taking advantage of existing solutions. And boy, is there ever a bunch of solutions out there; almost always represented by 3rd party code. I have yet to run into a situation where I wasn’t able to find some sort of help from another library or full program.
Take for example a simple request that came across my desk a few months ago. One of the producers at StreetWise came in with a blog site project. After getting the details it became apparent that taking advantage of an existing framework like Wordpress made sense; the client didn’t have any expectations of adding additional functionality outside of a basic CRUD app with a permission system so we were good to focus on other things. Unfortunately, this is the exception not the rule. Also, there was the plugin system of WordPress so when the client, eventually, realized the need to update and or upgrade it’d be easier.
Problem
The problem comes in when the developer takes the idea of never reinventing the wheel literally. When this happens growth stops and progress in the craft of programming stagnates. I’ve seen too many developers rely on external libraries and they can’t do anything without them (much less explain them). That process makes for a one dimensional developer who creates crappy products that are painful to maintain and upgrade.
It’s especially troubling in the php community because of the sheer number of OSS projects out there utilizing php and MySQL. Need a forum? Use phpBB or Invision Board. Oh, a Blog? Take a look at WordPress, Expression Engine or Serendipity. Survey system? Check out phpQuestionaire. And those are just the popular ones; there are tens of thousands of programs out there for free use.
What you end up with is an amalgam of disparate parts that is the picture of a maintenance nightmare. Seriously, close your eyes and think about the worst possible maintenance scenario; I bet you there’s at least a few external libraries being used. Anyone who has ever had to maintain one of these monstrosities will tell you how devastating this approach can be.
My first exposure to maintaining a program using the above approach was like falling in a frozen lake. The code in question was 80% custom with a phpBB forum attached that was heavily modified. phpBB was locked at version 0.9(!!) and hadn’t been updated, ever. This created a mess of the database schema and codebase that required a few different philosophy changes and switches. Just a fucking headache to work on. There was code and data duplication everywhere.
A lot of beginners go this route; they’re mystified by the possibility of being able to create large programs with little effort. Unfortunately beginners rarely, if ever, take maintainability into account.
It should be said that taking advantage of existing code also means you shouldn’t touch it. Ever. Woe betide those developers who modify 3rd party code. Seriously, don’t modify existing code if you want to painlessly upgrade an application.
Solution
So, if you shouldn’t modify 3rd party code, and if using it can increase maintenance woes, why is there even code out there to use? Because of examples like in the beginning of the post; if your project’s needs are within the parameters of the code you’re looking to use it’s a good idea to use it. Just don’t go mixing and matching different code bases.
The only reason I knew the client wouldn’t need additional functionality in the example above outside of a basic blog was because I’ve written blog scripts before. I know what makes up a blog at the basic to advanced levels. This makes interpreting project criteria and minutia a lot easier. If you’ve already done something you’re more capable to make those calls.
I may have implied earlier that only beginners use 3rd party code. That isn’t accurate; I use 3rd party code all the time. I’m just more selective about what I use and when and where I do it. Unless I plan on using the full application as a framework or if I’m not confident enough in the code to handle upgrade scenarios I just don’t use full applications in my projects. You can be sure that if I use a program like phpBB, WordPress or phpQuestionaire (among many, many, others) it’s because I know them inside and out. Issues won’t… (clear throat)... be an issue :/
Yet, if I’m not using a full 3rd party project I’m still using 3rd party code. Taking a look at the codebase for my latest project I see the following OSS code:
- Smarty
- HTML_QuickForm
- HTML_Table
- Geshi
- Snoopy
- phpMailer
- easyRSS
- phpexifrw
- etc, etc, etc…
The above are php classes that handle some of the bells and whistles for the project. All have excellent reputations and I am intimately familiar with all of them so any issues can be mediated efficiently. Extend each of the above classes in a wrapper and take care with what you do and making upgrades when they come out is pretty painless too.
It’s also important to note that none of the above classes are for the core functionality, just ancillary features. I agree with Jeff Atwood who, in his post Programming Is Hard, Let’s Go Shopping!, talks about why he went through all the trouble of writing his own text sanitizer:
But here’s the thing: deeply understanding HTML sanitization is a critical part of my business. Users entering markdown isn’t just some little tickbox in a feature matrix for me, it is quite literally the entire foundation that our website is built on.
That’s the best explanation of why not to use 3rd party code I’ve heard yet. Outside of that though, just use it in moderation and don’t be afraid to take control of the project.