开发者

Formatting tips for Objective-C nested blocks

I don't mean to start a code formatting holy war here, but I'm wondering if anyone has any advice on how to best format nested blocks in Objective-C.

Here's some code I wrote today:

 [UIView animateWithDuration:1.0
      animations:^{
       self.alpha = 0.5;
      }
      completion:^(BOOL finished){
       [UIView animateWithDuration:1.0
            animations:^{
             winView.alpha = 1.0;             
     开发者_Python百科       }
            completion:^(BOOL finished){
             winView.alpha = 0.0;
            }];
  }];

I pretty much let Xcode format it, and it's not terrible. But I'm kinda afraid in six months I'll stumble across this to fix some bug, and want to punch myself in the face.

Anyone have any pointers on how to make nested blocks as readable as possible?


And that is exactly why the common standard is to have only one block parameter to a method or function and always have the block as the last one. In this case, this was likely the most natural thing for UIView to offer and there you have it.

In any case, your approach is the right one. If anything, I might break out the inner block as a local variable and pass it as an argument. Or maybe more of the blocks.

However, the approach of being able to select an entire file and have Xcode reformat the whole thing is a tremendous time saver; I do select-all-tab on my source all the time. Minimizes diffs and inconsistencies.


The real issue here is methods that take multiple block arguments, which make it either ugly or hard to read. I haven't written any real code that needs to use it yet, but one possibility is to separate the } and ]; like this:

[UIView animateWithDuration:1.0
    animations:^{
        self.alpha = 0.5;
    }
    completion:^(BOOL finished){
        [UIView animateWithDuration:1.0
            animations:^{
                winView.alpha = 1.0;             
            }
            completion:^(BOOL finished){
                winView.alpha = 0.0;
            }
        ];
    }
];

This breaks the standard formatting of blocks (which keeps } and ]; together), but I think it is slightly more readable.


How I would recommend

[UIView animateWithDuration:0
                 animations:^
 {
     [UIView animateWithDuration:1
                      animations:^
      {
          <#code#>
      }];
 }
                 completion:^(BOOL finished)
 {
     [UIView animateWithDuration:1
                      animations:^
      {
          <#code#>
      }
                      completion:^(BOOL finished)
      {
          <#code#>
      }];
 }];

The default behaviour

[UIView animateWithDuration:0
                 animations:^{
                     [UIView animateWithDuration:1
                                      animations:^{
                                          <#code#>
                                      }];
                 } completion:^(BOOL finished) {
                     [UIView animateWithDuration:1
                                      animations:^{
                                          <#code#>
                                      } completion:^(BOOL finished) {
                                          <#code#>
                                      }];
                 }];

(note that it is exactly the same code)

I know Xcode forces to open braces at the same line, but please note that in case of blocks opening braces at the new line allows you to write code 4 (!!!) times closer to the left.

Also, I think it improves the readability.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