Skip to content

knowledge(performance): Get behind a key-is-set check in and-condition wastes a DB call - #275

Open
Kilian Seizinger (pri-kise) wants to merge 2 commits into
microsoft:mainfrom
pri-kise:get-behind-key-check-performance
Open

Kilian Seizinger (pri-kise) wants to merge 2 commits into
microsoft:mainfrom
pri-kise:get-behind-key-check-performance

Conversation

@pri-kise

@pri-kise Kilian Seizinger (pri-kise) commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Extends boolean-operators-do-not-short-circuit with the case where Record.Get(...) is the dependent operand behind a key-is-set check:

if (EntryNo <> 0) and Rec.Get(EntryNo) then exit(Rec.Field); exit('');

This is not unsafe, but when the guard means "key not set" the Get is an unnecessary call whose result the guard excludes anyway. (Get may be served from the server cache, so it is not always a database round-trip.)

Changes

  • Best Practice: guard clause first (if EntryNo = 0 then exit('')), call Get only after it.
  • Anti Pattern: new detection signal for Rec.Get(Key) behind Key <> 0 or Key <> '' when only a field value or default is returned and the guard intentionally means "key not set".
  • Samples: GetEntryDescription in .bad.al and .good.al, both with SetLoadFields(Description) so the guard is the only difference.

No new article: the rule already covers the pattern, so a separate file would duplicate it.

Validation

Test-ReviewFixtures.ps1 passes.

…n wastes a DB call

Extend the no-short-circuit rule with the case where Rec.Get is the dependent operand behind a Key <> 0 / '' check and only a field value or default is returned: nothing breaks, but every call with an unset key sends a query that cannot find a record. Add GetEntryDescription to the bad/good samples.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new GetEntryDescription example loads a full G/L Entry record to return only Description, so the good example still triggers our partial-record rule. Please select Description before Get in both samples, keeping the guard as the difference, or use a small self-contained table if the samples must support versions before SetLoadFields.

Please also describe this as an unnecessary Get whose result is excluded by the guard, rather than a guaranteed database round-trip. Get can use the server cache, and zero or blank key values can identify real records in AL.

…d wording

Select Description with SetLoadFields before Get in both samples so the guard is the only difference. Describe the case as an unnecessary Get whose result the guard excludes, not a guaranteed database round-trip, and note that 0/blank can be real keys.
@pri-kise

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Both points addressed in the latest commit:

  • Both samples now call SetLoadFields(Description) before Get, so the guard is the only difference and the good sample no longer trips the partial-record rule.
  • The wording now describes an unnecessary Get whose result the guard excludes, notes that Get can be served from the server cache, and states that the guard must intentionally mean "key not set" since 0 or blank can identify real records in AL.

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.

2 participants