天天看點

良好的代碼書寫格式:适時的代碼縮進

剛開始看到這樣的代碼時候,第一個瞬間是有點将其代碼對齊的沖動,好吧,也許是有點強迫症。

// 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,如需轉載請自行聯系原作者

繼續閱讀