Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pdcawley (485)

pdcawley
  (email not shown publicly)
http://www.bofh.org.uk/
AOL IM: pdcawley (Add Buddy, Send Message)

Journal of pdcawley (485)

Wednesday June 06, 2001
08:41 AM

More Perl Proverbs

[ #258 ]
Okay, time for a few more proverbial explanations or something.
  • Use `map' instead of `foreach'

    If you've got a `foreach' loop that's going pushing the results of each iteration onto a list, or maybe appending a string another, then consider redoing things using map, or join '', map ...

  • Use `grep', not `map'.

    If you only want a list of stuff where a given condition is true, then use `grep'.

  • Use `foreach', not `grep'

    Sadly, Perl isn't quite lazy enough. If you're only concerned that at least one element of a list matches and you don't care which one it is, or you just want the first match, then don't use 'grep' (especially if your list is really big...). Do

    foreach (@list) {$first_match = $_, last if match_func($_)}

    I admit that this is an optimization, and possibly a premature one at that, but it makes it explicit to the reader that you're really only concerned about whether something matches.

  • You're never too good to type #!perl -w

    It may be ages since you wrote something that triggered a warning. You may be concerned about some minute possible slowdown. But still set the warning flag. And do it, even if your code has gone into production or is running on the website. If you start seeing warnings in your logfiles then act on them. Fix the code that threw the warning and try to work out how you didn't trip over it before.

  • Use source control, and check in often.

    "But source control is just for teams!" Um... no. Source code control is for anyone who ever realised that they'd been barking up the wrong tree. Source code control is about having the power to roll back mistakes. Having code in a CVS repository has saved my bacon on several occasions, and made my life easier on more. I'd not be without it now.

  • Optimize for Readability

    Code spends most of its lifetime being maintained. You may know exactly what that arcane lump of impenetrable syntax does right now. But will you know what it does and how it does it in a couple of months time. How long will you have to waste working out what's going on?

    And if it is an optimization for speed, does it have a measurable effect? Before you start looking to rewrite code for speed, run a profiler over it, see if the bit you're about to optimize is responsible for system slowness.

I know (I hope) that some of this is blindingly obvious. I really do. But the idea behind Perl Proverbs is not necessarily to tell you something you didn't know. It's to package up wisdom in memorable packets. Please let me know if it's succeeding.