mirror of
https://github.com/microsoft/BCQuality.git
synced 2026-08-07 18:06:53 +01:00
Add CMFRT coding standards documentation and examples
- Introduced guidelines for "one codeunit one global function" architecture to enforce single responsibility in AL code. - Added best practices and anti-patterns for adding parameters via overloads to maintain backward compatibility. - Documented the importance of never deleting members in AL and always marking them as obsolete. - Established the requirement for OnBefore and OnAfter integration events for global procedures to enhance extensibility. - Defined naming conventions for CMFRT objects, including prefixes and object ID ranges to avoid conflicts. - Implemented patterns for case statements to ensure all cases are handled, including the necessity of an else clause. - Introduced the interface injection pattern to allow pluggable operations in table-level code. - Recommended using Confirm Management for user confirmations to improve testability. - Established a three-permission set pattern for security to ensure proper access control. - Created a review skill for CMFRT AL standards to automate compliance checks against established guidelines.
This commit is contained in:
parent
4119417ce4
commit
c72c0ad685
35 changed files with 866 additions and 0 deletions
16
custom/knowledge/patterns/cmfrt-case-requires-else.bad.al
Normal file
16
custom/knowledge/patterns/cmfrt-case-requires-else.bad.al
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
codeunit 2045760 "CMFRT GD Status Processor"
|
||||
{
|
||||
procedure CMFRTGDProcessByStatus(Status: Enum "CMFRT GD POI Status")
|
||||
begin
|
||||
// No else clause — a future enum value silently falls through
|
||||
// with no error and no action, producing a silent no-op.
|
||||
case Status of
|
||||
Status::Active:
|
||||
CMFRTGDActivatePOI();
|
||||
Status::Inactive:
|
||||
CMFRTGDDeactivatePOI();
|
||||
Status::Pending:
|
||||
CMFRTGDQueuePOIForReview();
|
||||
end;
|
||||
end;
|
||||
}
|
||||
18
custom/knowledge/patterns/cmfrt-case-requires-else.good.al
Normal file
18
custom/knowledge/patterns/cmfrt-case-requires-else.good.al
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
codeunit 2045760 "CMFRT GD Status Processor"
|
||||
{
|
||||
procedure CMFRTGDProcessByStatus(Status: Enum "CMFRT GD POI Status")
|
||||
begin
|
||||
case Status of
|
||||
Status::Active:
|
||||
CMFRTGDActivatePOI();
|
||||
Status::Inactive:
|
||||
CMFRTGDDeactivatePOI();
|
||||
Status::Pending:
|
||||
CMFRTGDQueuePOIForReview();
|
||||
else
|
||||
// New enum values added in the future are caught here
|
||||
// instead of silently doing nothing.
|
||||
Error('Unhandled POI status: %1', Status);
|
||||
end;
|
||||
end;
|
||||
}
|
||||
26
custom/knowledge/patterns/cmfrt-case-requires-else.md
Normal file
26
custom/knowledge/patterns/cmfrt-case-requires-else.md
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
bc-version: [all]
|
||||
domain: patterns
|
||||
keywords: [case, else, defensive-coding, unhandled-case, enum, option, silent-failure]
|
||||
technologies: [al]
|
||||
countries: [w1]
|
||||
application-area: [all]
|
||||
---
|
||||
|
||||
# CMFRT CASE statement requires ELSE clause
|
||||
|
||||
## Description
|
||||
|
||||
Every `case` statement in CMFRT AL code must include an `else` clause. The rule is unconditional: even when every currently known value of the matched expression is enumerated, the `else` clause guards against future values being added to an enum, option field, or integer range. Without `else`, a new value passes through the `case` block silently — no error, no action — and the resulting silent no-op or data corruption is difficult to trace because the `case` appears correct at the time it was written.
|
||||
|
||||
## Best Practice
|
||||
|
||||
Always add an `else` clause to every `case` statement. When no meaningful action applies to unexpected values, the `else` clause should raise an error that identifies the unexpected value, log it, or call a dedicated handler procedure. The key outcome is that the unexpected case is detected at runtime rather than silently ignored.
|
||||
|
||||
See sample: `cmfrt-case-requires-else.good.al`.
|
||||
|
||||
## Anti Pattern
|
||||
|
||||
Writing a `case` statement that enumerates all currently known values and omits `else`. The code appears complete but becomes a silent failure mode the moment a new enum value is added by a future developer or by a base application update, because the added value simply falls through the entire `case` block.
|
||||
|
||||
See sample: `cmfrt-case-requires-else.bad.al`.
|
||||
12
custom/knowledge/patterns/cmfrt-interface-injection.bad.al
Normal file
12
custom/knowledge/patterns/cmfrt-interface-injection.bad.al
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// Monolithic entry-point: the implementation is embedded, there is no interface,
|
||||
// and there is no OnBefore event. Dependent extensions cannot override the
|
||||
// calculation without an AL override hack.
|
||||
table 2045090 "CMFRT MS Measure State"
|
||||
{
|
||||
procedure CalcTotals()
|
||||
begin
|
||||
// All calculation logic is hard-coded here.
|
||||
Rec."Total Amount" := Rec."Line Amount" + Rec."Tax Amount";
|
||||
Rec.Modify();
|
||||
end;
|
||||
}
|
||||
31
custom/knowledge/patterns/cmfrt-interface-injection.good.al
Normal file
31
custom/knowledge/patterns/cmfrt-interface-injection.good.al
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Step 1: Interface with a single procedure.
|
||||
interface "CMFRT MS ICalcTotals"
|
||||
{
|
||||
procedure CMFRTMSCalcTotals(var MeasureState: Record "CMFRT MS Measure State");
|
||||
}
|
||||
|
||||
// Step 2 & 3: Base-table entry point and interface-accepting overload.
|
||||
table 2045090 "CMFRT MS Measure State"
|
||||
{
|
||||
procedure CalcTotals()
|
||||
var
|
||||
DefaultImpl: Codeunit "CMFRT MS CalcTotals Impl";
|
||||
Handled: Boolean;
|
||||
begin
|
||||
OnBeforeDefaultImplCalcTotals(Rec, Handled);
|
||||
if Handled then
|
||||
exit;
|
||||
CalcTotals(DefaultImpl);
|
||||
end;
|
||||
|
||||
procedure CalcTotals(CalcImpl: Interface "CMFRT MS ICalcTotals")
|
||||
begin
|
||||
CalcImpl.CMFRTMSCalcTotals(Rec);
|
||||
end;
|
||||
|
||||
// Step 4: OnBefore event with Handled pattern.
|
||||
[IntegrationEvent(false, false)]
|
||||
local procedure OnBeforeDefaultImplCalcTotals(var Ms: Record "CMFRT MS Measure State"; var Handled: Boolean)
|
||||
begin
|
||||
end;
|
||||
}
|
||||
26
custom/knowledge/patterns/cmfrt-interface-injection.md
Normal file
26
custom/knowledge/patterns/cmfrt-interface-injection.md
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
bc-version: [all]
|
||||
domain: patterns
|
||||
keywords: [interface, injection, extensibility, implementation, handled, on-before, pluggable, override]
|
||||
technologies: [al]
|
||||
countries: [w1]
|
||||
application-area: [all]
|
||||
---
|
||||
|
||||
# CMFRT interface injection pattern
|
||||
|
||||
## Description
|
||||
|
||||
The CMFRT interface injection pattern makes any table-level operation pluggable by dependent extensions using a four-step structure. Step 1: define an AL `interface` with a single procedure matching the operation's signature. Step 2: add a parameterless entry-point procedure on the base table that instantiates the default implementation codeunit, fires an `OnBefore` integration event with `var Handled: Boolean`, exits early if `Handled` is true, and otherwise calls the overloaded form in Step 3. Step 3: add an overloaded procedure on the same table that accepts the interface as a parameter and delegates to it. Step 4: declare the `[IntegrationEvent(false, false)]` `OnBefore` event as a local procedure passing `var Handled: Boolean`. Dependent extensions subscribe to `OnBefore`, set `Handled := true`, and call the overloaded procedure with their own implementation codeunit.
|
||||
|
||||
## Best Practice
|
||||
|
||||
Apply this pattern whenever a calculation or operation might need different behaviour in different customer deployments. The separation between the parameterless entry-point and the interface-accepting overload means a subscriber can inject an alternative implementation without modifying the base table. The default implementation remains the fallback for all extensions that do not subscribe.
|
||||
|
||||
See sample: `cmfrt-interface-injection.good.al`.
|
||||
|
||||
## Anti Pattern
|
||||
|
||||
Placing the entire implementation directly inside the entry-point procedure with no interface and no `OnBefore` event. Monolithic entry points cannot be overridden by a subscriber without an AL override pattern, which is a breaking change for the overriding extension every time the base procedure is updated.
|
||||
|
||||
See sample: `cmfrt-interface-injection.bad.al`.
|
||||
14
custom/knowledge/patterns/cmfrt-use-confirmmanagement.bad.al
Normal file
14
custom/knowledge/patterns/cmfrt-use-confirmmanagement.bad.al
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
codeunit 2045750 "CMFRT GD Deletion Handler"
|
||||
{
|
||||
procedure CMFRTGDDeletePOI(POICode: Code[20])
|
||||
var
|
||||
CMFRTGDPOI: Record "CMFRT GD POI";
|
||||
begin
|
||||
// Confirm built-in blocks test automation — automated tests hang here.
|
||||
if not Confirm('Delete POI %1?', false, POICode) then
|
||||
exit;
|
||||
|
||||
if CMFRTGDPOI.Get(POICode) then
|
||||
CMFRTGDPOI.Delete(true);
|
||||
end;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
codeunit 2045750 "CMFRT GD Deletion Handler"
|
||||
{
|
||||
procedure CMFRTGDDeletePOI(POICode: Code[20])
|
||||
var
|
||||
CMFRTGDPOI: Record "CMFRT GD POI";
|
||||
ConfirmManagement: Codeunit "Confirm Management";
|
||||
DeleteConfirmQst: Label 'Delete POI %1?', Comment = '%1 = POI Code';
|
||||
begin
|
||||
if not ConfirmManagement.GetResponseOrDefault(StrSubstNo(DeleteConfirmQst, POICode), false) then
|
||||
exit;
|
||||
|
||||
if CMFRTGDPOI.Get(POICode) then
|
||||
CMFRTGDPOI.Delete(true);
|
||||
end;
|
||||
}
|
||||
26
custom/knowledge/patterns/cmfrt-use-confirmmanagement.md
Normal file
26
custom/knowledge/patterns/cmfrt-use-confirmmanagement.md
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
bc-version: [all]
|
||||
domain: patterns
|
||||
keywords: [confirm, confirmmanagement, dialog, user-confirmation, test, automation, testability]
|
||||
technologies: [al]
|
||||
countries: [w1]
|
||||
application-area: [all]
|
||||
---
|
||||
|
||||
# CMFRT use ConfirmManagement instead of Confirm
|
||||
|
||||
## Description
|
||||
|
||||
CMFRT AL code must use the `"Confirm Management"` codeunit to prompt the user for confirmation instead of calling the `Confirm` built-in function directly. `"Confirm Management"` wraps `Confirm` and resolves confirmation dialogs without displaying UI during automated test runs, making test behaviour deterministic. The `Confirm` built-in always renders a modal dialog regardless of execution context, which causes automated tests to hang waiting for user input.
|
||||
|
||||
## Best Practice
|
||||
|
||||
Declare `ConfirmManagement: Codeunit "Confirm Management";` as a local variable and call `ConfirmManagement.GetResponseOrDefault(QuestionLbl, true)`. The codeunit suppresses the dialog automatically in test context. Use a `Label` for the question text so it is translatable.
|
||||
|
||||
See sample: `cmfrt-use-confirmmanagement.good.al`.
|
||||
|
||||
## Anti Pattern
|
||||
|
||||
Calling `if Confirm(QuestionText, true) then` directly. Direct `Confirm` calls are not interceptable by the test framework: the modal dialog appears during automated test runs, causing the test to stall indefinitely or fail with a UI-interaction error. Tests that hit a `Confirm` call cannot be run in a CI pipeline without manual intervention.
|
||||
|
||||
See sample: `cmfrt-use-confirmmanagement.bad.al`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue