Note for Defensive Programming
1.Check Parameters
參數檢查
–Don’t trust the inputs
–Fail quickly and cleanly
–Document exceptions that will be thrown?including unchecked exceptions
-For unexported (non-public) methods, assert the validity of parameters
2.Exceptions are not for control flow
*異常僅用于例外情況,盡量使用標準異常庫
-Exceptions should never be used for ordinary control flow
-Well-designed APIs do not force clients to program to exceptions
3.Cater for client assumptions
-Return empty arrays or collections, not nulls
4.Defensive copying
對于每個可變參數的賦值和構造進行防御性拷貝
-Make defensive copies when needed
*Perform defensive copying before checking validity
*For get methods, return defensive copies of mutable internal fields
5.Immutability?
轉載于:https://www.cnblogs.com/eli-ayase/p/5281971.html
總結
以上是生活随笔為你收集整理的Note for Defensive Programming的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于防御式编程 (Defensive p
- 下一篇: AutoFac教程一:如何去使用Auto