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 : empty 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.

var a = 10var c = 10a + = 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

    line of code to avoid exceeding 80 80 Span style= "font-family: ' The song Body '; A lot of characters, many ide development tools can be displayed in the edit window settings 80 line vertical. In xcode the setup process is to open the menu text Editing 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

< Span style= "FONT-FAMILY:COURIER;FONT-SIZE:11PX;" >  indent

4 A space is often used as a unit of indentation, and is indented at development time using tabs, although by default a tab is equal to 8 ide The number of tabs and spaces that may be set in the tool will vary. In xcode 4 spaces, we can be in xcode xcode→preferences text editing→indentation tags, which can be tab width &NBSP;

Indentation can be based on the following general specification:

    • include braces in functions, methods, closures, control statements, computed properties, and so on {}' code block, 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
650) this.width=650; "title=" 00.png "src=" http://s4.51cto.com/wyfs02/M01/7C/F2/wKiom1bdBSbSn2wcAAAs2MBEZnc219.png "alt=" Wkiom1bdbsbsn2wcaaas2mbeznc219.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php


This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1748346

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.