Use Matchers for Preconditions and Postconditions | Java Code Geeks - 2018
Preconditions and Postconditions
Preconditions and postconditions are a little old-fashioned, having been largely replaced by the proliferation of TDD, but I don't think they should be completely ruled out.
Preconditions are a set of assumptions that your function has about the parameters being passed into it.
This is especially useful when you accept "primitive" types, but what you actually want is considerably more restrictive than what those types provide. For example, you're accepting a string that is meant to be the path to a file. Many would argue that you should restrict it to a FilePath
type of some sort that makes sure the path string is of the correct format when the object is created. I would argue that that is the ideal, but not always the pragmatic solution. Another example along those lines is taking in a FilePath
, but the precondition is that the file pointed to already exists and it has the correct file extension. Creating specialty types just to ensure those preconditions will almost definitely be a pain.
Postconditions are similar to preconditions, but are a promise back to the caller that either a certain command was carried out or that the return value will be of a certain shape or format (assuming the preconditions were met).
Read full article from Use Matchers for Preconditions and Postconditions | Java Code Geeks - 2018
No comments:
Post a Comment