"Learn Swift from scratch" study notes (Day 59) - code typesetting

Original article, welcome to reprint. Please indicate: Guan Dongsheng's blog

 

Code layout includes : blank lines, spaces, line breaks, and indentation. The content of the code typesetting is more and the workload is a lot, but it is very important.

Blank line

Blank lines separate logically related sections of code to improve readability. Blank lines should always be added when:

  •    before the type declaration.
  •    Before and after the import statement.
  •    between two methods or functions.
  •    before block or single-line comments.
  •    Between two logical sections within a method or function to improve readability.
  •    between two fragments of a source file.                                                                                                space

Spaces are required in some positions in the code, and this workload is also very large. The following are the specifications for using spaces:

1. There is a space before and after the assignment symbol " = ". There is a space between var or let and the identifier. All binary operators should be separated from their operands by a space. Spaces should not be added between unary operators and operands, such as: ++ , -- etc. Examples are as follows.

var a = 10
var c = 10
a += c + d

 

 

2. There should be no space after the small left parenthesis " ( " and before the small right parenthesis " ) ". Examples are as follows.

a = (a + b) / (c * d)

 

 

3. There is a space before the opening brace " { ". Examples are as follows.

while a == d {
    n++
}

 

 

4. There is a space before the method or function parameter, and there is a space between the parameter colon and the data type.

Recommended Use:

func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
 
    ...
}

 

Deprecated:

func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
    ...
}

 

 

Decisive action

Try to avoid the length of a line of code exceeding 80 characters. In order to check whether a line of code exceeds 80 characters, many IDE development tools can display 80 vertical lines in the editing window. The setup process in Xcode is to open the menu Xcode→Preferences , select the Text Editing tab, and select Show→Page guide at column .

 

Since some codes are relatively long and need to be broken, they can be broken according to the following general specifications:

<!--[if !supportLists]--> l   <!--[endif]--> Break after a comma.

<!--[if !supportLists]--> l   <!--[endif]--> Break in front of an operator, to choose higher-level operator break, not lower-level operator break .

<!--[if !supportLists]--> l   <!--[endif]--> The new line should be indented two levels ( 8 spaces) from the previous line

 

indentation

4 spaces are often used as a unit of indentation typesetting. Tabs are used for indentation during development. Although a tab is equal to 8 spaces by default, a tab may be set in different IDE tools. The corresponding number of characters and spaces will be different. In Xcode , a tab corresponds to 4 spaces by default. We can open the menu Xcode→Preferences in Xcode , select the Text Editing→Indentation label, and set the Tab width .

 

Indentation can be based on the following general specifications:

<!--[if !supportLists]-->l   <!--[endif]-->在函数、方法、闭包、控制语句、计算属性等包含大括号“{}”代码块中,代码块中的内容与首行缩进一个级(4个空格)。

<!--[if !supportLists]-->l   <!--[endif]-->如果是if语句中条件表达式的断行,那么新的一行应该与上一行缩进两个级别(8个空格),再往后的断行要与第一次的断行对齐。

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327044664&siteId=291194637