Brief Principle & Practice

of Coding
@3D 2017-07-18
叩问灵魂的⼏几个问题
Agenda
๏ 好代码的特征
๏ Principle
๏ Best Practice
好代码的特征
可持续发展
可读性
可维护性可靠性
Principle
Don't Repeat Yourself
๏ 杜绝重复信息
๏ 系统中每⼀一部分,有单⼀一、明确、
权威的代表
KISS
๏ Keep Is Simple, Stupid
๏ 抓住问题核⼼心
๏ 避免过度设计/过早优化
๏ 降低维护成本
SOLID
๏ Single Repository Principle - 单⼀一功能原则
๏ Open-Close Principle - 开闭原则
๏ Liskov Substitution Principle - ⾥里里⽒氏替换原则
๏ Interface-Segregation Principle - 接⼝口隔离原则
๏ Dependency inversion Principle - 依赖反转原
则
Single Responsibility
Principle
๏ 每个类都只有单⼀一的职责
Open-Close Principle
๏ 对扩展是开放的
๏ 对修改是bu变的
Liskov Substitution
Principle
๏ ⼦子类可以代替⽗父类被使⽤用
Interface-Segregation
Principle
๏ 调⽤用者应当依赖仅仅它需要接⼝口
๏ 将⼤大接⼝口拆为多个⼩小接⼝口
๏ Readable / Closable / Iterable
Dependency Inversion
Principle
๏ ⾼高层次模块不不应该依赖于低层次
的实现,⽽而应该依赖于抽象接⼝口
๏ 抽象接⼝口不不应该依赖具体实现
SLF4J
Pythonic
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
๏ 美丽优于丑陋陋,明讲好过暗喻
๏ 简洁者为上,复杂者次之,繁涩者为下
๏ 特例例不不能打破原则
๏ 不不要忽略略错误提示
๏ 模棱两可时候,不不要猜测
๏ 使⽤用⼀一种明确的⽅方法,最好是⼀一种公认的
Best Practice
完
最佳实践包含哪些?
๏ 标准的命名
๏ 标准的结构
๏ 标准的业务流程
异常处理理的最佳实践
๏ 不不要忽略略异常
๏ 只针对异常的情况才使⽤用异常
๏ 对可回复的情况使⽤用受检异常,
对编程错误使⽤用运⾏行行时异常
๏ 优先使⽤用标准的异常
๏ 抛出与抽象相对应的异常
๏ 细节消息中包含能捕获失败的信
息
尽⼒力力解决 IDE 提示的所
有 ERROR / Warning
⼀一个函数不不要超过⼀一个
屏幕
避免使⽤用字⾯面量量
–伊⼽戈忍索得
「这不不是⼀一场分享,期望这是⾏行行动指南」
Reference
๏ SOLID, DRY, SLAP design principles
๏ Single Level of Abstraction (SLA)
๏ SOLID (⾯面向对象设计) - Wikipedia
๏ 敏敏捷软件开发 - Wikipedia
๏ ⼀一次且仅⼀一次 - Wikipedia
๏ KISS principle - Wikipedia

Brief principle and principle of coding