james mckay dot net

because there are few things that are less logical than business logic

2007

03
Jun

Comment Timeout 2.0 and friends

The first alpha versions of my new WordPress comment plugins are now available for download.

Comment Timeout 2.0 closes comments on posts on your blog a certain time after they are posted. It has been rebuilt from the ground up to incorporate some new features:

  • You can now override the default settings to allow certain posts to have the discussion kept open for a shorter or longer time, or even indefinitely.
  • You can define a “popularity level” above which the discussion can be kept open for an even longer period of time if you so desire.
  • You can have comments on older posts sent to the moderation queue instead of closing the discussion altogether.
  • The comment form now indicates when the discussion for a particular post will close.

Some features were added to version 1.3 but have now been spun off into two separate plugins:

Three Strikes and You’re Out examines your Bad Behavior logs and your spam queue and closes comments across the board on your blog when you are visited from any IP addresses that have been repeatedly misbehaving (the default settings are three times in a week). It also defines a couple of hooks and adds a new logging table to the database, so other plugins can register naughty events (e.g. failed captcha tests) or override the counting mechanism (e.g. to implement whitelists or blacklists).

Link Limits rejects comments which contain BBCode or more than two normal hyperlinks. I’ve found that this blocks approximately 80% of spam, yet genuine comments exceeding these limits are almost non-existent. It informs your commenters that this restriction is in place. It also logs any violations to Three Strikes And You’re Out, but it works perfectly well if you do not have Three Strikes And You’re Out installed.

I’ve marked them all as “alpha 1″ status, which means use at your own risk, though I am dogfooding them on my own blog. If you have any problems with them, I’ve written a post on how to report problems with WordPress plugins — please read it before giving me a shout, though I do welcome feedback and suggestions of course.

Starting with these plugins, I have changed the licensing terms. Whereas the old versions were GPL, these ones are available under the MIT X11 licence. It is GPL compatible but doesn’t have the “copyleft” element. This means that if you wanted to, you could adapt it for use with another, non-GPL, CMS or blog program.

25
May

Cycle helmets versus style and comfort

I sometimes wonder why I bother wearing a cycle helmet when I take my bike in to work. As far as I’m aware they’re not a legal requirement here in the UK and personally I think that’s a Good Thing. I’ve been observing other cyclists when I go into work and back home again, and it seems that we helmet-wearers are definitely in the minority. They do make you feel safer, but I sometimes wonder if that feeling is more a palliative than anything else. They’re uncomfortable, they make your head sweat like crazy, they’re bulky and cumbersome, and they make you look a complete wuss. And if what Wikipedia says on the subject is to be believed, there is no conclusive evidence that they make the slightest bit of difference to safety in the first place.

Regardless of whether I wear a cycle helmet or not, one thing you’ll never see me wearing when I’m on my bike is lycra — that horrible figure hugging stuff that reveals the outlines of those parts of you which should really be treated with more modesty. It may be aerodynamic and all the rest of it, but personally I think it makes you look so awful that it’s embarrassing, even if you are fit and healthy. It screams that you’re one of those fitness freaks who view cycling as an end in itself rather than as a means to an end and don’t care if you end up looking a total prat in the process.

22
May

The functional beauty of JavaScript

I’m the kind of developer who tends to sit on the “coding” side rather than the “design” side of the fence. I’m at my happiest when I’m designing web services, database access and application architecture, and although I do enjoy the odd creative spurt with Photoshop and the like, I don’t find it as interesting as the former. Consequently, until a couple of years or so ago, I only gave JavaScript and client-side development the bare minimum of attention that I needed to do my job. What with all the lunatic inconsistencies that you needed to handle in order to cope with Netscape 4 and Internet Explorer 4, I always perceived it as a monstrosity that needed a lot of ugly hacks to get it to do anything more compelling than bring up a dialog box saying “Hello world”, and was content to merely download whatever scripts I could find off the Internet to make it do whatever I needed it to do.

