Looking Back: Learning From Old Code

« »

In 2007 I wrote a blogging program from scratch. I was really proud of it, too. It was all my own invention, with a little help that I got from a Facebook developer I knew, and I worked really hard on it. Spent the whole summer writing it so I’d be able to launch it in time to blog from Washington, when I moved here. When I started looking for coding jobs, I gave them the website address as an example of my work.

Turns out that it’s great they didn’t ask for a code sample.

Here’s the actual code from the page that displays the entries…

<?php
session_start();
$path = getcwd();
$path = $path.'/includes/';

$page = 'entry';

include_once($path.'header.inc.php');
include_once($path.'dblogin.inc.php');
include_once($path.'functions.inc.php');
echo '<script type="text/javascript" src="ajax.js"></script>';
echo '<script type="text/javascript" src="motionpack.js"></script>';

if(isset($_SESSION['admin']) AND $_SESSION['admin'] > 0)
	{
	echo '<script type="text/javascript" src="admin_ajax.js"></script>';
	}

if(!isset($_GET['entryid']) OR $_GET['entryid'] == false) { echo $err_1; include_once($path.'footer.inc.php'); die; }


if(isset($_SESSION['loggedin'])) { $permissions = $_SESSION['permissions']; } ELSE { $permissions = '1'; }
$entryid = (int)$_GET['entryid'];

$query = mysql_query("SELECT * FROM bb_entry WHERE id = '$entryid' AND permissions <= '$permissions'");

if(mysql_numrows($query) == 0) { 


		echo '<p align="center"><font color="#FF0000"><b>Error!</b> You must log in before reading this entry.</font></p>';

		echo '<form method=post action="login.php">';
		echo '<p align="center"><b>Username:</b> <input type=text name=username size=14 maxlength=16></p>';
		echo '<p align="center"><b>Password:</b> <input type=password name=password size=14 maxlength=16></p>';
		echo '<p align=center><input type=submit name=submit value=Submit></p>';
		echo '<p align="center" class="menu"><a href="forgot_password.php">Forgot Your Password?</a></p></form>';


include_once($path.'footer.inc.php'); die; }

if(empty($_SESSION['admin']) OR $_SESSION['admin'] != 1) {
mysql_query("UPDATE bb_count SET count = count + 1 WHERE id = '$entryid'"); }

if(isset($_SESSION['username']) AND $_SESSION['admin'] != 1) { $user = $_SESSION['username'];
if(mysql_numrows(mysql_query("SELECT entryid FROM bb_count_users WHERE user = '$user' AND entryid = '$entryid'")) == 0 AND isset($user))
	{
	mysql_query("INSERT INTO bb_count_users (user,entryid) VALUES ('$user','$entryid')");
	}}
	
if(isset($_SESSION['admin']) AND $_SESSION['admin'] == 1)
	{
	
	$raw_count = mysql_query("SELECT count FROM bb_count WHERE id = '$entryid'");
	$raw = mysql_result($raw_count,0,0);
	
	$string = '<p>[ <b>Raw Count:</b> '.$raw.' | <a href="seevisits.php?entryid='.$entryid.'">See All Data</a>] [ <a href="makeentry.php?entryid='.$entryid.'">Edit</a> | <a href="deleteentry.php?entryid=172">Delete</a> ]<p>';
	
	}
	ELSE
	{
	$string = false;
	}
?>

(This isn’t the entire file, as it would fill an entire 201 lines, but just a taste.)

I couldn’t believe this code. It was amazing to me that I wrote this! There are no functions, it’s completely procedural, and it’s a disaster.

But you know what? I learned something.

It helped me to realize how far I’ve come. It helped me to realize the improvements I’ve made. This was written in a time when I had no concept of objects, array or string functions, or even virtual hosts. That was a long time ago.

Take some time today and look at some of your old code. What can you learn from it? What does it tell you about the progress you’ve made and where you’re going? Relish in the old code, and the creativity it showed, and then remember how you felt when you wrote it. I know I’ll be doing the same thing.

Happy Friday!

Brandon Savage is the author of Mastering Object Oriented PHP and Practical Design Patterns in PHP

Posted on 3/27/2009 at 12:30 am
Categories: Friday Inspirations, Personal Management, Web Architecture
Tags: , , ,

Pierre Minnieur wrote at 3/27/2009 3:07 am:

Hey Brandon, you should tag somebody in this post who you know, which should post some old code of their own, too. And they should tag somebody they know, which posts some old code … :)

Andrei (@andreisavu) wrote at 3/27/2009 6:12 am:

It’s sad how many “programmers” still write the code this way.

This code is what I describe as a prototype(proof of concept). Personally I prefer to use for this Python with webpy or RoR.

Writing production ready code is not easy and personally I believe tests are mandatory.

Branko Ajzele (@ajzele) wrote at 3/27/2009 7:49 am:

Interesting article. I feel with you. Had the same flashback the other day :)

Cheers…

Danilo Domínguez (@danilo04) wrote at 3/30/2009 10:32 am:

I feel the same when I see my old code, and you think: hey, I’ve improve!.

« »

Copyright © 2022 by Brandon Savage. All rights reserved.