Monday, July 25, 2005

How a CONST prevents murder...

Bad:

$sql = 'SELECT BLAH FROM BLAH WHERE BLAHTYPE = 1';
$db->query($sql);


Good:

$sql = 'SELECT BLAH FROM BLAH WHERE BLAHTYPE = ?';
$args = array( CLS_Blah::BLAHTYPE_BADCODER );

$result = $db->query($sql, $args);


So, today, I was refactoring. I notice that in the last two days, the Job class has shot up from about 55kb of code and interfaces [ICK!] to 166kb. Scratching my trusty chin, I looked inside.

It appears that three kitchen sinks had been grafted onto the class, no one had RTFM / RTFJavadox, half the code that was solid was reimplemented in the presentation logic [query the database to build up a string of people's names, then do it again to get a list of their phone numbers anyone?], and everywhere... and I mean *everywhere*... the above CONST problem with hardcoded crap in queries, if statements, while statements, etc existed.


If only they had have used a CONST, they'll cry. Office rampage ends in tradegy because coworker coders are lazy...

I can see the headlines now, and quite frankly I think I like it.

Some explaining for the people who are now afraid I'll murder them for uncertain reasons:

Take above Good/Bad code. Put both throughout a lot of different places. Change all of the BLAHTYPE expected values from (int)1 to (int)2.

What do you do with the first one?
Cry. You certainly can't / don't want to write a regexp find and replace to change all (int)1 to (int)2 in your entire codebase.
Hours upon hours of poring over random bits of code, and you'll miss all of the most important things probably.

What do you do with the second? Change the value of CLS_Blah::BLAHTYPE_BADCODER from (int)1 to (int)2. 3 seconds.

I'll going to crawl back through the remaining piles of kludge I've found, but world, you are on notice, do you hear?
Post a Comment