‘Design By Contract’ in C#

C# 4.0 Code Contracts Microsoft has released a library for design by contract in version 4.0 of the .net framework. One of the coolest features of that library is that it also comes with a static analysis tools (similar to FxCop I guess) that leverages the details of the contracts you place on the code. … Read more

What are contracts (as proposed for C++17)?

As far as I’ve read from this document: http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4415.pdf Contracts do what assert has been trying to do in a primitive way for years. They’re both documentation and a run-time assert of how the caller should be expected to call the function and in what state can the caller expect the code to be after … Read more

Why is design-by-contract not so popular compared to test-driven development?

The main problem with DBC is that in the vast majority of cases, either the contract cannot be formally specified (at least not conveniently), or it cannot be checked with current static analysis tool. Until we get past this point for mainstream languages (not Eiffel), DBC will not give the kind of assurance that people … Read more

Contract.Requires usage

You should do the following: Install the Code Contracts add-in as nfechner has noted Go to the project properties, ‘Code Contracts’ folder Check ‘Perform Runtime Contract Checking’ Switch ‘Assembly Mode’ to ‘Standard Contract Requires’ Substitute your Contract.Requires with Contract.Requires<SomeException> (the first one throws System.Diagnostics.ContractException while the second throws the exception you specified which is important … Read more

ReSharper – Possible Null Assignment when using Microsoft.Contracts

Note: as of the current R# 8.0 EAP, this functionality is included. Here’s the solution for the current (i.e. .NET 4.0) version of Code Contracts: Inside …\ExternalAnnotations\mscorlib\Contracts.xml, add the following: <assembly name=”mscorlib”> <member name=”M:System.Diagnostics.Contracts.Contract.Assert(System.Boolean)”> <attribute ctor=”M:JetBrains.Annotations.AssertionMethodAttribute.#ctor”/> <parameter name=”condition”> <attribute ctor=”M:JetBrains.Annotations.AssertionConditionAttribute.#ctor(JetBrains.Annotations.AssertionConditionType)”> <argument>0</argument> </attribute> </parameter> </member> <member name=”M:System.Diagnostics.Contracts.Contract.Assert(System.Boolean, System.String)”> <attribute ctor=”M:JetBrains.Annotations.AssertionMethodAttribute.#ctor”/> <parameter name=”condition”> <attribute ctor=”M:JetBrains.Annotations.AssertionConditionAttribute.#ctor(JetBrains.Annotations.AssertionConditionType)”> <argument>0</argument> </attribute> … Read more

Unit tests – The benefit from unit tests with contract changes?

The first issue you raise is the so-called “fragile test” problem. You make a change to your application, and hundreds of tests break because of that change. When this happens, you have a design problem. Your tests have been designed to be fragile. They have not been sufficiently decoupled from the production code. The solution … Read more

Using Design by Contract in Python

The PEP you found hasn’t yet been accepted, so there isn’t a standard or accepted way of doing this (yet — you could always implement the PEP yourself!). However, there are a few different approaches, as you have found. Probably the most light-weight is just to simply use Python decorators. There’s a set of decorators … Read more