Search code examples
invariants

Do you use invariants when you program?


I am taking an intermediate programming course which stresses the use of invariants. I have never used them before and they seem to take up more time to create. Does the software engineering industry stress the use of invariants?


Solution

  • Depends on who you ask - I use invariants simply because it makes life easier. Learning invariants is like learning blind-typing. Each time you use an invariant, you know more about your code. If you insert the invariant as a comment in your loop, it helps the reader A LOT. I would say that using invariants makes creation and maintenance of source code much cheaper, and makes you able to create much more sophisticated algorithms, which are still maintainable.

    And in contrast to OOP, I have never experienced anyone wasting their time by using invariants.