by nayuki · 6y ago · 0 comments
> There is an implicit "x != 0" in: if (x), while (x), ... etc.
> An explicit "x != 0" in these contexts serves no semantic purpose.
> And "x == 0" in these contexts might be better written as "!x".
I disagree with this advice because the idea is not portable across languages.For example, if we let the variable x hold the integer value 0, then:
Java 'if (x)' is a compile-time type error.
Python 'if x:' is falsy, similar to C.
Ruby 'if x' is truthy!So either no idioms are correct, or it is correct to apply the idioms pertaining to the language of choice.
So if(!strcmp(str1,str2)) is a valid way to check for equality, but I think the negation makes it confusing; it is tempting to intuitively read it as checking if the strings are not equal. if(strcmp(str1,str2) == 0) is the better choice.
Jokes aside, polyglot source files are pretty cool, and generally things like abuse of the c++ preprocessor are really neat to see, if completely unwise to do in production.
Insisting if takes a bool like java does is perfectly acceptable. But just taking a 0 value and making it "true" makes sense only in Bash?
E.g. in Common Lisp, the only falsey value is NIL (the empty list). This is easy to understand, easy to remember, and straightforward in practice because idiomatic Lisp does a lot of list operations. It wouldn't make sense for 0 to be truthy, because that's a legit value, and NIL represents the absence of a value.
I don't know what values are falsey in Ruby, so I can't comment on it.
If I return a number or nil, I want to check `if ret`, not `if type(ret) == "number"`. The latter suggests that it might be some other type‡, the former that it might be missing entirely.
A language which provides a proper Boolean has no need for the association between zero and falseness, it is a conflation of levels which can lead to subtle bugs.
‡ yes, nil is also a type. I mean some other type of type.
timeout = options[:timeout] || :infinity
if zero is falsy, you can be royally screwed with that code, if timeout should be settable to zero (definitely a thing).Consider how you would read it: "if grass is green ..." is effectively shortened to "if grass ...".
This makes no sense.
I always use explicit conditions and consider "if(x)" and antipattern and have done since I was learning C back in the mid eighties.
if (grass_is_green) { ... }
vs if (grass_is_green != 0) { ... }
or if (grass_is_green == 1) { ... }
The one with the implit comparison to zero is more readableif (bool_name == TRUE)
There is no ambiguity, and it forces you to handle exactly equal to 1 and not equal to zero separately.
if (bool_name == TRUE)
and
if (bit_field_value > 0)
are logically different even if they can be simplified to the same thing.
The comp.lang.c FAQ touches upon this:
The better approach is to always compare against FALSE, because that avoids the ambiguity of == TRUE only catching a single truthy value out of the millions of possible truthy values, contrary to the well-known semantics of C.
If you really need to distinguish between 1 and other truthy values, then just compare against 1 explicitly. Don't muddle the concept of truthiness.
The other two leave the reader with the lingering suspicion that the variable takes on two or more values. The latter more so than the former.