“David Garamond” lists@zara.6.isreserved.com schrieb im Newsbeitrag
news:40BC6E5F.1010502@zara.6.isreserved.com…
Robert Klemme wrote:
It’s superfluous to convert a boolean to a boolen in all programming
languages, although special bugs can arise from these conversions only
in C
and other languages that don’t have real booleans:
Agreed, it’s generally wrong in most languages to use equality test for
booleans. I think this is because truth values transcend types (i.e. we
can ask for truth value for many types of values).
No. The reason in C is, that there is a single value for ‘false’ but a
lot for ‘true’ and not that a lot of types can be used in a boolean
context (because they can be converted to bool aka int).
The same idiom is safe in Java, because it has boolean - but still
superfluous. In Java you should only use it in the rare case where you
want to compare two boolean expressions:
if ( getBoolX() == getBoolY() ) {…
is simpler and more intuitive than
if ( ( getBoolX() && getBoolY() ) || ( !getBoolX() && !getBoolY() ) ) {
…
Apart from that, there is a minor reason: the performance overhead for
some extra operations. But the main reason remains that it’s superfluous
and extremely error prone. Whenever you see something like this
(regardless of programming language), your alarm bells should ring:
if x > 10
return true
else
return false
return x < 22 ? false : true
return x == true ? false : true
(Safe replacements left as an exercise for the reader. :-))
Thankfully in Ruby
things are simplified: nil and false values are always false, everything
else is true.
From a certain point of view you could say that Ruby is more complicated
than C since it has two values meaning ‘false’ where C has only one. The
fact that all objects can occur in a boolean context just saves you some
explicit conversions you’d have to do in other languages.
Regards
robert