• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: ObjC BOOL and boolean c expressions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ObjC BOOL and boolean c expressions


  • Subject: Re: ObjC BOOL and boolean c expressions
  • From: j o a r <email@hidden>
  • Date: Thu, 6 Sep 2007 13:24:21 +0200


On 6 sep 2007, at 12.47, Wincent Colaiuta wrote:

In the method there is an implicit cast (the return of type BOOL) that can yield the same bug for appropriate values of foo and bar. This would be better:

- (BOOL)test
{
    return !!(foo & bar);
}


My philosophy is: Be explicit. What you really have here is (and you point this out in your blog post):

	(0 != (foo & bar))

Why is that so bad? Crystal clear and intention revealing. Still compact.
I encourage the use of expressions that evaluate to a boolean value, and not relying on the "sort of implementation detail" of C where 0==FALSE, and everything else is TRUE.


If "-foo" is a method returning an object, don't write:

	if ([self foo])

write:

	if (nil != [self foo])

Now no one will mistake "-foo" from returning a boolean value. Don't "optimize" away a few characters while sacrificing readability and maintainability.

Another example: If "-bar" is a method returning a boolean value, I've seen people use:

	(NO == [self bar])

rather than:

	(![self bar])

I can see why they choose the longer form, and I've recently started doing it myself. A little more typing, seemingly redundant even, but much more intention revealing - And that should take precedence.

All a matter of opinion, obviously.

Cheers,

j o a r


Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: ObjC BOOL and boolean c expressions (From: Scott Ribe <email@hidden>)
 >Re: ObjC BOOL and boolean c expressions (From: glenn andreas <email@hidden>)
 >Re: ObjC BOOL and boolean c expressions (From: Wincent Colaiuta <email@hidden>)

  • Prev by Date: Re: ObjC BOOL and boolean c expressions
  • Next by Date: Re: NSArrayController (or NSObjectController) in Entity Mode
  • Previous by thread: Re: ObjC BOOL and boolean c expressions
  • Next by thread: Re: ObjC BOOL and boolean c expressions
  • Index(es):
    • Date
    • Thread