ma11hew28
ma11hew28

Reputation: 126329

Objective-C: if (object) Vs. if (object != nil)

Assuming object is a kind of NSObject, the following if statements are equivalent, but which style should I use?

if (object) {
    // ...
}

or

if (object != nil) {
    // ...
}

Upvotes: 10

Views: 3597

Answers (2)

bernie2436
bernie2436

Reputation: 23901

They are equivalent in the sense that they do the same thing. But I would argue that the second statement makes the code more readable. When a person reads the line, they will understand that it means "if object is not pointing to nothing."

Remember Knuth's dictum: a programming language is a way to deliver instructions to a machine in a human-readable form...

Upvotes: 3

Dave DeLong
Dave DeLong

Reputation: 243146

As you say, they're equivalent. Thus...

which style should I use?

Whichever one you want.

Upvotes: 13

Related Questions