20180925-5 code specification, pairing requirements

Source: Internet
Author: User

1, must use has the clear meaning word, prohibits uses the individual letter, for example a,b,c ... To name the variable, (in the specific code, the variable n, can be directly treated as the number, variable A, can be directly regarded as an operator can be used; result, temp and other variables cannot be replaced by a, a, in the process of pairing programming, corrected)

2, in the case of the variable name is particularly long, do not use the hump-type naming rules, you must use the underscore to connect the word. (First_num, Second_num, Third_num, Fourth_num, before the correction of the naming form for Firstnum, Secondnum, Thirdnum, Fourthnum, in the process of pairing programming, has been corrected)

3, the code indentation problem: Because of Python code indentation requirements are particularly strict, so we strictly according to the requirements of indentation. Normal lines of code are not indented before, and the lines of code in the function are indented by the TAB key. (In the programming, the occurrence of indentation can not be indented when the situation, resulting in a program error, such as N=input () before the indentation, resulting in code appearance is not clear, more serious is the code is not out, in the process of pairing programming, was corrected)

4, the code format to its problem: the beautiful readability of code is also an essential part, so in pair programming, requires the same level of code must be to it. (in programming, this has been corrected several times, while the IF statement in the loop is more, not timely to the following statement to it, in the subsequent programming has been corrected)

5, the expression between statements without spaces separated: because I and Luti the same programming habits, I used to separate the operator on both sides with a space, but he did not have this habit, in order to reduce the workload and consider his habits, we chose not to take the space processing.

6. If, If-else, if elif statements use format

if   Condition:    statementsif  condition:     statementselse:    Statements if   Condition:        statementselseif  condition:    statements  Else:    statements

Because in this program, the topic function is to judge the operator, and then to operate, so you must strictly standardize if, if-else, if elif statements using the format, because Luti with the Python basic syntax is not very familiar with, often after if write (), for this corrected several times, for example, in the code

if fourth_num==1:        y=second_num    Else:        assert isinstance (Fourth_num, object)        y=fractions. Fraction (Second_num,fourth_num)

, the wording before the correction is:

if (fourth_num==1)        y=second_num    else        assert  Isinstance (Fourth_num, object)        y=fractions. Fraction (Second_num,fourth_num)

This syntax Python is not run out, in the pair programming it is corrected!

7) Try not to write too long statements.

I think that if the statement is too long, the statement should be split into several short statements to ensure that the code has good readability. When a statement is too long, the escape character is used at the end.

For example:

' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras magna Turpis, Viverra et cursus eget, efficitur sed eros. Curabitur finibus ultricies nisi, nec convallis nunc gravida sed. Nam ac facilisis eros, quis tincidunt turpis. Vestibulum quis mi quis justo faucibus condimentum. Aliquam accumsan ultricies orci ID mollis. Mauris faucibus dignissim luctus. Aenean lacus nunc, ultricies ac ante eu, Fringilla accumsan nisi. Cras et nunc posuere, Mattis mi at, Rutrum Felis. Etiam volutpat neque sodales condimentum consectetur. '

After the change:

" Lorem ipsum dolor sit amet, consectetur adipiscing elit. aliquam bibendum Purus at Nibh rutrum Dapibus. Aliquam quis sem sagittis, rutrum magna Quis, Gravida quam. Sed Porta ex nec Leo Elementum, a Efficitur neque ornare. Ut tristique nunc vitae ultrices molestie. "

8) Make necessary comments on key code and important business logic code.

The core functions and key steps must be well commented to facilitate the readability of the code. There are two common types of annotations in Python: #和三引号. #用于单行注释, three quotation marks are often used for annotations of a large section of descriptive text. For example:

def counts (file_name):        Total=0        file=open (file_name,"R")          for  in File.readlines ():   // read each line             words=line.split (")/    / divide each line of words with a space separator, and return a list             of stringstotal +=len (words)        // calculate the number of words per line, and        then draw a sum of print (    total)//output All the words of the document

Give comments in the key core section so that I and the two sides good understanding and communication.

9) Reduce the independent calculation in the inner cycle, as far as possible to the outer layer extraction.

For example:

 for () {    for() {        a=a*b*C;    }}

You should calculate the A*b*c first and then put it in the loop. Otherwise, each cycle will waste resources.

10) Each import statement imports only one module, preferably in the order of standard libraries, extension libraries, custom libraries, and so on.

For example:

Import Random Import Fractions Import string

Try to avoid importing the entire library, importing only the object libraries you want to use, making the program run faster.

20180925-5 code specification, pairing requirements

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.