Code layout includes : Blank lines, spaces, line breaks, indents, and so on. The content of the code layout is much more workload, but very important.
Blank Line
Empty lines are separated by logically related code snippets to improve readability. You should always add a blank line in the following situations:
Before the type declaration.
Before and after the import statement.
Between two methods or functions.
Block comments or line comments before.
Two logical segments within a method or function to improve readability.
Between two fragments of a source file.
Space
There are some places in the code that need spaces, and that's a lot of work. The following are specifications that use spaces:
1.there is a space before and after the assignment symbol "=". There is a space between Var or let and the identifier. All two-dollar operators should use spaces to separate them from the operands. The unary operator and operand are not due to the addition of spaces, such as:+ +,--and so on. Examples are shown below.
[HTML]View PlainCopyprint?
- var a = ten
- var c = Ten
- A + = C + D
2.do not have a space before the small opening parenthesis "(" after "small closing parenthesis") ". Examples are shown below.
[HTML]View PlainCopyprint?
- A = (A + b)/(c * d)
3.there is a space before the large left parenthesis "{". Examples are shown below.
[HTML]View PlainCopyprint?
- While a = = d {
- n++
- }
4. There is a space between a method or a function parameter, and a space between the parameter colon and the data type.
Recommended Use:
[HTML]View PlainCopyprint?
- Func TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpath) {
- ...
- }
It is not recommended to use:
[HTML]View PlainCopyprint?
- Func TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpath) {
- ...
- }
Break
The length of a line of code avoids more than 80 characters, in order to make it easier to see if a line of code exceeds 80 characters, many IDE development tools can display 80 lines of vertical line in the editing window settings. The Setup procedure in Xcode is to open the menu xcode→preferences, select the Text Editing tab, and select show→page guideat column.
Because some code longer requires a line break, it can be broken according to the following general specification:
Break after a comma.
Break in front of an operator to select the higher-level operator to break, not the lower-level operator.
The new line should be indented two levels (8 spaces) with the previous line
Indent in
4 spaces are often used as a unit of indentation, and are indented using tabs at development time, although a tab is equal to 8 spaces by default, but the number of tabs and spaces that may be set in different IDE tools will vary. In Xcode, the default is a tab corresponding to 4 spaces, we can open the menu xcode→preferences in Xcode, Select the Text editing→indentation label, you can set in Tab width.
Indentation can be based on the following general specification:
In functions, methods, closures, control statements, computed properties, and so on, contain braces for the "{}" code block, and the contents of the code block are indented one level (4 spaces) from the first line.
If it is a break in the conditional expression in the IF statement, then the new line should be indented two levels (8 spaces) from the previous line, and the next breakwill be aligned with the first break.
Swift Code Layout-Reference