Tuesday, July 31, 2007

Monday, July 02, 2007

Mired in quicksand

Okay, I lied. I didn't know it at the time, but it undeniably so.

I have never particularly liked delving into someone else's code. There's always that feeling of disorientation that comes from looking at a landscape that was apparently painted by the love child of Pablo Picasso and Salvador Dali while on heavy-duty psychedelic drugs, and that's when the code is fundamentally sound. Let's face it, at a certain level, code is more a matter of art than engineering, and each of us has our own aesthetic. My artistic sensibilities are easily offended, but I can usually make allowances for taste by gritting my teeth and muttering "chacun a son gout" under my breath.

The code I was going to "fix", though, is diseased to the root. Patternitis, and a fatal case of it, I'm afraid. I tried a simple Façadectomy, but I found that the unnecessary wrappers had metastacised throughout the entire body. Why do people insist on doing this:

function proprietary_do_something($param) {
 return generic_do_something($param);
 }

Now, that kind of thing might be forgivable if one had created, say, a façade interface for a database connection and a particular database's class happened to correspond exactly to the interface. There is no trace of that kind of foresight here. For database activity, one basically has a choice between mySQL. And there is neither interface nor class to be seen — nothing but proprietary wrappers around native PHP functions. I have to be generous here and try to convince myself that the wrappers had once been necessary, but the fact that the only difference between the proprietary method names and the built-in ones is a prefix leads me to believe otherwise.

Then there are the multiple if statements in a single function that repeatedly test exactly the same conditions. To the Notes folk out there, many of these instances are the equivalent of:

@If(
 @IsError(@DbLookup("":"NoCache"; ""; "view"; key; 2);
 "";
 @DbLookup("":"NoCache"; ""; "view"; key; 2))

Yep, they're not just doing the same test over and over, but they're doing the same high-cost test over and over. Now, I ain't no PHP guru, but I'd'a thunk that doing the test once and setting a flag variable based on the result, then basing your conditionals on the flag woulda been the way to go. But what do I know? The last time I did anything in PHP, Rasmus Lerdorf was still in Toronto and PHP stood for "Personal Home Page Tools".

So the code that I once thought not too bad (if one ignored the HTML) goes from being the backbone of my project to a mere sketch of the functionality that I'll need. I'll grant that it works, but merely working is not enough. This thing does "thumbnails" by setting the width and height attributes on full-sized images (when PHP can easily do image resizing/resampling — not fast enough to create them on the fly, but they can be created and stored when the big image is uploaded and modified when the application is reconfigured). Even the database schema needs help (said the Notes guy), and so I'll need to include a conversion utility with the "installer". I kinda feel like Mike Holmes (of Holmes on Homes, whose claim to fame is fixing criminally shoddy home renovation work).

I've got a lot more work to do, but at least I can take some pride in what I'm doing. Shouldn't we all?