Skip navigation.

Shadow's Workshop

working since 850 BC

Hall of Fame: How Not To Code

, , , ,

Lately i have been working on a project, where i had to patch the existing code, and make it work till we can find time for a rewrite. Now i have to admit that I've seen a lot as a web programmer but the pieces of code that I've seen in this project make me scream out and hit the person who wrote it. Therefor with out further introductions let's open the door of Stupidity and look at The Hall Of Fame.

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.

ProcrastinationEuphoria

Comments

zomg 14. November 2007, 09:50

haha, nice stuff you've digged up.

I kinda like how the variables are properly named in the first one, so it's not a totally lost case. :D

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

trust me, the true array is not used in a context like the one you described :wink:

my favorite is the one with all those if's in it.

Anonymous 14. November 2007, 10:18

kneekoo writes:

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

hehe, i do not know what happened to the guy who wrote it...but i'm sure as hell know what would have happened to him if he'd been closer to me. >:smile:

Anonymous 14. November 2007, 12:08

shenki writes:

it all looks like romanian to me :P

Anonymous 14. November 2007, 15:55

JustLikeThat writes:

Programmers can be good or bad :)), it can be from anywhere :) ..

Anonymous 15. November 2007, 00:32

kneekoo writes:

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

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.



I'm probably on a par with your developer friend, 'cause I can see a valid reason to dynamically generate javascript in inline code. :eek:

I could not believe this piece of code right here.



No, wait... I'm *definately* not that bad! That made me spit my drink at my monitor. That code justifies murder.

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.



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

LOL. Now, I have written PHP that outputs JS a few times. It just does weird things to your brain to constantly switch between two languages with such similar syntax! And truth be told I probably wouldn't like to sit down and debug those old scripts ;-)

(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

@hallvors

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

Alfil writes:

I found this (in C#):
throw (new Exception("ITS OK"));
But don´t worry... it´s ok!

shadowk 29. January 2009, 10:24

haha that is one of the best ever :smile:)

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

Download Opera, the fastest and most secure browser
November 2009
S M T W T F S
October 2009December 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30