Crufty code

jarich on 2006-01-23T07:38:54

I'm a infrequent reader of http://thedailywtf.com, and I know it's true, because I've seen similar code out there.

Some pieces of code, though, are so lanugage specific, that although they deserve to be show-cased on such a site, they're not suitable for that particular one.

Take today's example (from a popular CPAN module, no less):

unless (ref $self) {
    my $class = ref($self) || $self;
    # etc.
}

Classic code cruft! If ref $self wasn't true a moment ago, it's not suddenly going to become so now!

This is a very mild example of crufty code. I can easily see it happening, yet it's obviously wrong, and still in there!

Another example, from the the same module, is overly complex (identifying details obscured for the time being):

$object->Some::Module::signature([map {join $;, $_->[0], Some::Module::disqualify($_->[1]->{'type'} || '')} @values]) if @values;
return wantarray ? @values : $values[0];

I don't normally go hunting for code to complain about. I've written enough of that, and maintained other people's enough to satisfy any urges I might have in that direction. My hunting today, was to find where, in a very useful, but complex module, things were going wrong. And crufty code (not to mention very few comments) makes it just that little bit harder.

I'm not saying that the last example is bad code, but I do believe it is needlessly complex. I suspect it started out simple, and easy to understand, and then grew into that. Such is how crufty code happens.