The Problem with Code Samples

Up until about thirty seconds before I hit publish on this post, there was a section on this site called “Code Samples.”  I removed it (after pulling one piece of code over to this blog) because I realized it wasn’t providing what it should.

Said section was made up of four links to zip files, each containing the code for a different project.  Aside from a text description of the project, there was no breakdown of why the code was written a certain way or what was learned from writing it.

Since I started this blog, I realized that what I’d hoped to accomplish with the “Code Samples” section was being achieved with posts on things like my work with the Twitter and Trello APIs.  These posts explained not just the how of what I did but the why.

I’m going to keep posting new stuff under the “Code Sample” category in the blog. Hopefully that way I can make my samples less a display of specific code and more a discussion of how I code.

Retro Code Sample: Opening Night Countdown Image

Sometime soon I’ll write a bit on my new-ish philosophy about code samples.  For now it’s enough to say that I’m cleaning out that section of my site but there’s one project I wanted to keep documented, so I’m writing it up here and now even though the code is from 2005.

The “Opening Night Countdown Image” was my first foray into using the PHP image functions and the GD2 library.  Based heavily on the code from the Church Sign Generator, it took the hard-coded date of the Detroit Red Wings’ first game back after the 2004-05 NHL lockout and generated an image displaying the number of days remaining until that date.

There were four parts to the project: Two resource image files in .gif format, the actual PHP that mashed those together properly, and a .htaccess file that allowed for a pretty URL for the image.

The .htaccess file was simple enough.  Inside a folder called /nhl_countdown/ it looked as follows:

Turn rewrites on, allow Symlinks (completely unnecessary), set the rewrite base, block the .htaccess file, and actually rewrite requests for countdown.gif (the pretty filename) to the actual PHP script that generated the image.

The resource images were also pretty simple.

background

numbers

The first was just a static background featuring the team logos and an empty space where the countdown numbers should be. The second was the set of numbers to use in the countdown itself.  Both were .gif for some reason, probably because of the lack of support for .png in 2005.

The code is where it gets fun so let’s get into that.

We first determine the number of days remaining until gameday. This is horribly inefficient but it’s easy to say that with eight more years of development experience. This isn’t a code review, it’s a sample, so I’m not going to correct myself.

Next we map out what parts of the number resource file relate to each actual number. This is another thing I’d probably do differently now.

Then we do a little setup, getting the image resources loaded in and getting ready to do our typesetting.

We loop through each digit in the number of days remaining twice. First to find the size of the image representing the character so we can properly center the completed text, then to actually add said text to the new image.

Then we actually output the new image and do some cleanup.

As I said, it’s old, horribly inefficient code. As I’m cleaning up my code samples, though, this is something I didn’t want to lose (perhaps as a reminder of how far I’ve come) so I thought it made sense to write up a post on it.