0

Basic question: I am wondering if there are any industry practices or style guides that mention which of these two styles is better:

if x == 0 //or nullptr

or

if !x

(I am thinking of programming languages where for the same variable x both styles are syntactically allowed and semantically equivalent).

I've always preferred x==0 because it reduces the amount of thinking. You need to reason through about what x can be, and what the negations of those would become. The behavior also isn't consistent across all programming languages. The only benefit of !x is just shorter amount of code. That said for longer and more complicated conditionals, negation would probably be better because the code would become just too big and ugly with all the equals.

8
  • 2
    At the very least, you need to mention the language you're working in. In a lot of strongly typed languages, !x is invalid if x is an integer. Jun 20 at 21:35
  • @PhilipKendall That's correct, so it's a reason I mentioned why this is not preferred imo (in languages where this works) Jun 20 at 21:44
  • Related: c - Idiomatic way to check for nonzero
    – John Wu
    Jun 21 at 3:49
  • @DocBrown I'm implicitly talking about languages that allow this. Otherwise there's no choice Jun 21 at 5:55
  • It seems you have issues to find the right words in your question. I took the freedom to fix this for you. Please double check if I got your intentions right.
    – Doc Brown
    Jun 21 at 7:56

2 Answers 2

2

Express the intent

The driving principe should be to express intentions, i.e. what you really mean to achieve. This makes the code self-explanatory.

Applying this principe to integer comparisons with zero leads without a doubt to x==0 or x!=0. This is simpler to understand than converting mentally into a boolean.

Is there a guidance in some coding style?

In very strongly typed languages, integers are not implicitly convertible to bool. No need for a coding style, the compiler will convince you, as this Swift example will show:

let x : Int = 12
if !x {           // Error: Type Int cannot be used as a boolean. test for ==0 instead. 
    print ("x is false")
}

For more flexible typed languages such as C and C++, there is as far as I know no popular guidance for -- or against -- this style in C and C++. Not even in security standards such as MISRA.

The reason is backward compatibility with millions of lines of code that used this shortcut. Many of these lines were written before booleans made it to a real built-in types. And when integer variables represent booleans it makes sense, but in other cases there is no advantage for using the more compact version. Writing ==0 will produce the same code, and will be more readable if it's about integers values.

(Important remark: Nowadays, this idiom continues to be popular also for non numeric types. while(cin) or if (!cin) areused as popular idom for checking that a stream is in a valid or a faulty status. Abandoning this idiom might create more confusion than it resolves. It might even lead to faulty code if you want to replace while(cin) with while(!cin.eof()) due to the stream semantics.)

0

After having worked for years with tons of C and C++ code where if(x) or if(!x) was used to test if a pointer or integer x was valid or not, I got used to this idiom and don't see a huge difference to if(x!=0) of if(x==0) any more. So my recommendation here is to not overthink this - I would only avoid to switch too often between those two styles. And when working with teammates who have a specific preference, stay willing to compromise.

Not the answer you're looking for? Browse other questions tagged or ask your own question.