comp.lang.ada
 help / color / mirror / Atom feed
From: jng@sli.com (Mike Gilbert)
Subject: Re: Pre-condition vs. Post-condition
Date: 19 Mar 91 18:17:23 GMT	[thread overview]
Message-ID: <1991Mar19.181723.11186@sli.com> (raw)
In-Reply-To: 2865@sparko.gwu.edu


>>Say we have a function CAPITAL which, given a country's name, returns its
>>capital city.  If the given country does not exist, an exception
>>COUNTRY_ERROR
>>is raised.  Should the given country's presence be listed as a pre-condition
>>for this function, or should its absense (it doesn't exist) and the raising
>>of COUNTRY_ERROR be listed as a post-condition?
>>
>> ...
>
>Hmmm. Interesting question. I have always taught - and thought of - pre-
>conditions as a set of "contract terms" which, if they are met, would
>obligate the function writer to write code that delivers the right results.
> ...
>Failure of a caller to meet a pre-condition violates the contract between
>function writer and function user, in a way that the behavior of the
>function is _unpredictable_, for example the actual parameter is 
>unitialized. If the "garbage" in the parameter _happens_ to constitute
>an in-range value, the function delivers a "correct" answer, coincidentally.
>Otherwise, an exception may be _unexpectedly_ raised (constraint_error, say).
>Since the caller has violated the contract, he gets what he deserves (an
>unexpected propagation of an exception). So the pre-conditions are really
>saying "if you violate these, all bets are off on what this function does."
>
>This argument makes sense to me from a theoretical standpoint.  From a 
> practical standpoint, in describing the interface to a function, how does
>one distinguish between violations that result in a _predictable_ behavior
>and those that do not?
>
>Mike Feldman

I agree with Mike Feldman's description of a function's pre-conditions as a
contract, which, if satisfied, will cause the function to produce the
documented post-conditions, and, if violated, will produce an _unpredictable_
result.

Given this point of view, if a function guarantees to raise a specific
exception under certain pre-defined conditions, then the raising of that
exception must be considered to be part of the contract.  Because of the
guarantee of an exception, this case is very different from a function's
unpredictable behavior if preconditions are violated.

Thus, to answer the original question, the non-existence of a country causing
COUNTRY_ERROR to be raised should be listed as a post-condition.

If COUNTRY_ERROR is a post-condition, then the programmer has several options
for how to handle the possible COUNTRY_ERROR exception in the calling context:

	a If the programmer can guarantee that only valid countries will be
	  passed to CAPITAL, then, by the terms of the post-condition, the 
	  function will never raise COUNTRY_ERROR, and the calling context
	  doesn't have to allow for it.

	b If the programmer can't absolutely guarantee that the country name
	  is valid, but he doesn't expect to pass an invalid one, then the
	  choice of whether the calling context should handle COUNTRY_ERROR
	  depends on how robust the programmer wishes to make the program.

	  (For example, the country name could be generated by another function
	  which has not been formally verified but it believed to generate
	  correct output.)

	c If the programmer has no idea whether the country name is valid, then
	  the calling context had better handle COUNTRY_ERROR.  (For example,
	  the country name could be read as input from a user.)

Contrast these options with an alternate specification of CAPITAL in which the
country name must be valid as a pre-conditon, and thus an invalid country name
violates the function's contract.  With this specification, the function
CAPITAL will be _unpredictable_ if passed an invalid country name.  That is,
CAPITAL _could_ raise COUNTRY_ERROR, it _could_ raise any other exception, it
_could_ always return "SHANGRI-LA", it _could_ go into a loop, etc. etc.

So, if COUNTRY_ERROR is not a possible post-condition, then options "b" and
"c" above are not possible, because the programmer can't count on COUNTRY_ERROR
being raised.

What this all comes down to is that the post-conditions are what a function's
user can _count on_ given input that satisfies the pre-conditions.  Thus, since
the original question stated that COUNTRY_ERROR _is raised_ for a non-existent
country, then that specific exception should be listed as a post-condition.

Alternatively, if the original question had said something like "we've
currently implemented CAPITAL to raise COUNTRY_ERROR if the input country
doesn't exist, but we don't want to promise that it will always do so," then
users of CAPITAL would _not_ be able to count on that behavior, and then the
appropriate specification would be a pre-condition that the country must exist.

BTW, this is a distinction with a great deal of practical importance.  Many
large integrated software systems (e.g., a complex database package modified to
support distributed data by using a commercial network package) develop errors
because the integrator counts on behavior that a software package _currently_
exhibits (esp. under unusual circumstances), but that the developer never
intended to _guarantee_.  Then, when the next release of the software package
comes out, with different behavior under those circumstances, the integrated
system fails.

Mike Gilbert
Software Leverage

  parent reply	other threads:[~1991-03-19 18:17 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-03-15  3:57 Pre-condition vs. Post-condition Chris M. Little
1991-03-15 19:07 ` Michael Feldman
1991-03-17 12:26   ` George C. Harrison, Norfolk State University
1991-03-18 15:04   ` Joe Hollingsworth
1991-03-18 19:51     ` Marlene M. Eckert
1991-03-19 19:07       ` Michael Feldman
1991-03-21  3:01         ` Jim Showalter
1991-03-21 16:34           ` Exception usage design issues (was: Pre-condition vs. Post-condition) John Goodenough
1991-03-21 18:40           ` Pre-condition vs. Post-condition Michael Feldman
1991-03-19 20:38       ` Charles H. Sampson
1991-03-21  3:06         ` Jim Showalter
1991-03-19 21:07       ` Jim Showalter
1991-03-19  7:38     ` Jim Showalter
1991-03-19 14:46       ` Joe Hollingsworth
1991-03-21  2:46         ` Jim Showalter
1991-03-21  5:12         ` Explicit vs implicit checks (was Pre-condition vs. Post-condition) Scott Carter
1991-03-22 15:18       ` Pre-condition vs. Post-condition Brad Balfour
1991-03-19 18:17   ` Mike Gilbert [this message]
  -- strict thread matches above, loose matches on Subject: below --
1991-03-18 15:47 "Norman H. Cohen"
1991-03-24 21:23 stt
1991-03-25 16:00 ` Arthur Evans
1991-03-25 17:05   ` Michael Feldman
1991-03-26  4:31     ` Jim Showalter
1991-03-26 10:21       ` Richard A. O'Keefe
1991-03-26 16:44         ` Michael Feldman
1991-03-26 22:03           ` Richard A. O'Keefe
1991-03-26 23:36             ` Michael Feldman
1991-03-27 21:34             ` Jim Showalter
1991-03-28  2:54               ` Michael Feldman
1991-03-29  3:28                 ` Jim Showalter
1991-03-27  3:12         ` Jim Showalter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox