6
基于合同的编程与单元测试
我是一个有点防御性的程序员,并且是Microsoft代码合同的忠实拥护者。 现在我不能总是使用C#,在大多数语言中,我唯一拥有的工具就是断言。所以我通常会得到这样的代码: class { function() { checkInvariants(); assert(/* requirement */); try { /* implementation */ } catch(...) { assert(/* exceptional ensures */); } finally { assert(/* ensures */); checkInvariants(); } } void checkInvariants() { assert(/* invariant */); } } 但是,这种范式(或您所说的任何范式)会导致很多代码混乱。 我开始怀疑这是否值得付出努力,是否已经进行了适当的单元测试?