mirror of
https://github.com/microsoft/BCQuality.git
synced 2026-10-07 15:46:55 +01:00
Merge pull request #51 from Curabis/rule/binary-choice-must-be-boolean
[BCQuality] Binary yes/no choices must be Boolean, not Option/Integer
This commit is contained in:
commit
d7001e0e23
1 changed files with 59 additions and 0 deletions
59
custom/knowledge/style/binary-choice-must-be-boolean.md
Normal file
59
custom/knowledge/style/binary-choice-must-be-boolean.md
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
---
|
||||||
|
bc-version: [all]
|
||||||
|
domain: style
|
||||||
|
keywords: [boolean, option, yes-no, magic-number, variable-typing, field-typing]
|
||||||
|
technologies: [al]
|
||||||
|
countries: [w1]
|
||||||
|
application-area: [all]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Binary yes/no choices must be typed as Boolean, not Option or Integer
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
When a field or variable represents exactly two states — yes/no, on/off,
|
||||||
|
active/inactive, blocked/not blocked — it must be typed `Boolean`. Modeling
|
||||||
|
that same two-state choice as an `Option`/`Enum` with two members, or as an
|
||||||
|
`Integer` with two magic-number values (0/1), adds a layer of indirection a
|
||||||
|
reader has to resolve before understanding the code, and it invites a
|
||||||
|
three-way branch (`if X = 0 then ... else if X = 1 then ...`) where a simple
|
||||||
|
`if X then ...` would do.
|
||||||
|
|
||||||
|
This is distinct from a genuine multi-value choice — see
|
||||||
|
[[fixed-choice-set-must-use-enum-not-integer]] — where more than two named
|
||||||
|
states legitimately call for `Enum`. The line is the state count: exactly
|
||||||
|
two mutually exclusive states is a Boolean question, not an enumeration.
|
||||||
|
|
||||||
|
## Best Practice
|
||||||
|
|
||||||
|
```al
|
||||||
|
field(50; Blocked; Boolean)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
var
|
||||||
|
IsOverdue: Boolean;
|
||||||
|
...
|
||||||
|
IsOverdue := DueDate < Today;
|
||||||
|
if IsOverdue then
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
## Anti Pattern
|
||||||
|
|
||||||
|
```al
|
||||||
|
field(50; Status; Option)
|
||||||
|
{
|
||||||
|
OptionMembers = Active,Blocked;
|
||||||
|
}
|
||||||
|
|
||||||
|
var
|
||||||
|
OverdueFlag: Integer; // 0 = No, 1 = Yes
|
||||||
|
...
|
||||||
|
if OverdueFlag = 1 then
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
An `Option`/`Integer` standing in for a true yes/no forces every caller to
|
||||||
|
remember which value means what, and it leaves room for a third, meaningless
|
||||||
|
value that a `Boolean` cannot represent.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue