Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Guava have postconditions? What can I use instead?

Tags:

So, Guava has simple yet useful Preconditions to check method arguments. But I guess it would be reasonable to have a "Postconditions" class too. Or is it just because java provides assertions?

Since a class like this doesn't exist, what is the "best" (practice) alternative way to check postonditions before a mathod returns?

like image 496
Flo Avatar asked Oct 20 '12 20:10

Flo


People also ask

Do All methods have Postconditions?

It may be that a client does not need to do or know anything at all to successfully call your method. In those cases, it's ok to not mention preconditions at all. However, every method should have a postcondition.

Where are pre and postconditions stated?

It must be true prior to entering the segment in order for it to work correctly. Preconditions are often placed either before loops or at the entry point of functions and procedures. A postcondition is a statement placed after the end of the segment. It should be true when the execution of the segment is complete.

What are preconditions and postconditions?

• Precondition – a condition that must be true before a function (or code segment) executes, in order. for the code to work correctly. • Postcondition - a condition that will be true after a functions returns or at the end of a code segment.

When would you use a postcondition?

In computer programming, a postcondition is a condition or predicate that must always be true just after the execution of some section of code or after an operation in a formal specification. Postconditions are sometimes tested using assertions within the code itself.


2 Answers

Testing post conditions would be superfluous . The way we test post-conditions in java is by unit testing.

With unit testing, we make sure that for a given input we get predictable output. With Preconditions, we can verify that we have valid input, and hence the output is already guaranteed by the tests.

like image 68
Johan Sjöberg Avatar answered Nov 06 '22 14:11

Johan Sjöberg


I would use the Java assert keyword within the method itself to encode the postcondition.

Unit Test or Postcondition?

Unit tests and postconditions serve different purposes.

An assertion in a unit test provides a check on the result of a method for one input vector. It is an oracle specifying the expected outcome for one specific case.

An assert in the method itself verifies that for any input the postcondition holds. It is an oracle specifying (properties of) the expected outcome for all possible cases. Such a postcondition-as-oracle combines well with automated testing techniques in which it is easy to generate inputs, but hard to generate the expected value for each input.

Guava Postconditions?

As to why Guava has a Precondition class, but no Postcondition class, here's my understanding.

Guava Preconditions effectively provides a number of shorthands for common situations in which you'd want to throw a particular kind of exception (Illegal argument, null pointer, index out of bounds, illegal state) based on the method's inputs or the object's state.

For postconditions there are fewer such common cases. Hence there is less need to provide a shorthand throwing specific kinds of exceptions. A failing postcondition is like a HTTP 500 "Internal Server Error" -- all we know something went wrong executing our method.

(Note that Guava's notion of precondition is quite different from that of pure design-by-contract, in which there are no guarantees at all if a precondition is not met -- not even that a reasonable exception is thrown. Guava's Preconditions class provides useful capabilities to make a public API more defensive).

like image 27
avandeursen Avatar answered Nov 06 '22 15:11

avandeursen