Posts

Preconditions and postconditions in programming

avatar of @blockchitchat
25
@blockchitchat
·
0 views
·
1 min read

Definition preconditions and postconditions

Preconditions are the conditions that have to be met before a certain piece of code can be executed. Postconditions are the conditions that have to be met after a certain piece of code has been executed.

Preconditions and postconditions are can be said to be types of conditions.

Conditions and arguments are related terms, which will need to be defined for this topic.

A condition is something that needs to happen before an event or action takes place. The result is the outcome of an event, action, or process and in programming results are often directly affected by preconditions and postconditions.

Relationship between preconditions, postconditions and contract based programming

Preconditions are the conditions that must be met before a function can execute. In contract based programming, preconditions are used to ensure that the postconditions will always be satisfied. The precondition is also known as a constraint.

Postconditions are the conditions that must be met after a function has executed successfully. Postconditions are used to express what should happen after execution of the function.

Invariance and Invariant in preconditions and postconditions

Invariance is a property that describes the lack of change in a system. Invariant is a condition that holds true for all possible states of the system.

Invariant is a property that an object always has.

Invariant can be a condition or a postcondition.

Preconditions are the things that need to happen before the invariant can be true.

Postconditions are the things that need to happen after the invariant is true.