caines.ca/blog Shell-Shocked Ramblings from the Trenches of Software Development

7Oct/090

Purple Code

Of all the flavours of code I hate maintaining, the kind that I find the most frustrating is the kind I think of as "purple code".  I've always thought of programming as a form of writing, where the intended audience is comprised of myself, other developers and of course computers.   The concept of "purple code" comes from that parallel.

Back in highschool, I had an English teacher that was known to never give marks above 89%.  I'd already scored two 89's on two essays when (despite my usual policy of under-achievement) I decided to make a bold move with the next essay and spend a few hours swapping out every other word with some obscure 5 dollar word from a thesaurus that I didn't actually fully comprehend.  That essay was returned to me with a scathing mark of 80% (compared to the other ones anyway) and a comment that said "Prose was a bit too purple".  The fact that I didn't even know what the hell he meant compounded the sting I felt after I found the definition: I'd gotten too fancy.  I'd lost my cool and he called me on it.

I took that great lesson on with me, and with writing and programming being so similar,  I realize it's still completely applicable to code.

The fact of the matter is that clever, fancy, "purple" code is not nearly as readable as it is impressive.  The frustration of having to deal with this kind of code is compounded by the fact that the maintainer knows full well that the original author is smart enough to write clearer code, but chose not to for a variety of reasons such as:

This is not an exhaustive list at all;  I could easily come up with 4 more by tomorrow.

The problem with criticizing this kind of code is that it's obviously very clever and it's obviously been written by a clever person.  The sin here is that the degree of complexity of the solution is far beyond the degree of complexity of the problem.

With the exception of optimization (and remember "premature optimization is the root of all evil"!), anything you do to make your code less intelligible by the average coder actually makes your code less Good.

These days I still find myself writing purple code, but it's less and less often.  I've found myself maintaining my own purple code (sometimes years after writing it) often enough to actually get some sort of feeling of shame from it.   How clever was I really when I can come back and swap out 40 LoC for 10 that achieves the same goal?  It's obvious that I wrote it trying to be clever, and now that I actually have to read it and try to make sense of it, it's obviously not clever at all.

I want to deal with code written by a programmer who knows enough to match the complexity of the solution to the complexity of the problem (whether he's smart or not).  I want to deal with code written by a programmer who doesn't care about how fancy his code looks.

Maybe I'm maturing.  Probably not.  ;)

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.