design-by-contract

Code Contracts: Why are some invariants not considered outside the class?

戏子无情 提交于 2019-12-04 18:46:45
问题 Consider this immutable type: public class Settings { public string Path { get; private set; } [ContractInvariantMethod] private void ObjectInvariants() { Contract.Invariant(Path != null); } public Settings(string path) { Contract.Requires(path != null); Path = path; } } Two things to notice here: There is a contract invariant which ensures the Path property can never be null The constructor checks the path argument value to respect the previous contract invariant At this point, a Setting

IOC Container Handling State Params in Non-Default Constructor

混江龙づ霸主 提交于 2019-12-04 13:23:55
问题 For the purpose of this discussion, there are two kinds of parameters an object constructor might take: state dependency or service dependency. Supplying a service dependency with an IOC container is easy: DI takes over. But in contrast, state dependencies are usually only known to the client. That is, the object requestor. It turns out that having a client supply the state params through an IOC Container is quite painful. I will show several different ways to do this, all of which have big

Compile time checking in Design by Contract?

情到浓时终转凉″ 提交于 2019-12-03 20:25:32
I read that compiler can enforce dbc at compile time.. How does it do it? As far as I know, the most powerful static DbC language so far is Spec# by Microsoft Research . It uses a powerful static analysis tool called Boogie which in turn uses a powerful Theorem Prover / Constraint Solver called Z3 to prove either the fulfillment or violation of contracts at design time. If the Theorem Prover can prove that a contract will always be violated, that's a compile error. If the Theorem Prover can prove that a contract will never be violated, that's an optimization: the contract checks are removed

When to use assert in client & common GWT code

我是研究僧i 提交于 2019-12-03 12:49:04
There are several questions on StackOverflow discussing the question of when one should use an assert statement versus throwing some exception. (Examples here , here , here , here , and here . However, I have come to suspect that the conventional wisdom of assert-versus-throw is based upon the assumption that you are running within a JVM. In the GWT universe, where your Java gets transliterated to JavaScript and runs in the context of a browser, the set of tradeoffs feels different: asserts are always compiled-away when running in a browser, and anything that keeps the size of your JavaScript

Code Contracts: Why are some invariants not considered outside the class?

天大地大妈咪最大 提交于 2019-12-03 11:14:05
Consider this immutable type: public class Settings { public string Path { get; private set; } [ContractInvariantMethod] private void ObjectInvariants() { Contract.Invariant(Path != null); } public Settings(string path) { Contract.Requires(path != null); Path = path; } } Two things to notice here: There is a contract invariant which ensures the Path property can never be null The constructor checks the path argument value to respect the previous contract invariant At this point, a Setting instance can never have a null Path property. Now, look at this type: public class Program { private

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

半城伤御伤魂 提交于 2019-12-03 03:34:28
问题 I was reading about contracts in Thoughts about C++17 by B. Stroustrup and assisted a small presentation talking about them but I am not sure I have understood them really. So I have a some interrogations and if it is possible to illustrate them with some examples : Are contracts just a better replacement of the classic assert() and should they be used together ? What contracts really are put in simple terms for a software dev ? Would contracts have an impact on how we handle exceptions ? If

How much null checking is enough?

江枫思渺然 提交于 2019-12-03 02:04:37
问题 What are some guidelines for when it is not necessary to check for a null? A lot of the inherited code I've been working on as of late has null-checks ad nauseam. Null checks on trivial functions, null checks on API calls that state non-null returns, etc. In some cases, the null-checks are reasonable, but in many places a null is not a reasonable expectation. I've heard a number of arguments ranging from "You can't trust other code" to "ALWAYS program defensively" to "Until the language

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

谁都会走 提交于 2019-12-02 17:03:10
I was reading about contracts in Thoughts about C++17 by B. Stroustrup and assisted a small presentation talking about them but I am not sure I have understood them really. So I have a some interrogations and if it is possible to illustrate them with some examples : Are contracts just a better replacement of the classic assert() and should they be used together ? What contracts really are put in simple terms for a software dev ? Would contracts have an impact on how we handle exceptions ? If yes, how should we use exceptions and contracts ? Would using contracts imply an overhead at execution

Design By Contract, writing test-friendly code, object construction and Dependency Injection putting all together best practices

梦想的初衷 提交于 2019-11-30 10:20:19
I have been trying to figure out the best practices to write test-friendly code, but more specifically the practices related to object construction. In the blue book we discovered that we should enforce invariants when creating objects to avoid the corruption of our entities, value objects, etc. with this thought in mind, Design By Contract seems like the solution to avoid the corruption of our objects, but when we follow this, we could end up writing code like this: class Car { //Constructor public Car(Door door, Engine engine, Wheel wheel) { Contract.Requires(door).IsNotNull("Door is

Design by contracts and constructors

主宰稳场 提交于 2019-11-30 08:19:44
问题 I am implementing my own ArrayList for school purposes, but to spice up things a bit I'm trying to use C# 4.0 Code Contracts. All was fine until I needed to add Contracts to the constructors. Should I add Contract.Ensures() in the empty parameter constructor? public ArrayList(int capacity) { Contract.Requires(capacity > 0); Contract.Ensures(Size == capacity); _array = new T[capacity]; } public ArrayList() : this(32) { Contract.Ensures(Size == 32); } I'd say yes, each method should have a well