点击关注上方“SQL数据库开发”,
设为“置顶或星标”,第一时间送达干货
作者:xybaby
https://www.cnblogs.com/xybaby/p/11335829.html
Bjarne Stroustrup:优雅且高效;直截了当;减少依赖;只做好一件事 Grady booch:简单直接 Dave thomas:可读,可维护,单元测试 Ron Jeffries:不要重复、单一职责,表达力(Expressiveness)
命名的艺术
# bad code
def getItem(theList):
ret = []
for x in theList:
if x[0] == 4:
ret.append(x)
return ret
# good code
def getFlaggedCell(gameBoard):
'''扫雷游戏,flagged: 翻转'''
flaggedCells = []
for cell in gameBoard:
if cell.IsFlagged():
flaggedCells.append(cell)
return flaggedCells
不要挂羊头卖狗肉 不要覆盖惯用缩略语
# bad
def copy(a_list, b_list):
pass
# good
def copy(source, destination):
pass
如果名称读不出来,那么讨论的时候就会像个傻鸟
名字长短应与其作用域大小相对应
注释
The proper use of comments is to compensate for our failure to express ourself in code.
bad
// check to see if the employee is eligible for full benefit
if ((employee.flags & HOURLY_FLAG) && (employee.age > 65))
good
if (employee.isEligibleForFullBenefits())
法务信息 对意图的注释,为什么要这么做 警示 TODO注释 放大看似不合理之物的重要性
函数
函数的单一职责
public class UserValidator {
private Cryptographer cryptographer;
public boolean checkPassword(String userName, String password) {
User user = UserGateway.findByName(userName);
if (user != User.NULL) {
String codedPhrase = user.getPhraseEncodedByPassword();
String phrase = cryptographer.decrypt(codedPhrase, password);
if ("Valid Password".equals(phrase)) {
Session.initialize();
return true;
}
}
return false;
}
}
函数的抽象层级
每个函数一个抽象层次,函数中的语句都要在同一个抽象层级,不同的抽象层级不能放在一起。比如我们想把大象放进冰箱,应该是这个样子的:
def pushElephantIntoRefrige():
openRefrige()
pushElephant()
closeRefrige()

测试
You are not allowed to write any production code unless it is to make a failing unit test pass. 没有测试之前不要写任何功能代码 You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 只编写恰好能够体现一个失败情况的测试代码 You are not allowed to write any more production code than is sufficient to pass the one failing unit test. 只编写恰好能通过测试的功能代码
快速(Fast)测试应该够快,尽量自动化。 独立(Independent) 测试应该应该独立。不要相互依赖 可重复(Repeatable) 测试应该在任何环境上都能重复通过。 自我验证(Self-Validating) 测试应该有bool输出。不要通过查看日志这种低效率方式来判断测试是否通过 及时(Timely) 测试应该及时编写,在其对应的生产代码之前编写
——End——
后台回复关键字:1024,获取一份精心整理的技术干货 后台回复关键字:进群,带你进入高手如云的交流群。 推荐阅读 这是一个能学到技术的公众号,欢迎关注
点击「阅读原文」了解SQL训练营文章转载自SQL数据库开发,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




