代码规范,结对要求

此作业的要求:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2147]

结对伙伴:徐常实

(1)       所有符号的左右两边一定要加空格,如果不加空格,会极大的降低代码的可读性。

1 list = [['(', '', '', ')', '', ''], ['', '(', '', '', ')', ''], ['', '', '(', '', '', ')'], ['(', '', '(', ')', '', ')'], \
2         ['((', '', '', ')', ')', ''], ['', '(', '(', '', '', '))'], ['(', '(', '', '', '))', ''], ['', '((', '', '', ')', ')'],\
3         ['(', '', '', '', ')', ''], ['', '(', '', '', '', ')']]

 

(2)       定义的函数的代码行数不宜过长,10行左右最佳。同样是增加代码的可读性。但是我们的能力有限,有几个函数都超过了10行。

 1 def zyz(fenmu):
 2     s = []
 3     while fenmu not in [1]:# 循环保证递归
 4         for index in xrange(2, fenmu + 1):
 5             if fenmu % index == 0:
 6                 fenmu /= index # let n equal to it n/index
 7                 if fenmu == 1: # This is the point
 8                     s.append(index)
 9                 else : # index 一定是素数
10                     s.append(index)
11                 break
12     return s

 

(3)       在写代码时,在能够通过其他方式解决问题的时候,不要使用枚举法,费时费力。(我们曾尝试使用枚举法列举8种除号 ’/’ 存在的位置的情况,虽然实现了功能,但时很费时)。下面是一段已经注释掉的代码:

 1 # if operator1 == '/' and operator2 != '/' and operator3 != '/':
 2     #     truth = eval('Fraction(a,b)' + operator2 + 'c' + operator3 + 'd')
 3     # elif operator1 == '/' and operator2 == '/' and operator3 != '/':
 4     #     truth = eval('Fraction(Fraction(a,b),c)' + operator3 + 'd')
 5     # elif operator1 == '/' and operator2 == '/' and operator3 == '/':
 6     #     truth = eval('Fraction(Fraction(Fraction(a,b),c),d)')
 7     # elif operator1 != '/' and operator2 == '/' and operator3 != '/':
 8     #     truth = eval('a' + operator1 + 'Fraction(b,c)' + operator3 + 'd')
 9     # elif operator1 != '/' and operator2 == '/' and operator3 == '/':
10     #     truth = eval('a' + operator1 + 'Fraction(Fraction(b,c),d)')
11     # elif operator1 != '/' and operator2 != '/' and operator3 == '/':
12     #     truth = eval('a' + operator1 + 'b' + operator2 + 'Fraction(c,d)')
13     # elif operator1 == '/' and operator2 != '/' and operator3 == '/':
14     #     truth = eval('Fraction(a,b)' + operator2 + 'Fraction(c,d)')
15     # else:
16     #     truth = eval('a' + operator1 + 'b' + operator2 + 'c' + operator3 + 'd')
17     # if operator1 == '/' or operator2 == '/' or operator3 == '/':

 

(4)       在定义变量名时,选择用英文释义的方式定义变量,如果随便选择一个名字,会在复查代码时浪费很多时间。(如operator代表运算符,bkl与bkr分别代表左括号与右括号)。

(5)       在定义多个类型和作用都相同的变量时,可以采用同时赋值的方式,可以让代码很简洁。

如:

1 tag1, tag2, tag3 = random.randint(0, 3), random.randint(0, 3), random.randint(0, 3)
2 a, b, c, d = random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), random.randint(0, 9)

 

(6)       在一行代码过长时,应该使用反斜杠 ’\’ 换行。如第(1)条所示。

 

(7)       注意缩进,每次保证用4个空格的缩进,尤其是在Python中,缩进起到了逻辑分层的至关重要的作用。

 

(8)       对关键代码要进行注释。

如:

##后缀表达式计算结果
def calEndSuffixResult(list):
    stack = PyStack()
    sumEnd = 0
    if len(list) == 0:
        return sumEnd
    for i in list:
        if isnumber(i):
            stack.push(Fraction(i))
posted @ 2018-10-07 19:58  张帅zs  阅读(153)  评论(0编辑  收藏  举报