B
B
BonBon Slick2021-08-23 12:25:46
Software design
BonBon Slick, 2021-08-23 12:25:46

Why does an invariant violation indicate a vendor error?

Who knows the terminology of the client, supplier and invariant in contract programming, the question is for you.

Argument your opinion on the account of who is guilty of the violation and why do you think so?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexHell, 2021-09-04
@BonBonSlick

I take the first link to Wikipedia https://ru.wikipedia.org/wiki/%D0%9A%D0%BE%D0%BD%D...
about the quote invariant

The main idea of ​​contract programming is a model of interaction between the elements of a software system, based on the idea of ​​mutual obligations and benefits ... obligations to fulfill specific properties - invariants that must be fulfilled when a message is received by the supplier, as well as when the method exits.

if in your own words, and by example, there is a class
EmailSender
{
BlockingQueue<MessageToSend> queue;

public void addToQueue(String text, String targetEmail)
{
validateText(text); // реализация опущена - предусловие входного параметра
validateEmail(targetEmail);// реализация опущена - предусловие входного параметра
lock(queue)
{
  queue.add(new MessageToSend(text, targetEmail));
}
// инвариант - в очередь добавлено сообщение (если предусловия пройдены)
}

// периодическая отправка по таймеру (вызов опущен)
private void sendByTick()
{
  lock(queue)
  {
    // отправить все из queue
    queue.clear();
  }
    // инвариант 1 - после вызова queue стал пуст
    // инвариант 2 - если во время вызова sendByTick были попытки добавить с помощью addToQueue то они не потеряются (за счет lock)
}

}
it is obvious that if the invariants (about adding to the queue, about multithreading) are violated by the implementation (for example, the lock is forgotten, and messages are lost while they are being sent due to the clear queue), then the implementer is to blame, and who else (as invariants inside the class - this can be seen from the code, or better - described in the documentation for the class)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question