james mckay dot net

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

A highly irresponsible metric for web hosting companies

There’s a very simple question to ask web hosting companies that provides a pretty good first approximation to whether they’re any good or not:

For how long has PHP 5 been available on all your servers?

Simple? Yes. Irresponsible? Absolutely. However, the beauty of this metric is that without the option of time travel, it’s impossible to game, but at the same time, it is pretty effective at sorting out the sheep from the goats.

The key date to look out for here is 13 July 2007: the best web hosts started rolling out PHP 5 when it was in version 5.1 at least, and quite possibly when it was in version 5.0, but cheap and nasty web hosts didn’t do anything about it until the end of life announcement for PHP 4 just over a year ago. Some of them still have servers that don’t have PHP 5 installed on them, despite the fact that PHP 4 is now officially dead, buried, pushing up the daisies, and no longer supported by a lot of popular software.

Now this is not the only matter at stake — you need to take into account questions such as uptime, speed, and technical support. It may also not sound like a relevant question at first glance — pretty much all web hosts these days offer PHP 5.2.6 to new customers — but it does give an indication of how up to date your hosting package is likely to be with future changes and innovations, as well as raising serious questions if they’re not. After all, if a company needs a massive kick in the pants from the entire open source community to give you three year old technology rather than seven year old obsolete technology, what other shortcomings does it have?

07
Aug

PHP gets closures. Rejoice!

I just noticed the other day that PHP 5.3 (now in alpha) has closures and lambdas. This is excellent news — these are language features that can make for much more concise code. The syntax is a little bit more complex than in Ruby, JavaScript or C# thanks to the quirky way that PHP variables work, but it’s nothing drastic, and it’s a one-up on both Python and VB.NET, neither of which have multi-line lambdas.

The stable release of PHP 5.3 is due in September or October.

25
Jun

Less is more

Okay, folks, here’s a little exercise for those of you who think that closures are a pointless, computer-science-y concept of little or no relevance to real-world programming. This is a very practical snippet of code that I had to implement this afternoon, in PHP.

You have to write a function that takes two parameters: a template string containing placeholders such as [[foo]] and [[bar]], and a hashtable containing the values that are to be substituted into the placeholders, and returns a string carrying out the substitution. Your exercise is to write such a function in as few lines as possible.

In JavaScript, you can take advantage of the fact that anonymous functions have access to the arguments passed to the function in which they are declared, to produce a very elegant solution:

function do_template(template, values) {
    return template.replace(/[[(.*?)]]/g,
        function(key) { return values[key.slice(2, -2)]; }
    );
}

In PHP, unfortunately, it is nowhere near as straightforward — while you can create functions on the fly using the create_function method, they don’t have access to the scope in which they were created, so I couldn’t use that particular trick here. The result? Twice as many lines of code to achieve the same result:

function do_template_substitute($part) {
    global $tmp_values;
    return $tmp_values[$part[1]];
}

function do_template($template, $values) {
    global $tmp_values;
    $tmp_values = $values;
    return preg_replace_callback('/[[(.*?)]]/',
        'do_template_substitute', $template);
}

Oh well, I guess PHP is a better language if you think that productivity can be measured in lines of code per day

13
Sep

Beginners' languages can have advanced features too

C# introduced some very useful concepts in version 2: generics, the yield statement, and anonymous methods (which are similar to closures). However, VB.NET was largely left behind: it has generics, but it still misses out on both the yield statement and anonymous methods, and it had to wait until version 2 to get the simple but convenient and frequently used syntactic sugar of C#’s using statement.

C’mon, Microsoft, these are pretty useful language constructs. Sure, a lot of developers don’t know what they are or how to use them, but once you’ve seen how powerful they can be, you wonder how you managed to get by without them. It seems that in .NET land, VB is definitely a second class citizen, perhaps more comfortable for novices to use, but with some gaps in the feature set that will irritate more advanced developers.

PHP suffers the same dumbing-down problem. It has seen massive improvements in version 5 since version 4, but there are still some fairly major gaps that are not likely to be filled in the foreseeable future. For example, apparently Rasmus Lerdorf is of the opinion that PHP is not likely ever to get closures because most PHP developers would not have a clue what to do with them.

This kind of thinking seems flawed to me. There are some language features such as these, which more advanced developers can use to write code that is much more concise, clearer and easier to understand, while the less experienced can use the language without being aware of them, albeit perhaps not as idiomatically and concisely. JavaScript is a good example of this: despite the insanities of cross-browser quirks, it is easy enough for most novice developers to achieve some results, yet it has some powerful functional features that make it potentially very expressive and idiomatic — just look at what you can achieve with jQuery for instance.

Just because a language’s core constituency is dominated by beginners doesn’t mean you should leave out useful features that advanced developers can use. Don’t forget that experienced coders often have to use VB and PHP too.