On Oct 17, 2013, at 9:33 PM, Thomas Wetmore <
email@hidden> wrote:
Having an if-statement that uses 0/nil for false and anything else for true, is orthogonal to issues such as type checking and function prototypes.
Well … in a sense I agree with you, but really what’s going on is that “if” is an operator whose parameter is not type-checked. (Partly because C went so long without having any boolean type, and still doesn’t have a first-class boolean type since for backward compatibility logical and comparison expressions return int instead of bool.)
"0/nil for false and anything else for true” also describes the semantics for conditionals in nearly all late-binding / dynamically-typed languages, from LISP through Python/Ruby/_javascript_. So it’s a corner of C where it’s still basically typeless.
(Interestingly, Smalltalk-80, while very dynamic, is different here; conditionals are done via -ifTrue: and related messages, and only the Boolean class implements those, so a non-boolean receiver to a conditional will cause an exception.)
Today this expressiveness is often maligned as a cause of poor programming practices. Rat holes and pointless arguments lie in that direction.
Thomas, _you're_ the one who started arguing. The OP simply expressed a desire for an optional compiler warning that _he_ could use in _his own_ code to enforce his own practices. He did not ask for this to be mandatory (which wouldn’t work anyway) or opt-out, or try to convince anyone else to code the way he does.
I have no issue with the "!= 0" crowd writing code their way.
This is not an issue of coding style, it’s an issue of compiler type-checking. The OP wishes conditional expressions to be type-checked, precisely to catch errors where the coder is mistaken about the type of the _expression_. I really suspect you haven’t read the original post in this thread, as he made it pretty clear what the bug was that triggered this. He wrote something like
if (value) { … }
which he believed to be valid according to his coding style because he thought ‘value’ was a boolean (and so didn’t need a “!= 0” comparison). Unfortunately ‘value’ was an Objective-C object pointer. If the compiler were enforcing type checking on conditionals he would have gotten a warning and fixed his code. Instead it compiled without issues and then he had a runtime bug to waste time tracking down.
Honestly, if you reply again without acknowledging the existence of the above actual example, I’m going to assume you didn’t read the original post and just give up; I won’t waste time arguing with people who don’t read.
My issue comes when those in that crowd display so little knowledge of their language that they believe a warning is required to insult the intelligence of members of the terse crowd.
No, it’s _your_ statement that’s insulting — you are jumping to the conclusion that the OP (or those who agree with him) is ignorant, and also misstating his intentions by saying “required”. I agree with his desire that there should be such a warning (though I wouldn’t use it myself) and I can assure you that I have a pretty good knowledge of C, having used it since 1981.