• 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: Good idea/bad idea?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Good idea/bad idea?


  • Subject: Re: Good idea/bad idea?
  • From: Andy Lee <email@hidden>
  • Date: Thu, 24 Apr 2014 19:04:16 -0400

On Apr 24, 2014, at 5:45 PM, Quincey Morris <email@hidden> wrote:
> On Apr 24, 2014, at 14:21 , Andy Lee <email@hidden> wrote:
>
>> I still don't see how
>>
>> foo = [@"Something" fallbackIfNil:foo];
>>
>> has any advantage over
>>
>> foo = foo ?: @"Something";
>
> I don’t see how the latter has any advantage over your earlier suggestion [more or less]:
>
> 	if (!foo)
> 		foo = @“Something”;
>
> Admittedly, it takes two lines instead of one.

Well, I'd use braces in the latter case, and I put opening braces on their own line, so for me it'd be four vs. one. :)  But I don't have an objection to the "long form".

Because "foo" appears twice, you could conceivably change one and forget to change the other, but that's true of the ?: version too, which is another argument for a ?:= operator.

Occasionally when there's a repeating pattern I think it's clearer when things fit on one line, because successive lines make the pattern easier to spot.  What I mean is something like

foo = foo ?: [UserPrefs fooValue];
foo = foo ?: [GlobalSettings fooValue];
foo = foo ?: @"LastResort";

Not that I've done it, but this can even be written as

foo = foo ?: [UserPrefs fooValue]
          ?: [GlobalSettings fooValue]
          ?: @"LastResort;

> Is there *any* confusion over the “if” version?

That's an excellent point, and I'm always happy to type a little more to make things clearer.  At the same time, I think reasonable people can differ over which is preferable.  It might just be a matter of acclimation.  As a matter of program logic, binary ?: should be no more confusing than the fact that && and || are short-circuiting operators.  On the other hand, sometimes splitting a boolean calculation into separate statements is clearer than using && and/or ||, so your point applies as well.

Consider how unsettling people find it that Objective-C allows and even expects messages to nil.  That is, we routinely say

[obj doSomething];

instead of

if (obj)
{
    [obj doSomething];
}

After a while, we get used to the Objective-C way and don't have to calculate in our heads every time, "Let's see, if obj is nil, this will return zero, and that's okay because..."  Maybe the same could happen with ?:.

--Andy

_______________________________________________

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: 
 >Good idea/bad idea? (From: Alex Zavatone <email@hidden>)
 >Re: Good idea/bad idea? (From: Luther Baker <email@hidden>)
 >Re: Good idea/bad idea? (From: Lee Ann Rucker <email@hidden>)
 >Re: Good idea/bad idea? (From: Alex Zavatone <email@hidden>)
 >Re: Good idea/bad idea? (From: Andy Lee <email@hidden>)
 >Re: Good idea/bad idea? (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: Good idea/bad idea?
  • Next by Date: Re: ARC Retain Cycles
  • Previous by thread: Re: Good idea/bad idea?
  • Next by thread: Re: Good idea/bad idea?
  • Index(es):
    • Date
    • Thread