What does nasty code look like to you?

by jon on September 1, 2008

Ayende, C# guru in his own right and author of Rhino.Mocks (a C# mocking / testing framework) has released some code that he's going to use as "legacy code" for demonstration purposes.

This stuff reeks of code smell. enormous functions, multiple levels of nested conditional logic, mixing of concerns... the whole thing is a case study on how NOT to write code.

The comments are also quite interesting.

This got me thinking about what would probably have been the worst PHP code I've written. I went digging through an old shell account I have and found something from the first CMS I wrote (probably around 5+ years ago):

 
 if ($upload == "photo")
 
	{
 
	$realname = $HTTP_POST_FILES['userfile']['tmp_name'];
 
	if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))
	{
		$filename = $HTTP_POST_FILES['userfile']['tmp_name'];
 
		$realname = $HTTP_POST_FILES['userfile']['name'];
 
		@chmod("$userfile",0777);
		copy($HTTP_POST_FILES['userfile']['tmp_name'],ROOT."/gallery/$imagefolder/$path/".$realname);
 
		$size = getimagesize(ROOT."/gallery/$imagefolder/$path/$realname");
		$size2 = filesize(ROOT."/gallery/$imagefolder/$path/$realname");
 
	// thumbnail
 
	$name = ROOT."/gallery/$imagefolder/$path/$realname";	
 
	$data = GetImageSize($name);
	$im = @imagecreatefromjpeg($name); 
 
	$w = $data[0];
	$h= $data[1];
 
		if ($w > $h)
			{ 
 
			$nh = 125; // This is the MAX height of your thumbs
			$ratio = $nh/$h;
			$nw = $w*$ratio;
 
			}
		else
			{
 
			$nh = 125; // This is the MAX height of your thumbs
			$ratio = $nh/$h;
			$nw = $w*$ratio;
 
			}
 
	if ($truecolour == "TRUE")
	   {
	        $ni=imagecreatetruecolor($nw,$nh);
	   }
	else
	   {
		$ni=imagecreate($nw,$nh);
	   }
 
	ImageCopyResampled($ni,$im,0,0,0,0,$nw,$nh,$w,$h);
 
	ImageJPEG($ni,ROOT."/gallery/$imagefolder/$path/$thumbfolder/$realname",$thumbqual); 
 
	@chmod(ROOT."/gallery/$imagefolder/$path/$thumbfolder/$realname",0777);
 
	 PRINT "
<p style=\"text-align:center\">
 
	 New image has been uploaded
	 If you are not redirected to the add image page <a href=\"$PHP_SELF?path=$path\">click here</a>
 
 
        <META HTTP-EQUIV=\"Refresh\" CONTENT=\"1;URL=$PHP_SELF?path=$path\">";
 
	}
 
	else
 
	{
	 PRINT "
<div style=\"text-align:center\">
<h1>An Error Occurred</h1>
 
You must select a JPEG image from your harddrive, and it should be less than
	 your upload limit (normall 2MB)
	 If you are not redirected to the image page <a href=\"$PHP_SELF\">click here</a>
</div>
 
        <META HTTP-EQUIV=\"Refresh\" CONTENT=\"10;URL=$PHP_SELF\">";
 
	}
	}
 

What's the nastiest thing you can remember writing?

{ 2 comments… read them below or add one }

geoffrey September 4, 2008 at 1:43 am

Ugh! gross…canada, i’m astounded!

Jahangir September 6, 2008 at 7:08 am

haha.. i can soo relate to this post.

The nastiest code that I have written which is still fresh in my memory is when I had db queries, nested conditional blocks, validations, in-line js code and anything you could imagine all inside a template file of a CMS. yikes… NOT recommended at all. But hey, it got the job done ;)

Leave a Comment

Previous post:

Next post: