From: Florian Gross Date: 2004-12-09T08:52:29+09:00 Subject: Re: Shield Pattern (was Re: Freezing Variable Assignment) Eric Hodel wrote: > On 08 Dec 2004, at 13:12, Nicholas Van Weerdenburg wrote: >> Immutability also speaks to this- it's an interface contract that says >> "read-only", limiting side effects. Having experienced the beauty of >> making objects immutable and watching the exceptions flare was a >> epiphany for me in Java development. > > I've used unit tests for this more that immutability. Your same > euphoric state can be had with a solid, comprehensive set of unit > tests. (Not to say that immutability can't help.) I agree heavily with this. Specification by testing is a great way of ensuring that things work. Note that you can also state what things should *not* be allowed which can be just as important as testing that things are allowed and work correctly. Unit testing in general leads you to decoupled components that you can swap out easily with Mocks (which means that in the real code you will be able to replace them with similar Objects that use the same interface) for ensuring that interfaces are used correctly. I'd like to recommend the "Test-driven development by example" book -- it explains well how to enforce code quality via tests because it uses tests to state what should be done before it is done. Whether it does always make sense to do that is another factor, but it is certainly an interesting way of looking at Unit Testing. Another point I'd like to address is integrating specification and documentation. I'm doing this by embedding simple test cases into the documentation in the style of sample code. I can extract those and verify that the samples in the documentations still make sense easily. Personally, I would also like to see a way of testing whether a given Object implements a contract (here: passes a suite of unit tests) -- I think that would be useful for multi method dispatch. (Called method overloading in the C++/Java world.) Unit testing and how they can be integrated into development is a very interesting topic. I think they can be used for much more than they usually are.