In
this thread I compared
$hash{'key'} with
$hash{key}, suggesting that the former was slightly superstitious. I still hold to the view that it's not something I should feel obsessively obliged to do. (And I don't! Free at last!) But I was interested to read nodes by other monks who felt it was a sensible precautionary step. In particular, I liked
mstone's description of it as "
defensive programming".
Just now I wrote the following code:
for (@$Courses) {
if ($Course = CheckCourse($_)) {
print PageCourseInfo($Course);
$count ++;
}
else {
last;
}
last if $count >= 10;
}
Now, note the laste line in the loop. Why not just plain old
last if $count == 10;? Is it ever possible for
$count to be greater than ten without first being equal to ten? No. I can't think of any possible change I might make in the future to this code that wd mean that it would fail to exit with the simpler test.
But that doesn't mean there could never be. It's costless to replace
== with
>=, so I do it.
Now, why do I think of *this* as being defensive programming, whereas quoting hash keys feels like a superstition? Well, I don't really know. But one thought I have is that the hash key quoting defends against changes made to that line only. If I ever do alter the hash key to add some whitespace, or make it a variable or whatever, then I'll be right there. Nothing I do anywhere else can make any difference. On the other hand in my loop, there's a whole load of different things I could do, either by altering code at other places in the loop, or by cutting and pasting bits into other code, that could make my
last test not work.
Perhaps a way of thinking about defensive programming is that it is about making every line or expression extensible. Not just loosely coupled functions, but loosely coupled conditions, evaluations and assignments, that can work in the widest possible range of circumstances.
I should be most interested to know other monks' practices when it comes to defensiveness. If it's costless and it makes the world a safer place, then why not?
§
George Sherston
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.