Hall of Fame: How Not To Code
Tuesday, 13. November 2007, 12:26:04
It does not mather what this does, the fact that it has php and javascript code all bunched up into one file is enough to make me hurl.
<?php
$maxopts = array();
foreach($post['pagestats'] as $ppage=>$qpage) {?>
taguchi_webpage['<?=$ppage?>'] = base64Decode("<?=$qpage['webpage']?>");
taguchi_factors['<?=$ppage?>'] = new Array();
testtype['<?=$ppage?>'] = <?=$qpage['mainhistory']['testtype']?>;
select_factor['<?=$ppage?>']=new Array();
for(i=0;i<20;i++)
select_factor['<?=$ppage?>'][i] = 0;
<?$maxopts[$ppage]=0;
for($i=0;$i<$qpage['taguchi_test']['factors'];$i++){?>
taguchi_factors['<?=$ppage?>'][<?=$i?>]=new Array();
<?/*pt. fiecare regiune a factorului i*/?>
<?for($r=0;$r<count($qpage['taguchi_test_options'][$i]);$r++){?>
taguchi_factors['<?=$ppage?>'][<?=$i?>][<?=$r?>]=new Array();
<?$mm = 0;
for($k=0;$k<$qpage['taguchi_test']['levels'][$i];$k++){?>
<?/*factorul i regiunea r optiunea k*/
if(strlen($qpage['taguchi_test_options'][$i][$r]['option'.($k+1)])!==0)
$mm ++;
?>
taguchi_factors['<?=$ppage?>'][<?=$i?>][<?=$r?>][<?=$k?>]=base64Decode("<?=$qpage['taguchi_test_options'][$i][$r]['option'.($k+1)]?>");
<?}
if($mm>$maxopts[$ppage])
$maxopts[$ppage]=$mm;
?>
<?}?>
<?}
if($maxopts[$ppage]<=1)
$maxopts[$ppage] = 2;
}?>
I could not believe this piece of code right here.
if(i==0) cc="A"; if(i==1) cc="B"; if(i==2) cc="C"; if(i==3) cc="D"; if(i==4) cc="E"; if(i==5) cc="F"; if(i==6) cc="G"; if(i==7) cc="H"; if(i==8) cc="I"; if(i==9) cc="J"; if(i==10) cc="K"; if(i==11) cc="L"; if(i==12) cc="M"; if(i==13) cc="N"; if(i==14) cc="O";
Hm...i agree...we really need an array of true...just in case we forget it's value.
$valid_opts = array(true,true,true,true,true,true,true,true,true,true);
An error handler for php works best if i just tell it to ignore the errors i already know about.
function user_error_handler($severity, $msg, $filename, $linenum) {
if(strpos($msg,"Undefined index:")!==false)
return;
if(strpos($msg,"Undefined variable:")!==false)
return;
if(strpos($msg,"Assigning the return value of new by reference is deprecate")!==false)
return;
if(strpos($msg,"is_a():")!==false)
return;
if(strpos($msg,"not safe to rely on the system's timezone settings")!==false)
return;
debug($msg." - {$filename} - {$linenum}", "err");
}
Okey let's not use the body of the if statment, it's much easier to use the else statment instead of turning the condition into a negation.
if(preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/',$domain) || strrpos($domain,".")===false){} else {
if($lasttld)
$domain = substr($domain,strrpos($domain,'.')+1);
}
the php manual says it the best
This helps prevent poorly written scripts for eating up all available memory on a server.
ini_set('memory_limit', '100M');
That's about it for now, i do not know why i have a feeling that more gems are just waiting to be discovered. Feel free to add your own findings in the comments.










zomg # 14. November 2007, 09:50
I kinda like how the variables are properly named in the first one, so it's not a totally lost case.
And that true array is perhaps the single one which could actually make sense in certain context. Imagine having an array of user input, from a form or something, it would be pretty simple to compare the validity of the data to an another array such as that one.
shadowk # 14. November 2007, 10:14
my favorite is the one with all those if's in it.
Anonymous # 14. November 2007, 10:18
Ouch! That hurts. Sorry for being forced to debug that $h!t. Five rows after reading it I brain refused to process it anymore. The effort of finding out what it does is definitely not justified. It's by far simpler to write the code from scratch. No headaches involved and less time on the keyboard, that's for sure.
The guy who wrote that code is really lucky noone put a gun in his hands because he would've shot himself by mistake a long time ago. Or maybe that's what happened if your client needed another developer. :D
Good luck, man!
shadowk # 14. November 2007, 10:28
Anonymous # 14. November 2007, 12:08
it all looks like romanian to me :P
Anonymous # 14. November 2007, 15:55
Programmers can be good or bad :)), it can be from anywhere :) ..
Anonymous # 15. November 2007, 00:32
shenki, we (me, shadowk and others) are Romanian and we are shocked by that guy's coding style. You don't need to throw the cat on some other people's yard just because you knew someone like this guy. He could be from anywhere. Stupidity has no country/continental borders. :D
He is probably someone who managed to learn some PHP/HTML/JavaScript but he never got to learn and understand programming and its common sense dos and don'ts. That would really explain why he didn't use cc=String.fromCharCode(i+65); instead of that long list of crap... and so on. I'm not even curious where's he from. He gave me enough of a headache reading his code that I lost all my interest. :D
fritter # 15. November 2007, 10:00
I'm probably on a par with your developer friend, 'cause I can see a valid reason to dynamically generate javascript in inline code.
No, wait... I'm *definately* not that bad! That made me spit my drink at my monitor. That code justifies murder.
That's pretty bad code! However, I have done it myself sometimes in VB. VB has a NOT bitwise operator so some statements look odd if negated. EG:
If Not NoCallBacks Then
End if
It is easier to read and understand if you write:
If NoCallBacks Then
'empty body
Else
'some code
End If
But in the example you posted, there is no valid reason!
hallvors # 4. February 2008, 22:48
(Speaking of PHP/JS development problems, have you ever made PHP complain by trying to use for..in or the browser throw becaused you wrote foreach(.. as ..) in JavaScript?)
shadowk # 5. February 2008, 08:50
i for one was very happy not to have to write scripts that output complicated JS statements, and even when i had to do it, i found that a little "template" goes a long way...
Anonymous # 29. January 2009, 05:19
I found this (in C#):
throw (new Exception("ITS OK"));
But don´t worry... it´s ok!
shadowk # 29. January 2009, 10:24