Back to Top

Sunday, June 15, 2008

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, ...

0 comments:

Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.