On Apr 24, 2014, at 5:45 PM, Quincey Morris 
<quinceymor...@rivergatesoftware.com> wrote:
> On Apr 24, 2014, at 14:21 , Andy Lee <ag...@mac.com> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to