From: Mathieu Bouchard Date: 2006-01-13T04:07:59+09:00 Subject: Re: Design contracts and refactoring (was Re: mathn: ugly warnings) On Fri, 13 Jan 2006, mathew wrote: > *Dean Wampler * writes: > > Let me suggest an XP-style alternative; make thorough unit tests > > required and make sure they "document" - and test! - the design > > "contract". > Unit tests are not an alternative. They are an additional requirement. I find unit-tests to be often decomposable like this. Start with something like this: raise if Blah.new(666) != Blah.new(666) raise if Blah.new(747) != Blah.new(747) raise if Blah.new(242) != Blah.new(242) raise if Blah.new(69) != Blah.new(69) raise if Blah.new(37) != Blah.new(37) then generalize it ("equality is defined based on the arg of .new"): for x in [666,747,242,69,37] do raise if Blah.new(x) != Blah.new(x) end then extract a contract from it: class CheckedBlah < Blah def self.new(x) r = super(x) raise if r != super(x) r end end so now all Blah object creation may be checked throughout actual uses of a program and not just unit tests. The unit test now reduces to: for x in [666,747,242,69,37] do Blah.new(x) end so for many unit tests, all you have to do is just do things and discard the results, and the contract will do the job of checking. _ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - t�l:+1.514.383.3801 - http://artengine.ca/matju | Freelance Digital Arts Engineer, Montr�al QC Canada