Learning Swift from scratch (day 59)-code layout

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

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: + + 、--. Examples are shown below.

var a = ten= ten+ + C + D

2. Do not have a space before the small opening parenthesis "(" after "small closing parenthesis") ". Examples are shown below.

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

3. There is a space before the large left parenthesis "{". Examples are shown below.

 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:

Func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexpath) {    ...}

It is not recommended to use:

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 process in Xcode is to open the menu xcode→preferences, select the Text Editing tab, and select Show→page Guide at 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 tab, you can set in the 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 break will be aligned with the first break.

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php

Learning Swift from scratch (day 59)-code layout

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.