• 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: Newbie with stupid question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie with stupid question


  • Subject: Re: Newbie with stupid question
  • From: email@hidden
  • Date: Mon, 18 Mar 2002 09:48:51 -0800

[gamma setEnabled:([sender state] == NSOnState) ? YES : NO];

You might perhaps regard that more readable, but IMHO

[gamma setEnabled:[sender state] == NSOnState];

is *ways* cleaner -- the pattern of "(boolean expression)?YES:NO" looks to me as very particularly, ahem, impractical.

Yes, sorry, I started out with [sender state], then changed to ([sender state] ? YES : NO) to convert to a boolean value, then realized that for complete independence from assumptions about the values of constants, I ought to add in a use of NSOnState, but didn't fix my code in the obvious way. :->

Besides, thanks to this documentation excerpt

===
Although using the enumerated constants is preferred, value can also be an integer. If the cell has two states, zero is treated as NSOffState, and a non-zero value is treated as NSOnState. If the cell has three states, zero is treated as NSOffState; a negative value, as NSMixedState; and a positive value, as NSOnState.
===

you can use even the simplest and IMHO best readable

[gamma setEnabled:[sender state]];

(with some caveat in case sender allows mixed state, but actually I guess in majority of cases this code would do what you actually wanted, whilst yours would not ;))

Sigh. I tried to get too fancy. In my own code I do things like [gamma setEnabled:[sender state]], but I didn't want to encourage newbies to make the many assumptions contained in that line, from the value and type of NSOnState to the value and type of YES. But I overreached. :->
I think it's important, though, to emphasize the "right" way to do things to newbies (a sentence with wonderful ambiguity, due to English grammar -- the right way to do things to newbies is, of course, to tie them up first, then take a feather and... :->). While [gamma setEnabled:[sender state]] makes sense to experienced Cocoa programmers, and works, it's important for newbies to understand *why*. That's what I was trying to do. The best-laid plans...
Sorry for the confusion, folks. Y'all just listen to Ondra now...

Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Newbie with stupid question
      • From: Lance Bland <email@hidden>
References: 
 >Re: Newbie with stupid question (From: Ondra Cada <email@hidden>)

  • Prev by Date: Re: expanding the view area of a window - please help
  • Next by Date: Re: Newbie - which NSTextField is the cursor in?
  • Previous by thread: Re: Newbie with stupid question
  • Next by thread: Re: Newbie with stupid question
  • Index(es):
    • Date
    • Thread