Reference: github.com/raywenderli…

1. The constant

Use type constants rather than macros.

static NSString * const STMProjectName = @"GCDFetchFeed"
Copy the code

2. The variable

Variable names should clearly indicate the function and be suffixed with the type. Before using a variable, you need to initialize it as close to where it will be used as possible. Do not abuse global variables and use them to pass values as little as possible. Passing values through parameters can reduce the coupling between functional modules.

NSString *nameString = @"Tom";
nameLabel.text = nameString;
Copy the code

3. Conditional statements

If there are too many nested branches of conditional statements, switch is preferred for faster operation.

recommended

if(! error) {return success;
}
Copy the code

Is not recommended:

if(! error)return success;
Copy the code

4. The function

Function size should not be too large, preferably within 100 lines of code. If a function has a lot of internal logic, we can break it down into smaller logic and extract each smaller logic as a separate function. Each function deals with the smallest unit of logic and then goes up one layer at a time. This way we can use the function name to clarify the purpose of that logical processing and improve the readability of the code. It should be noted that after splitting into multiple functions with simple logic, the input parameters of the function should be verified to improve the robustness of the code.

- (void)saveName:(NSString *)nameStrif(! nameStr) {return; } // Contents}Copy the code

Class 5.

Class headers should import as little as possible from other classes. This can be declared with the class keyword and then introduced into the implementation file as required headers for other classes.

#import <UIKit/UIKit.h>

@class Model;

@interface ViewController : UIViewController


@end
Copy the code

6. Code organization

Use #pragma mark – to categorize the methods in the functional grouping and protocol/delegate implementation according to this general structure.

#pragma Mark - Build interface
#pragma Mark - Web request
Pragma mark - Proxy method
#pragma mark - Encapsulates the method
#pragma Mark - Click event
Copy the code

Distance between 7.

recommended

if(user.ishappy) {// do something}else{// do something}Copy the code

Is not recommended

if(user.ishappy) {// do something}else{// do something}Copy the code

recommended

[UIView animateWithDuration:1.0 animations:^{// something} completion:^(BOOL finished) {// something}];Copy the code

Is not recommended

[UIView animateWithDuration:1.0 animations:^{// something} completion:^(BOOL finished) {// something}];Copy the code

8. Enumeration types

When using ENums, the new fixed base type specification is recommended because of its stronger type checking and code completion capabilities. The SDK now includes a macro that promotes and encourages the use of fixed base types: NS_ENUM()

Typedef NS_ENUM (NSInteger, RWTLeftMenuTopItemType) {RWTLeftMenuTopItemMain, RWTLeftMenuTopItemShows,  RWTLeftMenuTopItemSchedule };Copy the code

9. Boolean value

Objective-c uses YES and NO. True, false applies to CoreFoundation, C, or C ++ code.

recommended

if(someObject) {}if(! [anotherObject boolValue]) {}Copy the code

Is not recommended

if(someObject == nil) {}if([anotherObject boolValue] == NO) {}if(isAwesome == YES) {} // Do not do this.if(isAwesome = =true) {} // Never do this.Copy the code

10. Conditional coding

When using conditional encoding, use less nested if statements and more return statements.

recommended

- (void)someMethod {
  if(! [someOther boolValue]) {return;
  }

  //Do something important
}
Copy the code

Is not recommended

- (void)someMethod {
  if ([someOther boolValue]) {
    //Do something important
  }
}
Copy the code