invariants

Do invariant assertions fit into C# programming?

社会主义新天地 提交于 2019-12-04 12:29:54
问题 In the book coders at work, the author asks "How do you use invariants in your code". Please explain what this question means. I saw class invariants on wiki, but the example is in Java and I am not skilled enough in Java to relate this example to C#. .NET 4.0 introduces invariance, covariance, and contravariance and is well explained here. Invariance is so broad. The authors usage of the word seems unit test related. For those that read the book, what does the author mean? Are we talking

Do invariant assertions fit into C# programming?

心不动则不痛 提交于 2019-12-03 08:12:20
In the book coders at work , the author asks "How do you use invariants in your code". Please explain what this question means. I saw class invariants on wiki , but the example is in Java and I am not skilled enough in Java to relate this example to C#. .NET 4.0 introduces invariance, covariance, and contravariance and is well explained here. Invariance is so broad. The authors usage of the word seems unit test related. For those that read the book, what does the author mean? Are we talking about making an assumption and simply testing the validity after the unit test? The word invariant doesn

DDD Invariants Business Rules and Validation

﹥>﹥吖頭↗ 提交于 2019-12-03 03:49:28
问题 I am looking for advice on where to add validation rules for domain entities, and best practices for implementation. I did search and did not find what i was looking for, or i missed it. I would like to know what the recommended way is for validating that properties are not null, in a certain range, or length, etc... I have seen several ways using an IsValid() and other discussions about enforcing in the constructor so the entity is never in an invalid state, or using preprocessing and

What is a class invariant in java?

浪子不回头ぞ 提交于 2019-12-03 01:47:13
问题 I googled the topic, but besides Wikipedia I didn't find any further useful documentation or articles. Can anybody explain to me in simple words what it means or refer me to some nice and easy to understand documentation? 回答1: It doesn't mean anything in particular in reference to java. A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does. For example, class X { final Y y = new Y(); } X has the class invariant that there is a y

What is the difference between Invariants and Validation Rules?

♀尐吖头ヾ 提交于 2019-12-02 23:43:04
I often see the term Invariants in DDD. Here Dino Esposito talks about it. If I look at the .NET library, I see a ValidationAttribute class. Are Invariants and validation rules the same? For example, can I say 50% discount is available only if the order total is more than $250 is an Invariant? Or are they different where Invariants are to protect an object from becoming invalid and validation is to check the validity of the object even after it has changed it's state (it can be in a valid or invalid state)? In the above example, if I use invariants, I check for the invariant before updating

Loop invariant of linear search

折月煮酒 提交于 2019-12-02 18:05:37
As seen on Introduction to Algorithms ( http://mitpress.mit.edu/algorithms ), the exercise states the following: Input: Array A[1...n] Output: i, where A[i]=v or NIL when not found Write a pseudocode for LINEAR-SEARCH, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. (Make sure that your loop invariant fulfills the three necessary properties – initialization, maintenance, termination.) I have no problem creating the algorithm, but what I don't get is how can I decide what's my loop invariant. I think I understood the concept of loop

DDD Invariants Business Rules and Validation

泄露秘密 提交于 2019-12-02 17:15:26
I am looking for advice on where to add validation rules for domain entities, and best practices for implementation. I did search and did not find what i was looking for, or i missed it. I would like to know what the recommended way is for validating that properties are not null, in a certain range, or length, etc... I have seen several ways using an IsValid() and other discussions about enforcing in the constructor so the entity is never in an invalid state, or using preprocessing and postprocessing, and others using FluentValidation api, how invariants impact DRY and SRP. Can someone give me

What is a class invariant in java?

强颜欢笑 提交于 2019-12-02 15:45:31
I googled the topic, but besides Wikipedia I didn't find any further useful documentation or articles. Can anybody explain to me in simple words what it means or refer me to some nice and easy to understand documentation? It doesn't mean anything in particular in reference to java. A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does. For example, class X { final Y y = new Y(); } X has the class invariant that there is a y property and it is never null and it has a value of type Y . class Counter { private int x; public int

How would I write a loop for this invariant?

我与影子孤独终老i 提交于 2019-12-02 02:38:56
问题 These are assertions for an algorithm to find the minimum of an array b[h.k]: Precondition: h <= k < b.length Postcondition: b[x] is the minimum of b[h...k] Is this the correct loop for this invariant? invariant: b[x] is the minimum of b[h...t] int x = t; int t = h; // {inv: b[x] is the minimum of b[h...t]} while (t != k) { t = t+1; if (b[t] < b[x]) { x = t;} } 回答1: You can find the minimum of an array this way (pseudocode): // assume b.length > 0 min = b[0] for i=1 to b.length if b[i] < min

How would I write a loop for this invariant?

点点圈 提交于 2019-12-02 01:52:34
These are assertions for an algorithm to find the minimum of an array b[h.k]: Precondition: h <= k < b.length Postcondition: b[x] is the minimum of b[h...k] Is this the correct loop for this invariant? invariant: b[x] is the minimum of b[h...t] int x = t; int t = h; // {inv: b[x] is the minimum of b[h...t]} while (t != k) { t = t+1; if (b[t] < b[x]) { x = t;} } You can find the minimum of an array this way (pseudocode): // assume b.length > 0 min = b[0] for i=1 to b.length if b[i] < min min = b[i] To restrict it to b[h, ..., k] : min = b[h] for i=h+1 to k if b[i] < min min = b[i] So you