These days, of course, the story is quite different: you have some nifty tools such as Firebug, and despite the myriad rendering bugs in Internet Explorer, standards such as CSS and XHTML make browsers are a lot more compatible with each other than they used to be, plus of course there is all the cool stuff that you can do with Ajax. All this makes learning JavaScript a much more attractive prospect, and not surprisingly I’ve had to do a lot more of it in the past couple of years than ever before.

It’s been a real eye-opener. Far from being the ugly, kludge-ridden monstrosity that I’d always thought of it as being, JavaScript is actually a beautiful, well designed language with some very nice constructs. I always thought of it as one of those linear, procedural languages much like VBScript or Fortran with some vaguely object-oriented bits and pieces thrown in as an afterthought like PHP 4, but in actual fact it has much more in common with functional languages such as Scheme, OCaml or Haskell. It has closures and first-class functions, for example, which means that you can express some things in very clever, succinct and beautiful ways.

One particular JavaScript framework that has gotten my attention in the past few months is jQuery, and it’s become my library of choice for DHTML and Ajax gee-whiz. It’s an increasingly popular and fully featured toolkit that enables you to do some pretty clever stuff with only a handful of lines of code. For a simple example of what you can do with it, here is a code snippet that will highlight rows in a table when you mouse over it:

var highlightColour = "#ffff00";
var normalColour = "#ffffff";

$(document).ready(function() {
  $("#my-table tr").hover(
    function() {
      $(this).css("background-color", highlightColour);
    },
    function() {
      $(this).css("background-color", normalColour);
    }
  );
});

I love the simplicity of this. As the WordPress guys say, code is poetry.

(Update: Jeff Attwood has an interesting take on the subject where he describes JavaScript as “the lingua franca of the web” with the likes of Flash and Silverlight as merely pretenders to the throne.)

15
May

I am not as good at Trivial Pursuit as you think

