Questions tagged [invariants]

In computer science, an invariant is a value that doesn't change.

Taken from Wikipedia:

Although computer programs are typically specified in terms of what they change, it's equally important to know or specify the invariants of a program. This is especially useful when reasoning about the program. The theory of optimizing compilers, the methodology of design by contract, and formal methods for determining program correctness, all pay close attention to invariants in computer programs.

Programmers often make use of assertions in their code to make invariants explicit. Some object oriented programming languages have a special syntax for specifying class invariants.

24 questions
102
votes
7 answers

What are invariants, how can they be used, and have you ever used it in your program?

I'm reading Coders at Work, and in it there's a lot of talk about invariants. As far as I've understood it, an invariant is a condition which holds both before and after an expression. They're, among other things, useful in proving that loop is…
gablin
  • 17,407
2
votes
3 answers

Common mistakes which lead to corrupted invariants

My main source of income is web development and through this I have come to enjoy the wonders of programming as my knowledge of different languages has increased over the years through work and personal play. At some point I reached a decision that…
Dave B.
  • 81