Defense in depth for programming


Two things you should always do when developing in Perl is to use strict and use warnings (with the caveat that warnings should be disabled in production systems or redirected to a log file).

However recently I was reminded that nothing is 100% (and this isn’t a compiled-vs-interpreted issue, because there are many errors compilers don’t catch). The code in question was:

my $bar = "some $expression";
foo($param1, $param2, , $param4);

What happened here was that I wanted to refactor the code and get the expression out of the function call (because it was becoming long and unwieldy), however after moving out the expression I forgot to put back the variable. Perl has a nice feature whereby undefined variables are eliminated from lists, so basically I got a three member list instead of a four-member one. And the error message wasn’t especially helpful either (it kept complaining about undefined values in the called function, and when I visually inspected the call, all that I saw was that $param4 is defined).

Takeaway lessons:

Just because strict doesn’t complain, it doesn’t mean that your code is correct (just as if a compiler doesn’t complain…)

Have other methods to ensure correctness: unit tests, integration tests, …


Leave a Reply

Your email address will not be published. Required fields are marked *