Recently, I started to use Code contracts for .net. The idea of code contracts itself is great in my opinion, but the implementation is very unpleasant.
The main reasons I don't like it are:
- I can use only methods like
Contract.Require()
inside my procedures.ContractAbbreviator
s have so many restrictions (like, I cannot put them in a separate assembly and I cannot use parameters) that it makes them less usable. There are no attributes, and no extension methods, so my code becomes very verbose. For example, if I just want to check that my return value of typeDictionary<string, string>
is not null, I need to add a monster such asContract.Ensure(Contract.Result<Dictionary<string, string>> != null)
. It's not even readable. - The static analyzer makes so many false alarms that I spend more time shutting it up than fixing actual problems.
- It is extremely slow. Even though it has some cache, it takes a few minutes to analyze even a small project, and that makes it useless for incremental fixing. It just takes too long.
- There are bugs in
ccrewriter
—it cannot chew half of assemblies, and it cannot survive .net 4.0 assemblies in the .net 4.5 runtime. - There is runtime/static checker dualism. When I had just started I thought it was as simple as
Debug.Assert
—you just add them everywhere you feel you need. But it turns out that I need to prove everything to the static checker, which is advanced, for sure, but the checker is stupid sometimes and can't resolve many obvious code constructions (likewhile
). And there's no instrument to just say to the analyzer "I know what I'm doing, just ignore this contract violation". - You cannot relate conditions. For example, there is no way to explain to the static analyzer that if I check
string.NotNullOrEmpty
this includesstring != null
. Or if I have my own big procedure checking a file path, I cannot explain to the analyzer that it's for sure not null and not empty. TheContractAbbreviator
attributes help a little bit, but all this verbosity goes there and it still looks dirty and stupid. - Code contracts is being developed very slowly, and even though it is open source now, as far as I know the code base is in a bad state.
Is there any advanced alternative to Code Contracts which has fewer flaws?