I'm learning how to program but one thing I can't quite get my head around is preconditions and postconditions.
Is an if statement before calling a function considered a precondition, or is there a separate more efficient way of doing this in most languages?
I'm also struggling to find any examples of preconditions that I could understand with my current knowledge of programming if anyone could prove a simple one then I would really appreciate it (any language will be fine)
The precondition is what the method expects in order to do its job properly. A postcondition is a condition that is true after running the method. It is what the method promises to do. Postconditions describe the outcome of running the method, for example what is being returned or the changes to the instance variables.
A pre-condition to a function is a condition that must be true before entering the function—no matter what. A post-condition to a function is a condition that must be true before leaving the function—no matter what. A loop invariant is a condition that must be true at the beginning and end of the body of a loop.
A postcondition associated with a method invocation is a condition that must be true when we return from a method. For example, if a natural logarithm method was called with input X, and the method returns Y, we must have (within the limits of the level of precision being used).
In computer programming, a precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification.
It is well-stated in this c++'s paper
A precondition is a predicate that should hold upon entry into a function. It expresses a function's expectation on its arguments and/or the state of objects that may be used by the function.
A postcondition is a predicate that should hold upon exit from a function. It expresses the conditions that a function should ensure for the return value and/or the state of objects that may be used by the function.
Preconditions and postconditions belong to Contract-based-programming.
In Dlang, Contract-based-programming have good designs. This document offers a sample:
long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert(result ^^ 2 <= x && (result + 1) ^^ 2 > x);
}
do
{
return cast(long)std.math.sqrt(cast(real)x);
}
Preconditions are in in
block, postconditions are in out
block.
9
into the function. live demo
-1
into the function. live demo
[email protected](8): Assertion failure
do
block, like return square
rather than square-root
, then, postconditions will work: live demo
[email protected](13): Assertion failure
For class, Dlang also has good tools, read the document to learn more
BTW, c++ also lists contract design into c++20's draft: https://www.reddit.com/r/cpp/comments/8prqzm/2018_rapperswil_iso_c_committee_trip_report/
Here is the proposal, maybe also helpful to understand them(though, much ugly than D, IMHO)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With