-1

In Javascript, if i do the following command :

if (cond1 && cond2) {
}

The parser will stop if cond1 is false, no need to check cond2. This is good for performance.

In the same manner :

if (cond1 || cond2) {
}

If cond1 is true, there is no need to check cond2.

Is there something similar on newer Delphi versions ? I'm on the 10.4

Thanx

delphirules
  • 6,443
  • 17
  • 59
  • 108
  • Nitpick regarding the title of this question: in my world, a comparison operator is an operator like `=`, `<>`, `<`, `>`, `<=`, or `>=`. `and`, `or`, `not` etc. are "boolean operators". – Andreas Rejbrand Jan 10 '21 at 14:01
  • This is available with **every** Delphi version that ever existed. Have you even tested this yourself before asking? – AmigoJack Jan 10 '21 at 14:11
  • Although this Q is fine and allowed me to give an A that focuses on a part I find important and often overlooked (that this is more about code conciseness and readability than performance), you could also have found this out by looking at the [docs](http://docwiki.embarcadero.com/RADStudio/Sydney/en/Expressions_(Delphi)#Complete_Versus_Short-Circuit_Boolean_Evaluation) about boolean operators. That's even better than testing it! – Andreas Rejbrand Jan 10 '21 at 14:16
  • Why didn't you read the documentation? – David Heffernan Jan 10 '21 at 14:35

1 Answers1

6

Yes, the Delphi compiler supports boolean short-circuit evaluation. It can be enabled or disabled using a compiler directive, but it is enabled by default, and often Delphi code assumes it to be enabled.

This is very often used not only to increase performance, but to write code more succinctly.

For instance, I often write things like

if Assigned(X) and X.Enabled then
  X.DoSomething;

or

if (delta > 0) and ((b - a)/delta > 1000) then
  raise Exception.Create('Too many steps.');

or

if TryStrToInt(s, i) and InRange(i, Low(arr), High(arr)) and (arr[i] = 123) then
  DoSomething

or

i := 1;
while (i <= s.Length) and IsWhitespace(s[i]) do
begin
  // ...
  Inc(i);
end;

or

ValidInput := TryStrToInt(s, i) and (i > 18) and (100 / i > 2)

In all these examples, I rely on the evaluation to stop "prematurely". Otherwise, I'd run into access violations, division by zero errors, random behaviour, etc.

In fact, every day I write code that assumes that boolean short-circuit evaluation is on!

It's idiomatic Delphi.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 1
    can I recommend that you search for duplicates when you encounter a question that you expect already to have been answered. – David Heffernan Jan 10 '21 at 14:42
  • 1
    @DavidHeffernan: You are right of course that this has been answered before in various forms, albeit sometimes indirectly. Although neither of the two Qs you mention asks the exact same Q or is of particularly high quality, they definitely do answer the Q. However, if you just want to find the answer, you very much need not SO at all -- you just need to read the docs. An SO answer can give a bit more context. I imagine it doesn't hurt if there are a duplicate or two with different wordings for people who search the WWW. Or do you suggest I remove this A so that the Q can be removed? – Andreas Rejbrand Jan 10 '21 at 14:51
  • 3
    Don't remove anything. Dupehammer it. Which I did. – David Heffernan Jan 10 '21 at 15:58