Comment driven development

There is quite a handful of programming techniques out there; TDD, BDD, YAGNI, DRY to name a few. This post will be about something many people might already be doing but don’t know it has a name: Comment-driven development or comment programming.

CDD is helpful for:

  • prototyping,
  • spitting out your thoughts in code editor, so you don’t forget anything later (good for brainstorming sessions),
  • explaining what needs to be done if someone else is going to be writing the code itself,
  • commenting the code :). Comments could remain, so your code is documented from the get-go

I often start the new PHP file or class or even method with the layout in comments. Here’s an example:

// Get the needed models
// Collect todo items
// Get lists that the todo items belong to
// Send to View

Ok, everything is clear. After the real code sets in it looks like this:

public function someAction()
{
    // Get the needed models
    $todoTable  = new TodoTable();
    $listTable  = new ListTable();
 
    // Collect todo items
    $todos      = $todoTable->findByAuthor($author);
 
    // Get lists that the todo items belong to
    foreach ($todos as &$todo) {
        $todo['lists'] = $listTable->find($todo['list_id']);
    }
 
    // Send to View
    DIC::get('View')->todos = $todos;
}

As you can see, the comments can stay in place. Even for this simple example, it is good practice to document your code.

The Wikipedia article states:

In comment programming the comment tags are not used to describe what a certain piece of code is doing, but rather to stop some parts of the code from being executed. The aim is to have the commented code at the developer’s disposal at any time he might need it.

And later on:

However, comment programming is used instead of a real implementation. The idea is that many functions can be written like this, and then the design can be reworked and revisited without having to refactor a lot of source code.

So, Wikipedia article is somewhat contradictory to itself. The aspect of comment programming I am writing about here is the “comments instead of a real implementation” part.

Do you write your code with comments first approach? Do you use some other technique?

PHP and template engines (and some other stuff)

I’m developing web sites for quite some time now, started when i was… Young? Yellow? Well, started a long time a go in a galaxy… AARGH! Can’t get Star Wars out of my life! Anyway. In the beginning i was using the good old php mess approach:

echo 'ul';
foreach($array as $item) {
    echo '<li>Some $item here</li>';
}
echo '<ul>';

And so on. For onepagers this was enough. So i stumbled accross mambo (former Joomla) and saw the raw power of cms and, well for me, good coding. K, let’s make ourselves a content management system. But how? What do we need? The neccesary ingridients where these:

  • Apache with php (obvious)
  • MySQL database (oh, what’s this database thing for?)
  • foresight

What’s the most important? No, wrong, not coffee. Foresight! Without analyzing and planning the outcome can’t be good. I started to code. Heavily. Took a good number of different approaches, all ended bit dull and like they couldn’t take on serious tasks.

Continue reading “PHP and template engines (and some other stuff)” »

WordPress SVN upgrade

Update: no need for this plugin anymore, the new WordPress handles updates on his own :( .

Usually when the new version of WordPress is out, you have to download the latest zip (or tar.gz), unpack it, upload everything, and run upgrade. You can help yourself with uploading the zip file to the server and unpack it there, but you have to have shell account and know a bit of console commands.

There is another way of upgrading to the latest version, and that involves usage of Subversion. This gets the latest of files but you still need the console. The command is:

svn co http://svn.automattic.com/wordpress/trunk/

So, to help with the upgrading process, I’ve developed the SVN upgrade plug-in. It uses the phpSVNclient and some other classes by Cesar, who posted it on PHP Classes. I took it and made the plug-in firstly for my CMS, and it works great. Than I saw that the WordPress can be obtained via Subversion, so I decided to make svn upgrade available to all.

SVN upgrade uses database (wp_svn table) in which it stores the latest repository revision. If a new revision is out, you can do the upgrade, otherwise it will tell you that it is on the latest version.

Oh, and I have just tested it, again. 8061 is out! Bleeding edge! wp-login.php just got debugged, or something, and i have it! With just one click! Great!

Continue reading “WordPress SVN upgrade” »