博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码规范 结对要求
阅读量:4467 次
发布时间:2019-06-08

本文共 2775 字,大约阅读时间需要 9 分钟。

此作业的要求:

结对伙伴:张帅

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

list = [['(', '', '', ')', '', ''], ['', '(', '', '', ')', ''], ['', '', '(', '', '', ')'], ['(', '', '(', ')', '', ')'], \         ['((', '', '', ')', ')', ''], ['', '(', '(', '', '', '))'], ['(', '(', '', '', '))', ''], ['', '((', '', '', ')', ')'],\         ['(', '', '', '', ')', ''], ['', '(', '', '', '', ')']]

 

 

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

 

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

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

# if operator1 == '/' and operator2 != '/' and operator3 != '/':      #     truth = eval('Fraction(a,b)' + operator2 + 'c' + operator3 + 'd')      # elif operator1 == '/' and operator2 == '/' and operator3 != '/':      #     truth = eval('Fraction(Fraction(a,b),c)' + operator3 + 'd')      # elif operator1 == '/' and operator2 == '/' and operator3 == '/':      #     truth = eval('Fraction(Fraction(Fraction(a,b),c),d)')      # elif operator1 != '/' and operator2 == '/' and operator3 != '/':      #     truth = eval('a' + operator1 + 'Fraction(b,c)' + operator3 + 'd')      # elif operator1 != '/' and operator2 == '/' and operator3 == '/':     #     truth = eval('a' + operator1 + 'Fraction(Fraction(b,c),d)')     # elif operator1 != '/' and operator2 != '/' and operator3 == '/':     #     truth = eval('a' + operator1 + 'b' + operator2 + 'Fraction(c,d)')     # elif operator1 == '/' and operator2 != '/' and operator3 == '/':     #     truth = eval('Fraction(a,b)' + operator2 + 'Fraction(c,d)')     # else:     #     truth = eval('a' + operator1 + 'b' + operator2 + 'c' + operator3 + 'd')     # if operator1 == '/' or operator2 == '/' or operator3 == '/':

 

 

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

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

如:

 

tag1, tag2, tag3 = random.randint(0, 3), random.randint(0, 3), random.randint(0, 3) 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))

 

转载于:https://www.cnblogs.com/xvcs/p/9751205.html

你可能感兴趣的文章
用python实现矩阵转置
查看>>
linux 小技巧(磁盘空间搜索)
查看>>
iOS开发——捕获崩溃信息
查看>>
(for 循环)编程找出四位整数 abcd 中满足 (ab+cd)(ab+cd)=abcd 的数
查看>>
tomcat使用spring-loaded实现应用热部署
查看>>
boost1.53中的lock-free
查看>>
链表_leetcode203
查看>>
基于ajax 的 几个例子 session ,ajax 实现登录,验证码 ,实现ajax表单展示
查看>>
连接不上sql server服务器的解决方案
查看>>
记录安装oracle的那些事(二)之双系统安装
查看>>
c3po数据库连接池中取出连接
查看>>
bootstrap-table 分页
查看>>
使用本机IP调试web项目
查看>>
【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?
查看>>
C++ Primer 第六章 函数
查看>>
交互设计算法基础(3) - Quick Sort
查看>>
Ubuntu各种软件的安装
查看>>
Android电源管理
查看>>
C#_基础_方法以及方法重载(十)
查看>>
新起点新希望
查看>>