From: Brian Candler Date: 2002-10-15T18:39:01+09:00 Subject: Re: 0 is true - how'bout "least surprises" ;-P (Was: Re: & operation and if statment) On Tue, Oct 15, 2002 at 06:43:53AM +0900, Szabolcs Szasz wrote: > > > Don't forget that 0 is true. > > > > Duh, thank you, I had compelely forgotten that. > > Er... Not that I couldn't fix my scripts (my server monitor > just stopped alerting me of a recent downtime exactly for > this mistake...), but, Matz, is "if 0" being the same as > "if true" your final word on this? ;-) One of the things I find annoying about Perl is having to write everywhere if (defined $x) or if (exists $x{$y}) because 'if ($x)' introduces subtle special-case bugs (where $x is a string containing exactly "0", for example) - and I see many examples of code which is broken in this way. IMO those surprises, in 0.01% of cases, are far more annoying and difficult to track down than something which persistently fails simply because the language does not do what a 'C' programmer would expect. Also, bitwise tests don't occur very frequently. I'd be quite happy to write puts "yippee" if (data & 0x80) == 0 and it makes the intention very clear. Perhaps it would be helpful if ruby -w generated a warning if a Fixnum is used as a truth value; that would force you to write if myint != 0 (However, if you don't know the type of an object in advance you could also be forced to write 'unless param.nil?' instead of 'if param' to avoid this warning, which takes us back to the Perl situation) Regards, Brian.