Re: curly braces to return value
Re: curly braces to return value
- Subject: Re: curly braces to return value
- From: Andy Lee <email@hidden>
- Date: Tue, 06 Aug 2013 19:48:47 -0400
On Aug 6, 2013, at 11:41 AM, Matt Neuburg <email@hidden> wrote:
> Just ran across this, and I was wondering what y'all thought of it:
>
> http://cocoa-dom.tumblr.com/post/56517731293/new-thing-i-do-in-code
>
> Of course I knew that you could create a new scope with curly braces, and I sometimes do that just for clarity; for example I might write
>
> {
> CGRect f = thing.frame;
> // do stuff to f
> thing.frame = f;
> }
>
> That's mostly just to clarify that these lines go together, making up for the fact that you can't modify a view's frame by assigning directly into its struct members. The local scope is a bonus. However, I never knew that you could return a value from a curly-braces scope and treat it as an rvalue:
>
> thing.frame = {
> CGRect f = thing.frame;
> // do stuff to f
> f;
> }
>
> It's like, did C suddenly turn into Ruby? :) Anyway, I'd be interested in hearing opinions of this.
I'd say it's a matter of taste, and while I don't have a strong moral objection to the proposed coding style, I don't really care for it.
One of the two advertised benefits is that "the instance variable in which I will store the generated object is in the first line, clearly showing what the next part of the code does. Prior to this, the assignment happend at the end." On the one hand, this does help me see that the overall purpose of this section of code is to do an assignment. The rhs of the assignment is wrapped by delimiters so it looks sort of encapsulated, so if I'm skimming the code, my eye processes this section as "lhs = ({ rhs })".
On the other hand, if I'm reading line by line to get a detailed understanding, by the time I get to the last line I have to glance back up to remind myself what is being done with the value that was just calculated -- in this case, what it is being assigned to. The lhs is now separated by several lines from the value being assigned to it, which makes my eye travel up ("Ah, right, I'm assigning the f we just calculated to thing.frame") and back down to pick up where it left off. Or, if my eye doesn't literally travel, my brain does have to pop a stack and fill in the "thing.frame = " when it sees the "f;", whereas doing it the "old" way it's stated right there: "thing.frame = f;".
That's my gut reaction. Maybe I'm just being green-eggs-and-ham about it and over-arguing the case, and if I saw it all over the place I'd get used to it.
--Andy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden