Obj.Nil? Vs. Obj == Nil

obj.nil? vs. obj == nil

Is it better to use obj.nil? or obj == nil

It is exactly the same. It has the exact same observable effects from the outside ( pfff ) *

and what are the benefits of both.

If you like micro optimizations all the objects will return false to the .nil? message except for the object nil itself, while the object using the == message will perform a tiny micro comparison
with the other object to determine if it is the same object.

* See comments.

From the Neo Koans = object.nil? or object == nil

obj.nil? is more readable and more efficient

require 'benchmark'

n = 500000
Benchmark.bm do |x|
x.report { n.times do ; 1.nil?; end }
x.report { n.times do ; 1 == nil; end }
end

# user system total real
# 0.050000 0.000000 0.050000 ( 0.056285)
# 0.100000 0.000000 0.100000 ( 0.094164)

difference between .nil? and == nil

No difference. According to docs, only nil object responds true to nil?.

ObjectiveC: if (obj) {...} AND if (obj != nil) {...}, which is better?

edit:
after testing a bit, I have determined that modern compilers will actually create the same machine code for both cases;

orig post:

It is (negligibly, perhaps) more efficient to use

if(obj) {

since you do not need to create the intermediary boolean value (by evaluating the comparison expression). I'm not sure which "other language" you are referring to regarding the non-zero being FALSE; the closest thing I can think of is c programs returning 0 for "success" and anything else for "error". Every modern language I have ever worked with uses 0 as FALSE and any non zero value for TRUE.

In Objective-C, nil is literally 0 (treated like a pointer). It is not just a pointer to zero, it is zero as a pointer. It is therefore reliably equivalent to FALSE (or, in our nomenclature "NO").

edit
after testing a bit, I have determined that modern compilers will actually create the same machine code for both cases; probably because nil is essentiall typedef'd to 0, so it knows the two styles of checking are both saying "if this pointer is non-zero".

Is if(obj && obj != nil) correct and necessary?

Correct? Yes. Necessary? No. Objective-C simply #defines nil to (void *)0, which is, in C terms, false. So simply writing

if (obj) {
[obj someMessage];
[anotherObj someOtherMessage];
}

is sufficient. Further, since Objective-C has message-eating nil, you can simply omit the check in some circumstances. (E.g., if the second line were not there in the if block, you could simply call [obj someMessage] indiscriminately.)

is there a difference between (!object) and (object == nil)?

No, there is no difference. Both of those examples are exactly the same. From the C spec 6.5.3.3 Unary arithmetic operators:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

Since nil is 0, that last sentence applies exactly.

Ruby: if !object.nil? or if object

There are differences. For example:

false.nil?
# => false

So:

if !false.nil?
'foo'
end
# => "foo"

if false
'foo'
end
# => nil

As @tokland suggested, in most cases using !obj.nil? construction is unnecessary.

object == nil or nil == object to check whether an object is nil?

The difference is mainly that if you mistakingly forget a = e.g like this

 (nil = myObject)

you will get an error cause you can't assign a value to nil. So it is some kind of faile-safe.

Is Checking The Pointer Of An Object With Negation Operator The Same As With Comparing It To Nil?

Yes, this is exactly the same. ! operator turns anything other than nil into zero, while nil becomes 1. Therefore, you get exactly the same results as the comparison with nil.

Similarly, using if (obj) is logically identical to if (obj != nil). This convention is inherited from C, where pointers are commonly used in conditions, and NULL-checked with ! operator.



Related Topics



Leave a reply



Submit