Unified coding specification can only avoid the lack of mutual identity among team members caused by inconsistent code styles.

Good code specifications require constraints in eight areas: constants, variables, attributes, conditional statements, loops, functions, classes, and classes

constant

When using constants, it is recommended to use type constants rather than macro definitions. For example, defining a string constant can be written as:

static NSString * const kStringName = @"kStringName";

Or in global constant files, defined in.m files:

NSString * const KALERTMESSAGE_BLOCK_SUCCESS = @" mask succeeded ";

External declaration in.h file:

UIKIT_EXTERN NSString * const KALERTMESSAGE_BLOCK_SUCCESS;

First of all, as a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public account: Programming Daxin, no matter you are small white or big bull are welcome to enter. Let us progress together and develop together!

variable

The variable name should clearly indicate the function, preferably with the type suffix. This makes it clear what each variable does, rather than treating one variable as a different value in different places. Before using, you need to initialize the variable first, and the initialization is as close to the place where it is used as possible do not abuse the global variable, as little as possible to use it to pass value, through the parameter value can reduce the coupling between function modules such as let nameString = “geekiness “;

attribute

In Objective-C, try to use the get method for lazy loading of properties to avoid useless memory usage and unnecessary calculations.

If the calculation attribute of Swift is read-only, the get clause can be omitted. Example code is as follows:

var areaDouble : Double { return long * width }

Conditional statements

In conditional statements, all branch conditions that may be involved in a conditional statement need to be considered, and each branch condition needs to be considered and processed, reducing or not using default processing. In particular, Switch does not have a default branch when dealing with enumerations.

In addition, conditional statements should not have too many nested branches. You can make full use of guard syntax in Swift, such as this example code

If let userName = login.userNameOK {if let password = login.passwordOK {// Login processing... } else { fatalError("login wrong") } } else { fatalError("login wrong") }

The above code indicates that the guard syntax can be used when the user name and password are in order

Guard let userName = login.userNameOK, let password = login.passwordOK, else {fatalError("login wrong")}

Looping statements

Continue and break should be used sparingly in loop statements. Guard syntax can also be used to solve this problem. The solution is to use guard syntax for everything that needs to continue and break, putting all exceptions in one place. This has the advantage of being easy to read at maintenance time, making the code easier to read and understand.

function

For functions, the size should not be too large, preferably within a hundred lines of code. If the function has too much internal logic, we can break the complex logic into smaller ones. Each small piece of logic is extracted as a separate function, each of which deals with the smallest unit of logic, and then combined layer by layer. In this way, we can use the function name to clarify the purpose of that logical processing, and improve the readability of the code. Once we have broken down the functions into logically simple ones, we need to validate the function’s input arguments, and the same guard syntax applies to checking the input arguments.

func saveRSS(rss: RSS? , store: Store?) {guard let RSS = RSS else {return} guard let store = store else {return}

In addition, the use of global variables to transfer data inside the function should be avoided as far as possible. Using parameters or local variables to transfer data can reduce the function’s dependence on the outside, reduce coupling, improve the independence of the function and improve the accuracy of unit test.

class

In Objective-C, class headers should import as few headers as possible from other classes, which can be declared using the class keyword, and then import as many headers as necessary from other classes into the implementation file. In the case of inheritance and follow the agreement, cannot avoid to other class header file, so when the code design or try to minimize inheritance, especially when inheritance relationships too much is bad for maintenance and modification of the code, such as to modify the parent class influence on all subclasses, also need to consider when if the assessment is not complete, impact is difficult to control

classification

When writing the classification, the method name added in the classification should be prefixed as far as possible, and if it is the classification of the system class, the method name must be prefixed to avoid the problem of method name duplication. Classification is suitable for the maintenance of different functional codes according to different categories when multiple people are responsible for the same class.

Code Review

Before Code is reviewed, you can use static Review tools to do a thorough Review of your submitted Code.

For Swift, you can use the SwiftLint tool to check the code specification,

For Objective-C you can use OCLint to do code specification checks using OCLint custom MVVM rules.

Finally, manual inspection

The code specification doesn’t need to be complicated, just stick to the principle of making the code logical, and the rest of the rules are built around that principle. Logical code is the most basic and necessary condition of high-quality code engineering, if the code is not clear, then the rest of the extension, reuse, simplicity and elegance are irrelevant.