mirror of
https://github.com/microsoft/BCQuality.git
synced 2026-10-07 23:56:56 +01:00
Follow-up from PR review: nested if is the right answer for two or three dependent conditions, but past that the nesting becomes the problem. AL's case statement is the flat alternative — the control statements documentation states a value set "must be an expression or a range" and that the first matching value set executes, so case true of / case false of accept boolean expressions and stop at the first match. That is the laziness the boolean operators do not provide. Adds case-true-of-for-long-condition-chains.md with good/bad AL companions: case false of for guard chains where every condition must hold, case true of for first-match dispatch. The bad sample shows both failure shapes — a five-level if ladder, and the worse escape of collapsing it into an and chain, which trades nesting for a real defect. Cross-links both articles, and adds the threshold to the short-circuit article's Best Practice so following it does not lead to a deep ladder. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
48 lines
1.7 KiB
AL
48 lines
1.7 KiB
AL
codeunit 50542 "Perf Sample CaseChain Good"
|
|
{
|
|
procedure IsShippableLine(SalesLine: Record "Sales Line"): Boolean
|
|
var
|
|
Item: Record Item;
|
|
begin
|
|
// 'case false of' evaluates the value sets in order and stops at the first
|
|
// match, so each condition is reached only when the previous one passed —
|
|
// and Item.Get is never called for a non-item line.
|
|
case false of
|
|
(SalesLine.Type = SalesLine.Type::Item):
|
|
exit(false);
|
|
(SalesLine."No." <> ''):
|
|
exit(false);
|
|
(SalesLine."Qty. to Ship" > 0):
|
|
exit(false);
|
|
Item.Get(SalesLine."No."):
|
|
exit(false);
|
|
(not Item.Blocked):
|
|
exit(false);
|
|
end;
|
|
exit(true);
|
|
end;
|
|
|
|
procedure FindOpenDocumentType(CustomerNo: Code[20]): Text
|
|
begin
|
|
// 'case true of' stops at the first condition that holds, so the later
|
|
// lookups never run once an earlier one matched.
|
|
case true of
|
|
HasOpenDocument(CustomerNo, "Sales Document Type"::Quote):
|
|
exit('Quote');
|
|
HasOpenDocument(CustomerNo, "Sales Document Type"::Order):
|
|
exit('Order');
|
|
HasOpenDocument(CustomerNo, "Sales Document Type"::Invoice):
|
|
exit('Invoice');
|
|
end;
|
|
exit('None');
|
|
end;
|
|
|
|
local procedure HasOpenDocument(CustomerNo: Code[20]; DocumentType: Enum "Sales Document Type"): Boolean
|
|
var
|
|
SalesHeader: Record "Sales Header";
|
|
begin
|
|
SalesHeader.SetRange("Document Type", DocumentType);
|
|
SalesHeader.SetRange("Sell-to Customer No.", CustomerNo);
|
|
exit(not SalesHeader.IsEmpty());
|
|
end;
|
|
}
|