Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do invariant assertions fit into C# programming?

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?

like image 711
P.Brian.Mackey Avatar asked Jan 03 '11 15:01

P.Brian.Mackey


People also ask

What are invariants in C?

An invariant is a condition or relation that is always true. The definition is modified somewhat for concurrent execution: an invariant is a condition or relation that is true when the associated lock is being set. Once the lock is set, the invariant can be false.

What are invariant assertions?

An invariant assertion is a predicate α on S that is preserved by application of the loop body: if it holds before execution of the loop body, then it holds after. • An invariant function is a total function on S that takes the same value before and after execution of the loop body (whenever the loop condition holds).

What is a class invariant C#?

In computer programming, specifically object-oriented programming, a class invariant (or type invariant) is an invariant used for constraining objects of a class. Methods of the class should preserve the invariant. The class invariant constrains the state stored in the object.

Should encapsulation only be used with invariants?

Encapsulation is one of the tools that can be used to guarantee invariant? No. It is necessary but by no means sufficient.


1 Answers

The word invariant doesn't mean more than something doesn't change under certain conditions. There are many different kinds of invariants. For example in physics the speed of light is invariant under lorentz-transform, i.e. it doesn't change if you change to reference frame. In programming there are many kinds of invariants too. There are class invariants which don't change over the lifetime of an object, method invariants which don't change during the life of a function,...

A class invariant is something that's always(at least at publicly observable times) true in an instance of that class.

This is in no way related to co-/contra-variance. Co-/Contra-variance describes which types can be substituted for other types with different (generic) parameters or return types. While you can call something invariant because it doesn't support Co-/Contra-variance this is a completely different kind of invariance than a class or method invariant.

For example some kind of collection might have the following invariants:

  • data != null
  • Size >= 0
  • Capacity >= 0
  • Size <= Capacity

With this class:

class MyCollection<T>
{
  private T[] data;
  private int size;

  public MyCollection()
  {
    data=new T[4];
  }

  public int Size{get{return size;}}
  public int Capacity{get{return data.Length;}}

  [ContractInvariantMethod]
  protected void ClassInvariant()
  {
    Contract.Invariant(data != null);
    Contract.Invariant(Size >= 0);
    Contract.Invariant(Capacity >= 0);
    Contract.Invariant(Size < Capacity);
  }
}

Almost every class has some invariants, but not everybody enforces them. .net 4 adds a nice way to document and assert them using code contracts.

like image 65
CodesInChaos Avatar answered Sep 18 '22 22:09

CodesInChaos