I’ve been humbled. We played Trivial Pursuit at our church life group this evening. Guys versus gals. Everyone expected my team to win simply by virtue of the fact that I was on it, but unfortunately, we lost. :(

Contrary to popular belief, it turns out that I don’t know everything. Some people seem to think of me as a bit of a walking Wikipedia, and to be sure, maybe I have accumulated a bit too much useless information in my head from browsing said Wikipedia, but there is still a heck of a lot that I know absolutely nothing about. Such as celebrities, for instance.

The fact of the matter is that the latest version of Trivial Pursuit seems very celebrity oriented. I got the impression that the majority of the questions were about characters such as Britney Spears. Even some of the geeky questions were about Britney Spears. And I know nothing whatsoever about Britney Spears, other than that she’s a pop singer or something like that. I’ve never really followed the celebrity scene in any depth — I simply don’t tend to find it all that interesting.

I think this is probably why the ladies’ team won in the end though. Celebrities and the like tend to attract more interest from women. Cambridge psychologist Simon Baron-Cohen has published a book that goes into quite some detail about this kind of thing: he says that men’s brains are hard-wired for understanding and building systems, such as cars, computers and the offside rule in football, whereas women’s brains tend to focus more on empathising: subjects such as people, soap operas and Celebrity Big Brother. Make of that whatever you like (if you don’t like it, just put it down to the fact that he’s the cousin of the man behind Ali G and Borat) — I’m just making it my excuse for not sweeping the board with useless trivia about pop stars.

26
Apr

The future of Comment Timeout

A colleague of mine mentioned to me the other day that I ought to read The Mythical Man-Month by Fred Brooks. I haven’t done so yet, but I was interested to see from the various reviews that it talks about the “second-system effect” — that the second version of a system that you design will tend to be over-engineered, since it has all the features that you thought about for the first version but didn’t implement due to time constraints. It means going from a small, elegant, successful system to something large, feature-laden and bloated. Scope creep, in other words.

I’ve been thinking about this as far as the next version of my Comment Timeout plugin is concerned. The original idea was quite simple: to close comments on your blog entries after a certain length of time. The current version has extended well beyond that remit, with a whole bunch of other features that, while useful and maybe related from a technical perspective, aren’t really related from the end user’s point of view to what the plugin is ostensibly trying to do. I wonder if this is probably a bit confusing for users and making it a bit difficult to determine whether issues are due to bugs in the code itself or people misunderstanding what it’s supposed to be doing.

I think the answer is probably to separate it out into different plugins, each of which concentrates on doing a single thing and doing it well. At the moment, I’m thinking of separating it into three different plugins, perhaps something along these lines:

  • Comment Timeout 2.0: a trimmed-down version, which concentrates solely on the job of closing comments on old posts after a certain length of time.
  • The Black Hole: the functionality to nuke comments altogether if they contain BBCode, too many hyperlinks, or certain keywords, so they don’t even make it as far as your spam queue.
  • Three Strikes and You’re Out: the bit that examines your Bad Behavior logs and spam queue and closes comments to IP addresses that are persistently causing trouble.

I’d be interested to know what people think of this idea. Would it make it easier to understand, or would it just make things a bit more confusing? Leave me a comment and let me know what you think.

24
Apr

Making website upgrades seamless: script the changes

One of my tasks this week has been to roll out some changes to a fairly busy web application. Because it is pretty heavily used, when we make changes we need to get it right and minimise downtime.

How do you do this? Script the upgrade process. Ideally, you should be able to simply copy a working build to the production server, hit a button (or run a script) to switch from the old version to the new one, and you’re done.

This particular web application’s upgrade process is very simple. You get the code files from the build that you want to deploy and copy them onto the web server — into a new directory for each version, e.g. “html-3.14″. You change a symbolic link at the web application’s root that points to the version you want to use. You’re done. Rolling back — should things go pear-shaped — is simply a case of changing the symlink back again.

To get this working effectively, I had to streamline the application itself. Some files, such as site configuration, may vary from one server to the next, so I had to separate these out. I partitioned the application into three directories: the code, the configuration files, and dynamic data, such as logs and uploaded files.

Only the code directory changes between versions: its contents are exactly the same whether they are on the developer’s machine, the staging server or the live server, and it has no special requirements in terms of access rights or contents. Files that need to vary between different servers (such as configuration files) are kept in a separate directory and are not changed during a normal upgrade process. This makes the upgrade script very simple.

This explanation is probably a little bit simplistic, although it can easily be extended to take account of extra requirements such as changes to the database schema or addition of new configuration options. Some upgrades will also have much more complex requirements, but the general principle is the same. Script the upgrade process so that it runs in a single step. It’s the same principle as the one Joel Spolsky makes when he asks in The Joel Test, “Can you make a build in one step?” If you have to edit several files and jump through several hoops, the risk increases that you will make a mistake somewhere along the line. Being able to do it all in a single step is much more robust, and mostly seamless from the end user’s perspective, because if all goes well, they will notice no downtime, but only that there are some new features and some irritating bugs have been fixed.

21
Apr

Mornington Crescent

The signs in stations on the London Underground direct you to platforms for a particular line heading in a particular direction — for example, the eastbound Circle Line, the northbound Jubilee line, and so on. The different lines are all colour coded — the Circle Line is yellow, the District Line is green, the Bakerloo Line is brown, and so on. With many platforms being shared by two or more lines, one would expect that the trains themselves would arrive indicating primarily which line they are running on, preferably with the same colour coding fairly prominent, right?

Wrong.

The trains themselves, and the notice boards on the platforms, indicate only which station is their final destination. There is no clear indication of exactly which line they are running on. When you are on a platform that is shared by two different lines, this can cause quite a bit of confusion if you are unfamiliar with the routes themselves, and, as is often the case on the Underground, you have only seconds to determine whether the train on the platform is the one you want or not before it closes its doors and heads off into the unknown.

Take what happened with the five of us who went up to MiniBar last night as an example. After some debate on the way back as to whether we should walk to Aldgate East or Liverpool Street station, we decided to head for the latter. At Liverpool Street, you head for the eastbound platform and take a Circle Line train. The Circle Line turns south then west after Liverpool Street and reaches Victoria after a dozen or so stops.

The train on the platform was the one for Barking. Okay, fine, where’s Barking? These trains stop for less than a minute, so rather than find a map and then look for Barking to see if this was the right line or not, we collectively decided to jump onto the train with only seconds to spare, and then ask questions.

We were halfway to the next station before we realised that Barking is, of course, at the end of the Hammersmith and City Line — i.e., heading in completely the wrong direction.

Not to worry, however. You get out at Aldgate East, cross over to the platform on the other side of the tracks, and catch the next District Line train heading west, arguing vigorously all the way about whose fault it was that you ended up on the wrong train in the first place.

The only problem is that you have the same problem on the other side. We had arrived on the platform and were scrutinising the map when a train came along. “Hammersmith via Kings Cross/St Pancras.”

The same individual who led us onto the wrong train in the first place now embarked on this one, and most of the rest of our party would have done so too, but for the fact that two of us had already figured out that this one was also operating on the Hammersmith and City Line, which does not go directly to Victoria, and would merely have taken us back to where we started.

Fortunately we managed to convince them to wait on the platform until we had determined which train was the right one before getting onto it, and eventually we ended up on the right train, but by this time, we were beginning to come to the conclusion that the game of Mornington Crescent bears a lot more resemblance to reality than originally intended.

06
Mar

Comment Timeout 1.3 beta 1

The latest version of Comment Timeout is now available for immediate download. This fixes several bugs:

1. The plugin no longer raises a warning when installed on a server where allow_call_time_pass_reference is turned off in your php.ini file.

2. It is now possible to disallow hyperlinks in comments entirely, by setting the maximum number of hyperlinks to zero.

3. Hyperlinks that contain line breaks are now counted correctly.

One important note: If you are upgrading from an earlier version, and had set it to allow unlimited hyperlinks in your comments, it will now reject all comments containing any hyperlinks at all. To fix this, go to the options page and uncheck the box that says “Reject all comments containing more than 0 hyperlinks” — or alternatively, you can change the zero to some larger number of your choice.

05
Mar

Vertical monitors

A couple of weeks ago I was treated to two new monitors — 20 inch wide screen flat Dell offerings, each with a resolution of 1680×1050 pixels. These replaced a couple of excruciatingly old CRT behemoths that were occupying three quarters of my desk.

The new monitors can rotate on their stands, so you can have them in either a portrait or a landscape orientation. After a little bit of experimentation, I’ve plumped for having both of them upright.

My monitor setup

This arrangement really comes into its own for coding: you can see ninety lines of code on one screenful without compromising clarity. This is quite helpful when you encounter a gargantuan 1,600-line function with loops and if statements a dozen levels deep, written by someone who has never read Martin Fowler’s excellent book on Refactoring.

The only thing is that having them vertical slows down the graphics card a bit for some reason — but since I’m not playing video games or watching DVDs at work, that doesn’t really matter. It’s also a bit odd when the machine boots up because the Windows splash screen appears on its side.

Some people like to have three or more monitors, but I’m not one of them. While two monitors are definitely much better than one, I find that a monitor arrangement that is too wide can be a bit uncomfortable when you’re constantly having to move your head through an angle of about 45° to get from one end of the screen to another. With my previous setup of two 21 inch CRT monitors, my applications all tended to congregate on the left hand screen, and when I had the new monitors in landscape orientation it felt even more awkward. However, with them both upright, it’s probably the best monitor arrangement I’ve ever had, since it provides an optimum ratio of screen real estate to required head movement to make the most of it.

04
Feb

Stopping the spam flood

Following a recent flood of comment spam last week, I’ve decided to tighten up on my blog commenting policy. I’m now limiting comments to two hyperlinks each, which must not be in BBCode format. WordPress doesn’t use BBCode anyway, and I’ve never seen a genuine comment on anybody’s blog which contains more than one hyperlink, so I don’t think this is going to be a problem.

I’m also going to close comments and trackbacks right across the board to any IP address which has three comments pending moderation in my spam queue or which gets trapped by Bad Behavior three times in a week. In practice, it’s not likely to affect you unless you are running a spam bot on your computer or network.

If you want to do something similar on your own blog, it’s handled by the the latest version (1.3) of Comment Timeout. Note that this is still in alpha, so use it at your own risk. It’s configurable as before, so you can set it to allow three — or more — hyperlinks if you prefer.