comp.lang.ada
 help / color / mirror / Atom feed
* Short circuit boolean evaluation
@ 2003-11-05 23:28 svaa
  2003-11-06  3:49 ` Steve
  2003-11-07  0:49 ` Gautier Write-only
  0 siblings, 2 replies; 13+ messages in thread
From: svaa @ 2003-11-05 23:28 UTC (permalink / raw)


Hello

I'm a newbie in Ada, I've worked in several languages, last three
years I've worked in Delphi (Pascal).

Shouldn't be a good idea to implement short circuit evaluation for
boolean expresions as default in Ada0x?

Currently Ada uses "and then" and "or else" to implement short
circuit. I think it's a burden. The default behavior of "and" and "or"
should be "and then" and "or else".

The questions are: When is useful short circuit evaluation? when is
useful full evaluation?

Short circuit evaluation:
sometimes it saves you of spliting "if sentence" for previous checks:
if (a/=0) and (b/a>k) ... (A quite common event when you chek null
pointers).
sometimes allows you to decide the order according with you human
knowledge of probability of each expression.

full evaluation:
Let the compiler freedom to optimize the order?

In fact, I usually have a clear idea of the order I want the
evaluation. Perhaps there are a few cases of simple expresions where I
don't mind the order, and I can't guess, at first sight, which has
better performance. Probably I can optimize a boolean expresion better
than a compiler. I know what's the program about!!.

Standard pascal has full evaluation, but most compilers have an option
for short circuit evaluation, and it's the default one, that should
give a clue. In years of programming I'have never missed the full
evaluation, but in some languages that don't use short circuit, I've
missed it. As in aritmetic expresions, I'm aware of left to right
boolean evaluations, I take advantage of it, and I say ****!!,
whenever I have to write "then" after "and" and "else" after "or".

Programs will have the same result if you change the behavior to short
circuit, but you will save a lot of "then" and "else".

Well, I'm missing the case of expresions that call functions. If that
function have side efects, you have to evaluate it. I think this is a
bad coding style, but perhaps it's because I usually work with
short-circuit. In this cases you would lose backward compatibility.
Although it could be solved with a pragma.

Anyhow, I think that even without backward compatibility, the advanges
of short circuit evaluation are worth.

But if someone can't live with of full evaluation you could add the
operators "or always" and "and always", I'm sure they will be less
used than "or else" and "and then" ;-)



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2003-11-10 17:36 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-05 23:28 Short circuit boolean evaluation svaa
2003-11-06  3:49 ` Steve
2003-11-06 19:44   ` svaa
2003-11-06 23:03     ` tmoran
2003-11-07  0:26     ` Mark Lorenzen
2003-11-07 21:27       ` Simon Wright
2003-11-07 22:59         ` Mark Lorenzen
2003-11-07  2:29     ` Wes Groleau
2003-11-07  3:48     ` Steve
2003-11-10 11:18     ` Georg Bauhaus
2003-11-07  0:49 ` Gautier Write-only
2003-11-07  3:03   ` Alexandre E. Kopilovitch
2003-11-10 17:36   ` Warren W. Gay VE3WWG

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