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
99
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,377
  • 22
  • 89
  • 138
31
votes
9 answers

"Dead programs tell no lies" in the context of GUI programs

In The Pragmatic Programmer, the authors write: One of the benefits of detecting problems as soon as you can is that you can crash earlier, and crashing is often the best thing you can do. The alternative may be to continue, writing corrupted data…
samfrances
  • 1,065
  • 1
  • 10
  • 15
17
votes
6 answers

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

This is the most popular way (it seems to me) of checking if a value is in an array: for (int x : array) { if (x == value) return true; } return false; However, in a book I’ve read many years ago by, probably, Wirth or Dijkstra,…
16
votes
5 answers

Why are invariants important in Computer Science

I understand 'invariant' in its literal sense. I also recognize them when I type code. But I don't think I understand the importance of this term in the context of computer science. Whenever I read conversations\white papers about language design…
14
votes
1 answer

Object lifetime invariants vs. move semantics

When I learned C++ long ago, it was strongly emphasized to me that part of the point of C++ is that just like loops have "loop-invariants", classes also have invariants associated to the lifetime of the object -- things that should be true for as…
Chris Beck
  • 553
  • 5
  • 14
9
votes
2 answers

C++ class design with invariant

I've been pondering a really basic question about how far to take enforcing a class's invariant. Maybe that's worded badly, so as an example, let's say that I want to write a class which stores a limited palette of colours. The class's constructor…
WalderFrey
  • 313
  • 2
  • 6
9
votes
3 answers

What is the functional programming answer to type-based invariants?

I am aware that the concept of invariants exists across multiple programming paradigms. For example, loop invariants are relevant in OO, functional and procedural programming. However, one very useful kind found in OOP is an invariant of the data…
8
votes
2 answers

Loop Invariants in Python

I have a bit of experience with loop invariants but I'm not really clear on them. I'm trying to learn them through an example in Python. Can someone point one out or help me understand? I've searched both on programmers.SX and on the web but the…
recluze
  • 269
  • 1
  • 5
  • 13
7
votes
4 answers

Why is Invariance, Covariance and Contravariance necessary in typed languages

Ok not really sure if I'm right. I only recently learned that I needed to have contravariant interface to be able to pass that interface as a parameter in C# and this feature was only added in .NET 4.0. So obviously there is some reason you can't do…
Ingó Vals
  • 415
  • 5
  • 11
6
votes
3 answers

DDD: deciding when to lean towards eventual vs transactional consistency

I am reading Vaughn Vernon's series of articles about effective aggregate design. On the subject of deciding between transactional vs eventual consistency, it states the following: Discussing this with Eric Evans revealed a very simple and sound…
6
votes
1 answer

Understanding Object-Oriented invariants

I'm referring to some architectural styles here and under Object Oriented architectural style there are couple of invariants mentioned. Objects are responsible for preserving the integrity (e.g.,some invariant) of the data representation The data…
prime
  • 219
  • 1
  • 4
5
votes
1 answer

How to guarantee invariants / Inner logic in setter methods

According to DDD-principles I use factory-methods to create consistent objects and to ensure that the objects are in the right state. Now I'm in doubt about inner logic of setter methods. I'm tied up with an object, that's similar to the following…
5
votes
1 answer

Loop invariant vs Assertions

I have an exam on Programming Concepts this Saturday and I am struggling to find some information to understand some concepts better. What is the difference between loop invariant and assertion? To me they look the same. Also, what exactly is while…
Vetaxili
4
votes
1 answer

Understanding Pipe and Filters style invariants

I'm developing an application where it read data from different data sources. And then those data should be pre-processed and then go through some chain of steps (Filters ?) where those data will get processed and augmented. Finally those data…
prime
  • 219
  • 1
  • 4
3
votes
3 answers

Does variance make sense in a fully immutable language?

In many OOP programming languages, types can be made co-, contra- or in- variant. Most (if not all of these languages) are able to let variables be mutated in place, i.e. they are not fully immutable languages. I've seen in this SO answer that, in…
ljleb
  • 41
  • 6
1
2