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.
Subscribe to:
Post Comments (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...
-
Many developers that I work with avoid working with msbuild. This is a shame, since a little msbuild knowledge can go a long way. Her...
-
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...
-
Context: I have been a (proud) Windows fanboy since Win3.11. At Uni I studied physics, by the end of my masters, I knew I wanted to be a dev...
No comments:
Post a Comment