Why?

I see code…

my @showart;
my @articles;
map {push(@showart, $_) if($_->{'article_description_cat'} eq $category);} @{$alteration};
@articles = sort {$a->{'article_description_rank'} <=> $b->{'article_description_rank'}} @showart;

So what do we have there?

  • A superfluous temporary variable: @showart.
  • A use of map that explicitly throws away map’s return value and goes for the side-effects only.
  • Lines that are way too long.
  • Don’t get me started about the semicolon in the map expression.
  • Don’t make me think about the whitespace that is missing in all sorts of places.
  • An instance of intentional obfuscation?

5 Comments

Is this what she meant?

my @articles =
    sort {$a->{article_description_rank} <=> $b->{article_description_rank}}
    grep {$_->{article_description_cat} eq $category}
    @$alteration;

I find that this sort of code is rarely the intention of a the person who wrote it. Many coders experiment with quite a bit of the problem, combining various bits and pieces as they go along, and using intermediate variables as they figure out the steps.

Now, it's not good code and there are some structural problems with it, but picking out things like "line too long" and "semicolon in map" is just being grumpy. In the big picture, everyone writes bad code at some point. Some people clean up their bad code once they've figured out what they were really doing. Letting bad code bother you is something to get over. It's just code. If you let it bother you you will be forever bothered.

Some people will love you if you point them out how to improve certain constructs in their code some of them will hate you or just plain disregard you. This happens to me both as a contractor and even when I am teaching Perl.


I wish I could figure out up-front who is in the first group and only try to help those...

The code doesn't look that bad. Sure it could have been tidied up a lot as Gnustavo pointed out, but it was easily understandable. If that is the worst you have to work with, I envy you confuseACat.

Leave a comment

About confuseAcat

user-pic Random observations that may in some way be related to Perl.