comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@home.com>
Subject: Re: short-circuit control forms
Date: Thu, 21 Jun 2001 17:11:36 GMT
Date: 2001-06-21T17:11:36+00:00	[thread overview]
Message-ID: <3B322AC5.2B8DC7B1@home.com> (raw)
In-Reply-To: mailman.993075744.15649.comp.lang.ada@ada.eu.org

"Beard, Frank" wrote:
> -----Original Message-----
> From: Marin David Condic [mailto:marin.condic.auntie.spam@pacemicro.com]
> 
> > Better to have the short-circuit things optional so you can manually
> enable
> > and disable the behavior as needed. Most of the time the extra efficiency
> is
> > likely to be a) too small to sweat and b) not an issue because it isn't a
> > hard realtime system.
> 
> Well, the argument could be made that if your second condition raised an
> exception then your unit testing was insufficient (and yes I know unit
> tests can't always catch everything).

But Marin's point was that if you want to be closer to 100% sure that
this piece of code will not raise an exception or blow the CPU budget,
then you need to make sure that it is _always_ executed. Otherwise, you
have to create the right kind of conditions to test that 0.01% -- which,
might be near impossible without modifying the code (which itself
invalidates the test). If I were flying on that code, then I'd want 
the AND to be used, and not the AND THEN clause. The object is to have
greater confidence in the testing.

> And as a general rule, it's a bad
> idea to write functions with side effects, unless you have some glaring
> necessity to do so.

Nobody disagrees with this, but even functions without side-effects
can create exceptions. That was the point. Also, it is possible for
functions to have different CPU budgets, depending upon the inputs.
This should be tested thoroughly.

> Most, if not all, of the conditional statements that
> I've written fall into the "and then" and "or else" category.

I could see this in a non-critical piece of software. But where
rigorous testing is involved, you will simplify your life by using
the naked AND instead (where possible at least).

> But as a point Marin has already made, I don't even worry about any of
> these cases unless there is some runtime penalty.  In most cases, I
> just use "and" and "or" because the code is cleaner looking.

Coming from a C/C++ background, I tend to err on the side of
short circuiting, but realize that this is not always wise. Even if
you spend most of your life in business like, non-critical systems,
you could end up writing re-usable code that may be applied differently
than you imagined, by someone else.

For this reason, you _should_ probably assume the worst case, as
suggested by Marin, so that you don't inflict trouble on the next
user of your code. The other benefit, as been pointed out, it is
easier to test your code and gain greater confidence from your
tests (greater path coverage).

-- 
Warren W. Gay VE3WWG
http://members.home.net/ve3wwg



  parent reply	other threads:[~2001-06-21 17:11 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-20 22:20 short-circuit control forms Beard, Frank
2001-06-21 14:58 ` Marin David Condic
2001-06-21 17:11 ` Warren W. Gay VE3WWG [this message]
2001-06-21 17:49   ` Marin David Condic
     [not found] <B6A1A9B09E52D31183ED00A0C9E0888C469BC4@nctswashxchg.nctswash.navy.mil>
2001-06-20 21:10 ` Wilhelm Spickermann
  -- strict thread matches above, loose matches on Subject: below --
2001-06-20 19:50 Beard, Frank
2001-06-20 20:35 ` Ted Dennison
2001-06-20 22:32   ` Jeffrey Carter
2001-06-21  1:18     ` Mark Lundquist
2001-06-21 17:05       ` Jeffrey Carter
2001-06-21 14:31     ` Wes Groleau
2001-06-20 23:45   ` Dale Stanbrough
2001-06-20 20:57 ` Marin David Condic
2001-06-21  7:31 ` Keith Thompson
2001-06-20 19:23 James A. Krzyzanowski
2001-06-20 20:15 ` Ted Dennison
2001-06-20 20:47 ` Marin David Condic
2001-06-20 22:23 ` Jeffrey Carter
2001-06-21  0:45   ` Al Christians
2001-06-21 15:06     ` Wes Groleau
2001-06-21 15:46       ` Al Christians
2001-06-21 18:28         ` Wes Groleau
2001-06-21 18:51         ` Marin David Condic
2001-06-22 12:17           ` Marc A. Criley
2001-06-22 14:55             ` Marin David Condic
2001-06-22 20:58   ` Robert Dewar
2001-06-22 21:49     ` Ted Dennison
2001-06-22 22:58     ` Jeffrey Carter
2001-06-23  0:38       ` Larry Kilgallen
2001-06-23 17:34       ` Simon Wright
2001-06-26 15:48       ` Wes Groleau
2001-06-25 17:00     ` Wes Groleau
2001-06-21  0:13 ` Mark Lundquist
2001-06-21  0:55   ` Al Christians
2001-06-21 12:39   ` Larry Kilgallen
2001-06-21 15:02   ` Wes Groleau
replies disabled

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