Pages

Wednesday, April 22, 2009

This Code Goes Up to 11

Some people, when confronted with a problem, think “I know, I'll use regular expressions.”

Now they have two problems.

-- Generally attributed to Jamie Zawinski, an early Netscape engineer.

I was working with HTML strings yesterday, trying to create a teaser from a a block of HTML with predictable structure. My first thought was to use regex and parse the first paragraph and I happily set about doing the work. I mean, I can do regex, that means I'm cool, right? A bit later I realized that there was a much easier way:

$tokens = split('\<\/p\>');
$teaser = strip_tags($tokens[0)];

I've been coding for a while and I am mindful of simplicity in engineering, but this just goes to show that we often use more complicated tools than necessary, just because they are there.

So when you're coding on ten and you need that extra umph, consider turning it down to five instead of up to eleven.

No comments:

Post a Comment