IOS code Specification

Core principles

Writing elegant code is like organizing a room, so that the code structure is a tree with branches. Everything a program does is passing and branching information.

The code should be simple and easy to understand, logical, modular code

  1. Clean code is easy to spot bugs, and you need to write code that is obviously bug-free, not bug-free

  2. Avoid writing functions that are too long. Functions should be no longer than 40 lines, which is about the height of the viewing Angle on the screen, and logically break up smaller functions

  3. Make small utility functions, and pull out the duplicate code to make functions, although some of the duplicate code is only 2 lines long, and extract it to make functions can simplify the logic in the main function

  4. Each function does one simple thing,

For example, we might sometimes write “generic” functions that are chosen to do what they do based on internal criteria

func foo() { if getOS().equals("MacOS") { a(); } else { b(); } c(); If getOS().equals("MacOS") {d(); } else { e(); }}Copy the code

func fooMacOS() {
  a();
  c();
  d();
}

func fooOther() {
  b();
  c();
  e();
}

Copy the code

Write readable code

  1. Increase the expressiveness of programming languages, using meaningful function and variable nouns that can realistically describe their logic
Put (elephant1, Fridge2) is obviousCopy the code
  1. Local variables should be as close to where they are used as possible, short and not reusable, and repeated assignments can be confusing.

Example:

int index = ... . . . Bar (index) may not use index in the middle or change the dependent data, so it is easy to read it as if it is storing some kind of changed data, or has been modified in the middle. If you put index in the bottom, you know that index does not hold variable values. I didn't make any changesCopy the code
. . . int index = ... bar(index)Copy the code
  1. Remove the complicated logic and make it a helper function

  2. Extract the complex expression, make it intermediate variables, taboo deep nesting


make.left.equalTo(view.snp.left).offset(10) 

Copy the code

Programming for change, not requirements

Requirements are only temporary, code is not easy to meet requirements, write extensible and easy to modify the program, the code is your lover

Code specification

class

  1. The layout of the class

When classifying the layout, you can specify what the function points are

#pragma mark - Life Cycle Methods
- (instancetype)init
- (void)dealloc

- (void)viewWillAppear:(BOOL)animated
- (void)viewDidAppear:(BOOL)animated
- (void)viewWillDisappear:(BOOL)animated
- (void)viewDidDisappear:(BOOL)animated

#pragma mark - Override Methods

#pragma mark - Intial Methods

#pragma mark - Network Methods

#pragma mark - Target Methods

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - UITableViewDataSource  
#pragma mark - UITableViewDelegate  

#pragma mark - Lazy Loads

#pragma mark - NSCopying  

#pragma mark - NSObject Methods

Copy the code

Abnormal handling of fetching interface

On the UI side, warning handling and error handling on each interface need to be considered, not just empty data, in case of unnecessary problems

variable

  1. Variable names are humped

Classes, protocols use large hump objects and other local variables use small hump


HomePageViewController.h
<HeaderViewDelegate>

NSString *personName = @"";
NSUInteger totalCount = 0;

Copy the code
  1. Variable names contain function and type
UIButton *addBtn // Add button UILabel *nameLbl // name tag NSString *addressStr// Address stringCopy the code
  1. The system often adds suffixes to class instance variable declarations
Type/suffix UIViewController VC UIView View UILabel Lbl UIButton Btn UIImage Img UIImageView ImagView NSArray Array NSMutableArray Marray NSDictionary Dict NSMutableDictionary Mdict NSString Str NSMutableString Mstr NSSet Set NSMutableSet Mset

Copy the code

attribute

  1. Attributes are named using small humps

  2. Instantiating an object costs resources, and if a property in the object requires “a lot of configuration and calculation “, we can use lazy loading, loading it just before it is used

- (NSDateFormatter *)dateFormatter 
{
    if(! _dateFormatter) { _dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
           [_dateFormatter setLocale:enUSPOSIXLocale];
           [_dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    } 
    return _dateFormatter;
}

Copy the code

NSArray&NSMutableArray

AddObject does a non-null check, takes a subscript to see if it’s out of bounds, and you can initialize the array with lazy load before you get the data

The operator

  1. There is no space between unary operators and variables
! isTopCopy the code
  1. Space between binary budget symbols

3 + 1

Copy the code

annotation

  1. Special logic processing

  2. API branch processing, when required, indicates the ipA call

  3. Use run-time method exchange, indicated