As developers, we have a bunch of stupendous stories about the time spent debugging legacy code. This blog is designed to highlight some of the most annoying / interesting / hair-rising / hilarious solutions of the past.
Friday, April 4, 2014
PHP SPL is a mess
The standard PHP library is a mess. There's no system in function naming neither parameter order whatsoever. Two key things, which would be crucial for learning the language without having to memorize all kind of random sh*t. Even more confusing, if you - as me, few seconds ago - face an incredible composition of two functions, that seemingly produce different behaviour. They don't. They are just named this way, thanks to the great SPL. Yes, I've met html_entity_decode(htmlentities($content)) in a code that's meant to clean some ckeditor input text. Just as horrible, as it seems. The more anti-conventional PHP is, the more newbies will make these mistakes. I'm seriously waiting the moment when camels come, dollar signs disappear, and PHP turns to an object-oriented language, while it stays dynamically typed. Sweet dreams, huh? (until then, Ruby is almost the whole package).
Reading directory contents the right way
I stumbled upon another magical line that uses the same scheme for logical evaluation. Goes for sure.
while(false !== ( $file = readdir($dir)) )
After pasting this, I'm some kind of suffering from cognitive dissonance, so let me fix it.
while( $file = readdir($dir) )
while(false !== ( $file = readdir($dir)) )
After pasting this, I'm some kind of suffering from cognitive dissonance, so let me fix it.
while( $file = readdir($dir) )
The emptiness strikes
An empty included file can mess up character encoding if it's saved not as UTF8. But why does one include a blank document?
Sunday, March 30, 2014
How to overload a PHP constructor properly?
The answer is obviously this:
if(func_num_args()==0){
//do nothing
}else{
if(func_num_args()>0 && isset(func_get_args[0])){
}
}
Saturday, March 29, 2014
Query massacre
Wow! I got myself over the shock of facing another deprecated mysql_ function, but look at this construction:
for($i=0;$i<5;$i++){
$q[$i]=mysql_query("SELECT * FROM users LIMIT $i,1");
}
Uses a for loop to execute multiple queries selecting one line each instead of LIMIT 0,5. GENIUS.
Thursday, March 27, 2014
Trust me, it's magic!
You know, something's fscking wrong with the code when you find out that the longest class is actually called Magic.
Unconditonally loving WHERE
What if we glue the query string's segments together as a future condition string in a WHERE clause of an SQL statement, but there are no conditions set? Easy. Just substr($cond,0,-5) , today's legacy code tells. But what if it's followed by a space? No problem, check it five different times like this: if( isset($cond) && ($cond!='W') && $cond!=''). Check again right before building the query, if($cond=='W') $cond =''; .
Subscribe to:
Posts (Atom)