mway’s blog

Memores Acti Prudentes Futuri

Things I Will Never Understand #1

no comments

There are tens of thousands of professional programmers in the world.  Maybe more – it’s hard to say.  Development of any kind, though, is a lot like playing guitar – just because you can, doesn’t mean you should.  Just like there are tons of awkwardly-handed kids that learn to play (poorly) every Dave Matthews song ever written, there are also tons of developers who do equally pointless things with code.  To make this even more analogous, plenty of really terrible musicians land record deals by somehow faking talent or beguiling the powers that be into thinking they are a sound investment (no pun intended); there are also way, way, way, way too many developers who somehow manage to land high-profile jobs but are fairly inept.  Consider the following:

function formatCity($city) {
	$cityExplode = explode('_',$city);
	$cityName = $cityExplode[0];
	for($i=1;$i<count($cityExplode);$i++) {
		$cityName = $cityName.' '.$cityExplode[$i];
	}
	$cityName = ucwords($cityName);
	return $cityName;
}

Why?  For the love of all that is good and holy, why?  This code used to live on a popular entertainment group’s (sorry, no names) site that serves several hundred thousands of views a day (which is relatively small in terms of volume).  So, several hundred thousand times a day, on one particular component (with many that do similar things), we initialize two unnecessary variables (an array and a string), loop through the array (which has usually 1 but no more than 2 members), concatenate members of that array into the string, and then call a built-in function on that string before returning it.  Bear in mind that the entire purpose of this is to format an underscored_string into a Capitalized String within CakePHP. …what?

function formatCity($city) {
	return Inflector::humanize($city);
}

function formatCity($city) {
	return ucwords(str_replace("_", " ", $city));
}

Both of the above functions are easily marginally better (in terms of practice and performance), but there is still no reason that even Inflector::humanize($string) can’t be called inline in whichever part of the application needs to convert the string.  The difference in overhead between a call to the Inflector class’ (which always exists in memory anyway) method humanize and a call to an AppController or Helper method formatCity is negligible (all of those classes will always be initialized, and Inflector::humanize uses the same exact code as the last formatCity example), but favors using Inflector::humanize for the sake of conforming to frameworking principles.  The function that calls Inflector::humanize within it creates more overhead than the other (because there are calls to two class methods when it’s used), but if that function were to accomplish anything except for humanizing the string, it would be completely acceptable.

While this example may not cause huge problems in non-massive (eg, hundreds of millions of pageviews a day, like MySpace or Facebook) sites, it is principally wrong and any good programmer should understand exactly what they are doing, what that thing implies, and the cost of implementing it.  I will never understand why developers are careless and not completely conscientious of the implications of their code.   Memory doesn’t grow on trees – use it wisely.

Written by mway

18th April 2009 at 4:34 pm

Posted in Tech

Leave a Reply

You must be logged in to post a comment.