1

I have seen in several places the use of if(variable != nil), I personally prefer if(variable) because we avoid a comparison operation. Some one can explain me please whats the better approach and most important, why! Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
D33pN16h7
  • 2,030
  • 16
  • 20
  • This question was asked literally 1-2 days ago. And it is still off-topic. –  Dec 13 '13 at 17:53
  • No reason to be down voting this. Its a valid question. – logixologist Dec 13 '13 at 17:53
  • 1
    @logixologist No reason to be upvoting this. It's a valid but off-topic and duplicate question. –  Dec 13 '13 at 17:54
  • some use `if(nil != variable)` instead `if(variable != nil)` – Ruslan Soldatenko Dec 13 '13 at 17:57
  • @H2CO3 Yes there is. Its countering a unwarranted downvote. Though I understand the need to give feedback but I only downvote when its absolutely necessary. Others will choose to downvote if they dont like the title or they personally know the answer they think its a dumb question. This is one question I have always wanted to ask but was afraid to get into downvote hell by doing so. – logixologist Dec 13 '13 at 17:59
  • 2
    @logixologist The downvote is not unwarranted, and anyway, upvotes are not there for countering downvotes. –  Dec 13 '13 at 18:02
  • 1
    @logixologist I didn't do any voting but keep in mind that one use of down voting is to indicate that the question shows no effort prior to asking the question - even if it is a good question. Looking for duplicates should be part of that effort. And I must agree that up voting for the sake of countering a down vote is inappropriate. Only up vote if the question truly merits an up vote. Not because you disagree with a down vote. – rmaddy Dec 13 '13 at 18:08

2 Answers2

4

The compiler will optimize the comparison out anyway, there won't be any differences in the generated code.

It's just a question of style, and possibly readability, really.

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31
  • There's no "optimization" in that either. A comparison **is** made against the null pointer in both cases (since the two forms are equivalent). –  Dec 13 '13 at 18:04
  • It's a **definition** of `if(variable)`: *In both forms, the first substatement is executed if the expression compares unequal to 0.* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf – ilya n. Dec 13 '13 at 18:11
  • @H2CO3 - There *is* an optimisation here, and its as old as the hills. See comment on accept answer to duped question. – CRD Dec 13 '13 at 19:21
  • @CRD No, that's not quite an optimization. –  Dec 13 '13 at 19:24
2

if(variable) because we avoid a comparison operation

The compiler will optimize the comparison out anyway

It's a definition of if(variable): In both forms, the first substatement is executed if the expression compares unequal to 0. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf

And nil is defined as 0 pointer.

So the correct way is to say that if (variable) is defined as if (variable!=nil) whenever variable is a pointer.

It's just a question of style,

You can make a big mistake by omitting a first symbol in != or ==, so it's a pretty standard advice to avoid using if(variable==nil) and if(variable!=nil). Although clang should warn you anyway.

Community
  • 1
  • 1
ilya n.
  • 18,398
  • 15
  • 71
  • 89