Questions tagged [invariants]

In computer science, a predicate is called an invariant to a sequence of operations provided that: if the predicate is true before starting the sequence, then it is true at the end of the sequence.

In loops, invariants are data structures referenced within the loop that do not change during any iteration. In design-by-contract, invariants are invariants are properties of a class than must be satisfied at the end of any method call that is invoked from outside of the class itself.

References

218 questions
193
votes
13 answers

What is an invariant?

The word seems to get used in a number of contexts. The best I can figure is that they mean a variable that can't change. Isn't that what constants/finals (darn you Java!) are for?
Dustman
  • 5,035
  • 10
  • 32
  • 40
108
votes
3 answers

What is a class invariant in Java?

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?
Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212
42
votes
3 answers

Simple examples of co and contravariance

Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). All samples I've seen so far was just casting some object into System.Object.
Grant Smith
  • 755
  • 2
  • 6
  • 11
42
votes
1 answer

What are the differences pre condition ,post condition and invariant in computer terminology

I am reading the book Java Concurrency in Practice and getting a little bit confused with these terms: Pre condition Post condition Invariants Can someone please explain me them (with an example, if it's possible)?
Inquisitive
  • 7,476
  • 14
  • 50
  • 61
36
votes
7 answers

Loop invariant of linear search

As seen on Introduction to Algorithms (http://mitpress.mit.edu/algorithms), the exercise states the following: Input: Array A[1..n] and a value v Output: Index i, where A[i] = v or NIL if v does not found in A Write pseudocode for LINEAR-SEARCH, …
Clash
  • 4,896
  • 11
  • 47
  • 67
34
votes
2 answers

What is the difference between Invariants and Validation Rules?

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…
wonderful world
  • 10,969
  • 20
  • 97
  • 194
30
votes
4 answers

Is it possible to program and check invariants in Haskell?

When I write an algorithm I usually write down invariants in comments. For example, one function might return an ordered list, and the other one expect that a list would be ordered. I'm aware that theorem provers exists, but I have no experience…
Andrew
  • 8,330
  • 11
  • 45
  • 78
26
votes
2 answers

What constitutes a valid state for a "moved from" object in C++11?

I've been trying to wrap my head around how move semantics in C++11 are supposed to work, and I'm having a good deal of trouble understanding what conditions a moved-from object needs to satisfy. Looking at the answer here doesn't really resolve my…
Pillsy
  • 9,781
  • 1
  • 43
  • 70
26
votes
3 answers

How free can I be in the code in an object invariant?

I'm trying to demonstrate invariants in Code Contracts, and I thought I'd give an example of a sorted list of strings. It maintains an array internally, with spare space for additions etc - just like List, basically. When it needs to add an item,…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
24
votes
3 answers

Using Facebook's invariant vs if throw

I've been looking at various Node.js projects' source, and I've noticed that some people use invariant. From what I understood, invariant is a tool that lets you put assertions in your code, and raise errors as needed. Question: When would you favor…
rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
22
votes
2 answers

Can I write a type guard that asserts multiple invariants?

Can I write a type guard asserting something about one or multiple sub-objects of an argument? In pseudo-code, it might look like this: class C { a: number?; b: string?; function assertInitialized() : (this.a is number) and (this.b is…
Clément
  • 12,299
  • 15
  • 75
  • 115
20
votes
2 answers

DDD Invariants Business Rules and Validation

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…
17
votes
3 answers

shared_ptr that cannot be null?

Using a std::shared_ptr expresses shared ownership and optionality (with its possibility to be null). I find myself in situations where I want to express shared ownership only in my code, and no optionality. When using a shared_ptr as a function…
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
16
votes
6 answers

checking invariants in C++

Are there any established patterns for checking class invariants in C++? Ideally, the invariants would be automatically checked at the beginning and at the end of each public member function. As far as I know, C with classes provided special before…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
16
votes
5 answers

What is the best way of determining a loop invariant?

When using formal aspects to create some code is there a generic method of determining a loop invariant or will it be completely different depending on the problem?
filinep
  • 529
  • 2
  • 11
  • 17
1
2 3
14 15