Skip to content
June 25, 2008 / Abe Pralle

On reinventing the wheel

For as long as programmers have been writing code, there’s been this idea that we should avoid “reinventing the wheel” – that we shouldn’t waste time writing useful bits of code that have already been written by other people.  And – conversely – that we should make our code general enough so that it can be reused.  And pretty much everyone will nod their head in agreement and then go right ahead and reinvent the wheel anyways, ignoring libraries that others have written.  So either we need to find some way of actually making people practice what they preach – or perhaps we should just let go of this idea.

Look at wheels in the real world for a minute.  There’s not one kind that’s used for everything.  There’s dozens or hundreds of different types for all kinds of purposes: trucking, construction, racing, aviation, winter, off-road.  Even general-purpose auto tires come in all kinds of looks and sizes just for aesthetics.  Wheels are reinvented all the damn time!  So are body moldings, window glass, seats, and so on.

To explore the analogy further, what kinds of things aren’t continually reinvented on a per-car basis?  Really simple things like nuts, bolts, and hand tools.  Really straightforward things like chassis.  And really complex things like engines.

I think this analogy holds true in computer science.  The code that we’re willing to reuse includes really simple things such as line intersection formulas.  Really straightforward things such as arraylists and textured triangle-drawing functions.  And really complex things like languages and engines.

As I see it, there’s several major caveats to using anyone else’s library code:

  1. Programmers like the experience of writing their own code as a form of training and practice.  There’s been many times where I haven’t truly understood how something works until I try to do it myself.
  2. For many needs you often feel like you can write your own version faster than you can effectively learn someone else’s API.
  3. If you didn’t program something yourself, there’s always the question in the back of your mind: is this or that bug in your code, in the library code, or is it because you’re using the API in the wrong way and don’t realize it?  When you write something yourself there’s probably gonna be more bugs – but at least you’re confident that they’re your bugs.
  4. If an API in general-purpose enough to adapt to your project, then almost by definition it’s got a lot of extra features (code bloat) that you don’t care about.  Writing your own code is a way to keep things slim and focused.
  5. There’s always some new feature that needs adding.  What’s the total cost of learning an API, figuring out how to modify it, and then adding new features – versus doing something yourself?  There’s the famous example of Valve licensing the Quake II engine so they didn’t have to write one themselves.  Then by the time they were done with Half-Life they rewrote and modified the engine to such an extent that fully half of it (if I recall) was original.

Here’s my experience:  I rarely use anyone else’s library code for mid-level framework.  When I do, I usually run into one of the gotchas listed above.  However, I’ve also spent many hours of my life fleshing out API functions for my own mid-level frameworks that nobody uses.  Not me, not anyone.  That’s not to say I don’t write stuff that others do find useful – I do, and it’s always a great feeling.  But let’s note that line of demarcation.

Bottom line: as programmers, let’s stop wasting time programming stuff no one’s going to care about or use.  Use good coding practices, sure – but just Git’R’Done for your current project and don’t worry too much about making some flexible, extensible, reusable system.  Code reuse shouldn’t be a potential – it should happen when you find yourself actually using your same code over and over again.  Spend your refactoring time on that actuality, not just the possibility!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: