Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

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

17
votes
6answers
2k views

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 ...
3
votes
2answers
96 views

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 ...
4
votes
1answer
133 views

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 ...
1
vote
1answer
125 views

Understanding Object-Oriented invariants [closed]

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 ...
2
votes
2answers
528 views

Where to put code related to invariants?

I am developing an small application, just to practice DDD. Afaik. invariants are the umbrella term of validation related to domain. So for example if I want to have only ucfirst names, then that is ...
2
votes
1answer
126 views

Protecting Invariants of Classes

I have a bit of a storm in a teacup at work, and I'm trying to figure out if I'm in the right, in the wrong or maybe a little bit of both. It all started out innocently enough; a developer from ...
12
votes
1answer
2k views

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 ...
7
votes
2answers
365 views

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 ...
0
votes
0answers
86 views

How to ensure this constraint at compile time?

I have an interesting constraint/invariant and I would love to hear people's input on how to maintain it. Big bonus if it can be ensured at compile time... I have the types below, and the following ...
9
votes
3answers
506 views

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 ...
3
votes
1answer
529 views

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 ...
6
votes
2answers
6k views

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 ...
5
votes
1answer
3k views

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 ...
14
votes
5answers
4k views

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 ...
7
votes
4answers
3k views

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....
2
votes
3answers
193 views

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 ...
37
votes
6answers
24k views

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 ...