Skip to content

Add community knowledge: AL boolean operators do not short-circuit - #136

Open
António Silva (aacnsilva) wants to merge 1 commit into
microsoft:mainfrom
aacnsilva:feat/boolean-operators-no-short-circuit
Open

Add community knowledge: AL boolean operators do not short-circuit#136
António Silva (aacnsilva) wants to merge 1 commit into
microsoft:mainfrom
aacnsilva:feat/boolean-operators-no-short-circuit

Conversation

@aacnsilva

@aacnsilva António Silva (aacnsilva) commented Aug 24, 2026

Copy link
Copy Markdown

What

Adds community/knowledge/performance/boolean-operators-do-not-short-circuit.md, with .good.al / .bad.al companions.

AL gives no short-circuit (lazy) evaluation guarantee for and, or, and xor. Code must therefore not use the leftmost operand as a guard for a later one, and must not expect a costly operand to be skipped once the result is decided. The fix is a nested if — guard outermost, dependent or expensive condition inner.

Why this passes the admission test

An LLM trained mostly on C#, JavaScript, or SQL carries short-circuiting in as a default assumption, and BC has no compiler diagnostic that contradicts it. That produces two failure shapes the file prevents:

  • A guard that does not guard. (Index >= 1) and (Index <= ArrayLen(Thresholds)) and (Amount > Thresholds[Index]) still evaluates the subscript. Likewise Customer.Get(No) and (Customer.Blocked <> ...) reads Blocked from a record that was never loaded — a silently wrong result rather than an error.
  • Cost paid on a path that was already decided — a database call or validation procedure invoked as the right operand of an and whose left operand is already false.

Note on the documentation

Neither AL operators nor Boolean operators mentions short-circuit or lazy evaluation in either direction. The article is therefore worded as no guarantee is given, so do not rely on one, rather than asserting a documented evaluation order. That yields the same guidance without claiming platform behaviour the docs do not state — and is part of why this went to /community/ rather than /microsoft/.

🤖 Generated with Claude Code

AL gives no short-circuit (lazy) evaluation guarantee for and/or/xor —
neither the AL operators nor the boolean operators documentation defines a
lazy evaluation order. LLMs trained on C#, JavaScript, or SQL assume the
left operand guards the right, which produces conditions where a guard
does not protect an unsafe subscript or a field read after a failed Get,
and where an expensive operand is paid on every path.

Adds community/knowledge/performance/boolean-operators-do-not-short-circuit.md
with good/bad AL companions. The guidance prefers nested if when one operand
depends on another, while keeping and/or legitimate for operands that are
independently safe and cheap, so a reviewer does not flag harmless bound
checks. This is the first article in a community performance domain; the
Microsoft performance review leaf skill already sources candidates by domain
across every enabled layer, so no skill change is needed to reach it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@aacnsilva

Copy link
Copy Markdown
Author

Some people prefer to use the in operator to short-circuit/lazy evaluate but AL developers are most used to seeing if statements and the in operator might confuse more than help, at least in the beginning, so I added nested if conditions instead in the knowledge file and good pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant