.Net Code Contracts - Alternatives

I recently started using Code Contracts for .net . The idea of ​​code contracts is great in itself, but the implementation is very unpleasant.

The main reasons why I do not like it:

  • In my procedures, I can only use methods such as Contract.Require(). ContractAbbreviatorhave so many limitations (for example, I cannot put them in a separate assembly, and I cannot use parameters), which makes them less usable. There are no attributes and extension methods, so my code becomes very verbose. For example, if I just want to check that my return value of a type is Dictionary<string, string>not null, I need to add a monster such as Contract.Ensure(Contract.Result<Dictionary<string, string>> != null). It is not even readable.
  • The static analyzer makes so many false alarms that I spend more time closing it than fixing current issues.
  • It is very slow. Despite the fact that it has a cache, it takes several minutes to analyze even a small project, and this makes it useless for incremental fixing. It takes too long.
  • There ccrewriterare errors in it - it cannot chew half of the assemblies and cannot survive .net 4.0 assemblies in the .net 4.5 runtime.
  • There is dualism runtime / static checker. When I just started, I thought it was as simple as Debug.Assert- you just add them wherever you feel you need. But it turns out that I need to prove everything that is statically checked, but it is checked, but the check is sometimes stupid and cannot resolve many obvious code constructs (for example, while). And there is no tool to simply tell the analyzer: "I know what I'm doing, just ignore this breach of contract."
  • . , , string.NotNullOrEmpty, string != null. , , , , . ContractAbbreviator , , - .
  • , , , .

- Code Contracts, ?

+4
1

, , , . , (~ 1000 ) , VS.

  • . () , , , ContractClass ContractClassFor. ContractClassFor-classes, .

  • , , , .

  • . , , . , , .

  • MSBuild, , .

    msbuild myproject.sln /p:CodeContractsExtraAnalysisOptions="-show progress -stats=!! -stats slowMethods"

  • , - , Contract.Assume(), , . .

    Contract.Assume(mystring != null && mystring != "") , , Contract.Assume(!string.IsNullOrEmpty(mystring))

  • Debug.Assert, , . , . , , , .

+13

Source: https://habr.com/ru/post/1620888/


All Articles