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

218 questions
16
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
14
votes
2 answers

covariant type T occurs in invariant position

I'm moving my first steps in Scala and I would like to make the following code works: trait Gene[+T] { val gene: Array[T] } The error that the compiler gives is: covariant type T occurs in invariant position in type => Array[T] of value gene I…
Vincenzo Maggio
  • 3,787
  • 26
  • 42
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,178
  • 4
  • 30
  • 54
10
votes
3 answers

Hoare Logic Loop Invariant

I'm looking at Hoare Logic and I'm having problems understanding the method of finding the loop invariant. Can someone explain the method used to calculate the loop invariant? And what should a loop invariant should contain to be a "useful" one? I'm…
nunopolonia
  • 14,157
  • 3
  • 26
  • 29
10
votes
2 answers

Can an aggregates invariant include a rule based on information from elsewhere?

In DDD can an aggregates invariant include a rule based on information in a another aggregate? Now I don't think so, however this causes me a problem and I don't know how to solve it. I have an entity called Asset (equipment) which I'm modelling as…
MJM
  • 431
  • 4
  • 14
9
votes
2 answers

Class invariants in Python

Class invariants definitely can be useful in coding, as they can give instant feedback when clear programming error has been detected and also they improve code readability as being explicit about what arguments and return value can be. I'm sure…
Teppo Perä
  • 165
  • 1
  • 7
9
votes
1 answer

Code Contracts: Invariants in abstract class

I have encountered a problem while using Invariants with Code Contracts. I want to define an Invariant within my abstract class but it is simply ignored. The code below shows my interface and the abstract…
Dynamike
  • 93
  • 4
9
votes
2 answers

DDD - Enforce invariants with small aggregate roots

I'm having my first attempt at DDD and I'm running into a problem with aggregate design. My application contains 3 Entities; Graph, Node, Link. Each of these entities has a name property which can be modified by the user (which I believe makes…
tonyj444
  • 383
  • 4
  • 13
8
votes
3 answers

OpenGL deterministic rendering between GPU vendor

I'm currently programming a scientific imaging application using OpenGL. I would like to know if OpenGL rendering (in term of retrieved pixel from FBO) is supposed to be fully deterministic when my code (C++ / OpenGL and simple GLSL) is executed on…
softmllx
  • 480
  • 4
  • 9
8
votes
1 answer

How to prove this invariant?

I aim to prove that the Horner's Rule is correct. To do so, I compare the value currently calculated by Horner with the value of "real" polynominal. So I made this piece of code: package body Poly with SPARK_Mode is function Horner (X : Integer;…
8
votes
2 answers

C# 3.5 Covariance issue?

I've been hearing/reading a lot about covariance issues in C# and I wanted to pose a few questions & scenarios so hopefully I can clear up my confusion on the matter. Throughout these examples, please assume the following is always defined: public…
void.pointer
  • 24,859
  • 31
  • 132
  • 243
8
votes
4 answers

Do invariant assertions fit into C# programming?

In the book coders at work, the author asks "How do you use invariants in your code". Please explain what this question means. I saw class invariants on wiki, but the example is in Java and I am not skilled enough in Java to relate this example to…
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
7
votes
2 answers

Specifying invariants on value constructors

Consider the following data Predicate = Pred Name Arity Arguments type Name = String type Arity = Int type Arguments = [Entity] type Entity = String This would allow the creation of Pred "divides" 2 ["1", "2"] Pred "between" 3 ["2",…
Nickolay Kolev
  • 1,179
  • 2
  • 9
  • 14
7
votes
4 answers

How do you validate an object's internal state?

I'm interested in hearing what technique(s) you're using to validate the internal state of an object during an operation that, from it's own point of view, only can fail because of bad internal state or invariant breach. My primary focus is on C++,…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
7
votes
2 answers

Export only getter or setter from a module

Is there a way for me to only export specific getters xor setters from a module with a lens? For example, let's assume a data structure that has an invariant of being always >= 0, being modified only by incrementing it and being created only with…
Shoe
  • 74,840
  • 36
  • 166
  • 272
1
2
3
14 15