V
V
Victor Bomberow2020-01-07 13:29:01
OOP
Victor Bomberow, 2020-01-07 13:29:01

What is the difference between the terms Invariant and Property?

What is the difference between the concepts of invariant and property (unchanging)?
(Is the question formulated correctly?
What is the meaning of the difference between an invariant and a specification for a class that embodies some abstraction from the subject area?
)
I give examples below and, possibly, erroneous judgments:
1) Suppose there is a graph that can be visually displayed in different ways ( link list, vertex matrix, chain enumeration). The graph invariant is (or more correctly - are?) the number of vertices and edges.
2) Let's say there is a class and some class diagram that describes that instances of this class are persistent. The class, moreover, belongs to a certain inheritance hierarchy, in which the levels of inheritance are counted by natural numbers. Then:

  • An invariant of a class of inheritance level 1 is the non-private interface of the base class.
  • An invariant of a class of inheritance level 2 is a non-private interface of a concrete parent class from inheritance level 1.

3) Let's say there is a container with unsorted elements. There is a class whose constructor takes a container, and there is a method that implements sorting and is called in the constructor. It is then correct to say that the class provides an invariant for the sorted container.
4) "The heap has a non-increasing element ordering property" (equivalent) "A class that implements the heap abstract data structure provides a non-increasing element ordering invariant"
Please clarify the mistakes made, I doubt that I understood the concept correctly.
Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2020-01-07
@majstar_Zubr

From Wikipedia:

An invariant in object-oriented programming is an expression that defines a consistent internal state of an object.

But this applies not only to OOP, but in principle.
From your examples, in my opinion, only 4 are about the invariant.
Based on the wiki definition, an invariant is a boolean expression (not a property) that must always hold for an object. If it is not executed, then the object (class, structure, etc.) is in an inconsistent state and its further use is dangerous.
Usually, invariants are violated inside class methods; when the method exits, the invariant must be restored again, otherwise the method did not work correctly.
A simple example: for a C string, the invariant is not a null pointer at the beginning and a 0 character at the end of the string.
For example, you have the AddString function, which adds another string to an existing string. For simplicity, let's assume that you don't need to reallocate memory. During the execution of AddString, the invariant is violated - the 0 character is replaced by the added characters of the second string. But after the addition, the null character must be restored at the end, after which the invariant will again be true.
There can be several invariants for one object, not all invariants may be important for different methods of an object. If a method violates some invariant in the course of its work, it makes sense to check this invariant with an assert at the beginning and end of the method. This will prevent possible errors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question