It’s that time again. I’m coding Coffeesh0p from the ground up for the third time since I started in 2006, only this time, it will look exactly the same. Sounds pointless, right? Well, programming is very much like skinning a cat — there’s more than one way to do it, and currently the programming behind Coffeesh0p resembles an epileptic retard with Huntington’s Chorea trying to skin a cat with a dull pocket knife under a strobe light. Almost.
The reason I’m recoding it this time, and also the reason behind why I recoded it last time is that I’ve gotten a lot better at programming since the previous incarnation. This time, I’ve been trying to wrap my head around taking an Object Orientated approach. That means using code to represent things as an object, such as a product, a news post, a comment, or a basket, which is a great way of looking at things. Before, I might have written some code to pull product information straight out of the database and display it, which works OK, but if the database behind the site changes for some reason, then I’d have to go through every page and update the code to work with the new database. Object orientated code makes anything like this much easier, because all the code to talk to the database is kept in one or two files, which can be modified or swapped for something else really easily. Since each object is kept in a single file, they can also be reused for any other site that requires something similar. For example, Coffeesh0p and Salvia-Trip.net both use the same object to talk to the database — I just borrowed the code from one site and uploaded it to the other.
As Coffeesh0p expands, the existing code has become much harder to maintain and add new bits to, which is also much easier using objects, as the code is far easier to read and understand. Imagine if I wanted to add a comment to something. Without objects, it might look something like this horrible mess:
$sql = "INSERT INTO `comments`(`item_id`,`name`,`email`,`body`,`rating`) ";
$sql .= "VALUES (1, 'Synchronium', 'firstname.lastname@example.org', 'Here\'s my comment!', 5);';
mysql_query( $sql, $database_link );
But with objects, you don’t even have to know any PHP to work out what the following does:
$comment = new Comment();
$comment->item_id = 1;
$comment->name = 'Synchronium';
$comment->email = 'email@example.com';
$comment->body = 'Here\'s my comment!';
$comment->rating = 5;
As you can imagine, coding like this hardly gives me a headache at all!
Learning to code was probably one of the best things I ever did. Seriously. I’d recommend it to anyone, and there are loads of books out there for beginners.
So, yeah, that’s what I’ve been up to. Nothing interesting or hilarious, but important nonetheless.
As a treat for indulging my geekiness, I bring you the Brass Eye episode on drugs. Brass Eye got a passing mention in my misses’s last post, Can I Get High Legally?, and it occurred to me that a lot of readers from outside the UK probably wouldn’t know what we were talking about. Well, for those that don’t know, it’s one of the funniest TV programs of all time, written by a personal hero of mine and comedy genius Chris Morris.
Click the red play button once, close any adverts that might pop up then click the green play button to watch it. The first 40 seconds or so are blank, so you might want to skip it forward.