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

199 questions
153
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
  • 4,567
  • 10
  • 30
  • 40
96
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
  • 14,849
  • 42
  • 118
  • 186
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
33
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,485
  • 9
  • 42
  • 63
32
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
  • 6,637
  • 13
  • 46
  • 56
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
  • 7,680
  • 11
  • 42
  • 73
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,461
  • 38
  • 68
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,261,211
  • 792
  • 8,724
  • 8,929
21
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
  • 8,704
  • 14
  • 76
  • 142
20
votes
2 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
  • 10,557
  • 9
  • 55
  • 98
18
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…
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
  • 509
  • 2
  • 9
  • 17
15
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
  • 237,063
  • 85
  • 359
  • 638
14
votes
6 answers

Empty constructors and setters on JPA Entites

I don't like the requirement on have at least one empty constructor and public setters on JPA entities. While I understand the issue on the EntityManager side, this invalidates class invariants. Does anyone have a solution for this (design pattern…
Igor Couto
12
votes
5 answers

Imposing constraints or restrictions on method body, in Java

Context (Edit) Some clarification was on demand, so I'll try to sum up what influences the question. The goal of the project is to provide a certain functionality to programmers, most probably in the form of a library (a JAR with class files, I…
afsantos
  • 5,018
  • 4
  • 29
  • 53
1
2 3
13 14