I recently stumbled upon a formal design by contract framework for .NET. If you're not familiar with design by contract, you can go here for a quick overview. Basically, it's a methodology that you use when designing your methods. You have a strict definition of the inputs and outputs.
I'm working on a project right now where I'm building a layer on top of the FarPoint grid control. I'm basically creating a grid that is specifically suited to some applications that we're working on. I'm adding a lot of features to the underlying grid, and the complexity has been going up exponentially. Every feature interacts with every other feature. For example, the grid always needs to be in a sorted state, and at the same time could be subjected to filtering, row inserts, data changes, etc. I have an extensive suite of automated unit tests, but it's difficult to test every possible combination of features.
The framework I'm using has a method for checking invariant's. It's simply the concept of checking the state of your object (in this case, the grid), to make sure that it's valid. For example, I can verify that the rows in the grid are sorted correctly. I've created a method that looks at the grid in almost every possible way, and determines if anything looks incorrect.
I run a full invariant check at the beginning and end of every public method. Obviously this causes a significant performance penalty. Fortunately, I can use the .NET conditional attribute to cause the invariant's to not run when in release mode.
- Invariant Check
- Pre-condition Check
- Method logic
- Post-condition Check
- Invariant Check
This checking quickly found a lot of bugs, and it's made it easy for me to locate them. Any time the grid is sorted incorrectly, an exception is thrown at the earliest possible time, so I can quickly locate the offending method. This also compounds the effectiveness of my automated unit tests. A short unit test that tests a small piece of functionality is actually testing that functionality against dozens of other assertions.
This has been a lifesaver.
I'll keep a running list of bookmarks about design by contract here.
posted @ Wednesday, September 19, 2007 1:43 PM