天天看点

良好的代码书写格式:适时的代码缩进

刚开始看到这样的代码时候,第一个瞬间是有点将其代码对齐的冲动,好吧,也许是有点强迫症。

// start the slide up animation 

[UIView beginAnimations:nil context:NULL]; 

    [UIView setAnimationDuration:0.3]; 

    // we need to perform some post operations after the animation is complete 

    [UIView setAnimationDelegate:self]; 

    self.pickerView.frame = pickerRect; 

    // shrink the table vertical size to make room for the date picker 

    CGRect newFrame = self.tableView.frame; 

    newFrame.size.height -= self.pickerView.frame.size.height; 

    self.tableView.frame = newFrame; 

[UIView commitAnimations]; 

 可是在同一页面上,又看到了这样的一处类似的代码。

我才瞬间意思到什么。因为这些代码是官方的实例代码。

良好的代码书写格式除了命名之外,必要的格式缩进,能够良好的表达一段功能逻辑代码的开始和结束。

在书写代码的时候,除了if,for语句等的缩进,这种独立功能的写法,委实是比较好的方式,值得好好体会学习。

本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/774268,如需转载请自行联系原作者

继续阅读