|Welcome to the Monastery|
Re: Coding superstitionsby mstone (Deacon)
|on Jan 08, 2002 at 01:55 UTC||Need Help??|
That's not superstition. It's called 'defensive programming', and it's the kind of habit programmers tend to aquire after a few trips down the razor blade of life. You're adding information the compiler doesn't need, but so what? Programming languages aren't for computers anyway.. they're for humans.
If you want to talk about something we could eliminate, let's talk about whitespace. The compiler ignores it, except in very special cases like searching for the closing delimiter of a here document. Yet programmers wage holy wars about whether to indent braces like so:
or like so:
when all the compiler sees is:
And technically, those semicolons aren't necessary either (nor are the braces, now that I think about it). I always use them, though, because experience has shown that I may want to put another statement into one of those blocks, and it's easier to terminate the expression now than to go back and do it later. I always use a trailing comma in list definitions:
for exactly the same reason.
High-level code is for humans. If it wasn't, we'd still be using machine code. Redundant information, which the compiler ignores, can still convey information to humans. Conventions like 'always quote string literals' and 'use meaningful variable names' make code more useful for humans, even if they don't do anything for the compiler.
Instead of worrying about whether such things are necessary, ask yourself how much they cost. So the compiler optimizes out a few tokens while it's building the syntax tree. Big deal. It's not worth retraining a habit just to save that dozen clock cycles or so. And if you personally find the code slightly more stable or readable, the payoff is worth the cost.