On 12/16/05, Darren Duncan wrote:
The root question of the matter is, what does "undef" mean to you?

To me it means nothing.  (I'm so callous.)

The fact is, that in any normal program, using an undefined value as if it were a defined one is a bug. Normally there will be a point where such a variable should be tested for definedness and either be given a default value explicitly or fail. Checking your input at the gates is good programming practice.

Funny, I feel just the opposite: if a normal[sic] program has to define initial values for its variables, then it probably isn't designed right. Sure, sometimes you need to start with your $countdown=10, but most of the time I expect my strings to be empty until I put something in them, my booleans to be false until proven true, etc. Uninitialisation warnings are the bane of my existence, and I usually start my programs with C<use warnings;no warnings("uninitialized");> (unless I'm feeling brazen enough to forgo warnings altogether, hah! And once I tried doing the crossword in ink, so there!!).

But still, the default action should be that undef never becomes anything magically, which aids in avoiding bugs. Such as when you're using a value that you thought you tested but actually didn't.

I can't say it's never bitten me (I make enough mistakes for anything to happen), but undef problems certainly don't stick out in my memory. As opposed to SQL's null<>null, which has caught me many times in the past, and probably will catch me many more times to come.

Having users explicitly set defaults with //, or by setting a defaults method, makes the program more self describing, as you can see what it is doing in the code, and it isn't doing anything without saying so.

I was mostly serious in saying that programs should be able to assume variables start out as nothing. Having to define every last thing actually makes code harder to read and write, like trying to breathe consciously, or deliberately move every leg muscle when trying to walk. It's just too low a level.

Having an unknown/danger value, as well as the good ol' undef/none value, could be useful though. But I think you should have to deliberately use it ("my $x=unknown;") to show that something unnatural is going on.


-David

Reply via email to