I've been experimenting with .NET 4 Code Contracts for a couple of months.
Contracts themselves are simple enough to understand. The potential to produce stabler code, improve readability, and some help with the pesky 'object null' problem makes them very attractive.
A few blog posts, such as Billy McCafferty, and a series from Derik Whittaker help get up to speed.
My initial work with Contracts started with simple require not null type contracts, and soon escalated to more useful contracts such as 'no nulls in a list', FileInfo.Exists == true. What I found was a simple rule helped to limit what to declare for a contract:
Don't Require more than what a caller can simply (one-line) Ensure.
It is easy for a caller to check an object is not-null before calling a library... but out-of-the-box checking for more complex constraints, such as list contains no-nulls made the code less readable. The idiom above doesn't stop me from using more complex constraints, but it does ensure I provide a simple (one line) way for callers to ensure the constraint.
Showing posts with label DbC. Show all posts
Showing posts with label DbC. Show all posts
Tuesday, January 18, 2011
Monday, November 15, 2010
CodeContracts - yum
I just spent the weekend converting a little in-home code over to using the new .Net4 code contracts. My opinion: FANTASTIC.
My initial goal for the weekend, was to create a simple demo, to see whether I'm in the
A) keep unit-tests for contracts. Or
B) don't unit test a contract.
Camp. Opinion on-the-net seams divided over this, so I wanted to make my own mind up by using CodeContracts in a non-trivial piece of software.
What I found: my code was full of potential bugs :( The starting code base wasn't my best work, but I did have a good set of unit tests for it. I was expecting the code-contracts to highlight some weekneses, but it ended up forcing me to think a lot more about the possible problems, so I spent my time fixing what looked like decent code.
To that end I now think
1. Writing verifiable code with CC is a must do.
1.1 it takes a lot more time (script kiddies will hate it), but better time now then bugs later.
2. The current lack of static-checking for Contract.ForAll and Contract.Exists is a problem.
3. Using Contract.Assume is a crappy, currently needed hack.
4. CC tutorials and examples need to focus on Contract.Ensure, not Contract.Requires. To my mind being able to express guarantees on what-my-code does is more important than expressing limits that clients need to obey.
5. Think of CC as like writing tests, docx, and defensive programming all rolled int a couple of code lines.
6. CC forces you to think far more critically. Sometimes, you won't believe it's warnings. Sometimes it'll be because the static checker isn't smart enough, but mostly it'll be the developer isn't looking close enough.
7. CC is NOT just for null checks, if you only Ensure not-null, your missing a lot of the power.
Requires that use more than the input params need to be simple for callers to check.
Sample contract on a duel-construct such as ui ctor + create.
Simple contract on disposable / disposed object.
My initial goal for the weekend, was to create a simple demo, to see whether I'm in the
A) keep unit-tests for contracts. Or
B) don't unit test a contract.
Camp. Opinion on-the-net seams divided over this, so I wanted to make my own mind up by using CodeContracts in a non-trivial piece of software.
What I found: my code was full of potential bugs :( The starting code base wasn't my best work, but I did have a good set of unit tests for it. I was expecting the code-contracts to highlight some weekneses, but it ended up forcing me to think a lot more about the possible problems, so I spent my time fixing what looked like decent code.
To that end I now think
1. Writing verifiable code with CC is a must do.
1.1 it takes a lot more time (script kiddies will hate it), but better time now then bugs later.
2. The current lack of static-checking for Contract.ForAll and Contract.Exists is a problem.
3. Using Contract.Assume is a crappy, currently needed hack.
4. CC tutorials and examples need to focus on Contract.Ensure, not Contract.Requires. To my mind being able to express guarantees on what-my-code does is more important than expressing limits that clients need to obey.
5. Think of CC as like writing tests, docx, and defensive programming all rolled int a couple of code lines.
6. CC forces you to think far more critically. Sometimes, you won't believe it's warnings. Sometimes it'll be because the static checker isn't smart enough, but mostly it'll be the developer isn't looking close enough.
7. CC is NOT just for null checks, if you only Ensure not-null, your missing a lot of the power.
Requires that use more than the input params need to be simple for callers to check.
Sample contract on a duel-construct such as ui ctor + create.
Simple contract on disposable / disposed object.
Subscribe to:
Posts (Atom)
A short list of C# coding issues.
Injecting services into entities It been bugging me for a while now that to use services in a entity can be a bit of pain. It usually starts...
-
Recently I needed to create a link to edit a content item in Orchard . Orchard uses a url like http://mysite/Home/Admin/Contents/Edit/ 12...
-
Recently while migrating projects from MVC.Net 5 to core 1 rc1 (mvc6 / vNext) I ran into an IoC problem. I was using Autofac as my conta...
-
I like the idea of coding a Rich domain model, and then applying services to the model. Recently I was trying to get just a little closer to...