From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: ** X-Spam-Status: No, score=2.7 required=5.0 tests=BAYES_00,INVALID_DATE, PDS_OTHER_BAD_TLD,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!tut.cis.ohio-state.edu!python.cis.ohio-state.edu!holly From: holly@python.cis.ohio-state.edu (Joe Hollingsworth) Newsgroups: comp.lang.ada Subject: Re: Pre-condition vs. Post-condition Keywords: pre-condition, post-condition, exception Message-ID: <98063@tut.cis.ohio-state.edu> Date: 19 Mar 91 14:46:04 GMT References: <344@platypus.uofs.edu> <2865@sparko.gwu.edu> <97779@tut.cis.ohio-state.edu> Sender: news@tut.cis.ohio-state.edu Reply-To: Joe Hollingsworth Organization: The Ohio State University Dept of Computer & Information Science List-Id: In article jls@rutabaga.Rational.COM (Jim Showalter) writes: >>procedure pop(s: stack) >>begin >> if(not empty(s)) then >> -- pop the stack >> else >> raise underflow end if; >>end pop; > >>procedure pop(s: stack) >>begin >> stack.top := stack.top - 1; >> exception >> when Constraint_Error => raise underflow >>end pop; > >Note that in the second case the procedure is faster, since it doesn't >have to do the check first. I figured I'd see this argument, but I don't buy it. 1) Refering back to Kernighan & Plauger's "The Elements of Programming Style," I'll quote another rule: "Write clearly - don't sacrifice clarity for 'efficiency'." 2) Ok, so you think that the second version of Pop is faster because it is not doing the test. Well there may not be an explicit test, but there sure has to be an implicit one. The compiler has to generate code to test to see if a constraint error needs to be raised, i.e. there is run time checking going on here. And you sure can't use the SUPRESS pragma to get rid of it. There is testing going on here, either you see it explicitly in the code, or it gets done for you by run time checks. >Not only is it faster, it is safer, since >without using tasks you cannot guarantee that between the time you >checked and the time you popped it hadn't been popped elsewhere. The examples given were for the sequential world, there was no mention in the original posting or my follow up about tasking. >For both of these reasons, I'd say the second version is far better than >the first version, and that the original poster's thesis that exceptions >should be used rarely if ever has been contradicted by the very examples >provided to support his/her case! I don't think the follow up poster has really come up a with good case. His/her case is mainly based on efficiency concerns, which obviously doesn't hold since run time checks have to be performed. And, the follow up poster's far better version is still not as clear as the first version. >P.S. You need an "end if" in the first example. Thank you, I've edited the above example to include "end if." Joe holly@cis.ohio-state.edu