Advanced CodeCop Analyzer and Custom Rulesets (Business Central)
Advanced CodeCop Analyzer and Custom Rulesets
We have already talked about Rulesets in Business Central.
This article is about extending it to have multiple ruleset files and using CodeCop Analyzers from the community.
Attention: This only works on a Windows PC. It does not work on a Mac.
Setup
Find your workspace settings file in the .vscode
folder. It is named settings.json
and
make sure you have this:
{
"al.enableCodeAnalysis": true,
"al.codeAnalyzers": [
"${CodeCop}",
"${UICop}",
"${PerTenantExtensionCop}",
"${analyzerFolder}BusinessCentral.LinterCop.dll",
"${analyzerFolder}CompanialCopAnalyzer.dll",
],
"al.backgroundCodeAnalysis": "Project",
"al.ruleSetPath": "./.vscode/Main.ruleset.json",
The Main.ruleset.json
file is used to point to the other ruleset.json files.
It should not contain any rules.
{
"name": "Personal Project ruleset",
"description": "A list of project specific rules",
"includedRuleSets": [
{
"action": "Default",
"path": ".rulesets/AfterConversion.ruleset.json"
},
{
"action": "Default",
"path": ".rulesets/LinterCopIgnore.ruleset.json"
},
{
"action": "Default",
"path": ".rulesets/CompanialCopIgnore.ruleset.json"
}
]
}
The rulesets are placed in a subfolder .rulesets
in the .vscode
folder.
BusinessCentral.LinterCop
Stefan Maroń is the author of the BusinessCentral.LinterCop
After having installed the LinterCop, I am ignoring some of Stefan’s rules.
Add the LinterCopIgnore.ruleset.json
file to the .rulesets
folder with the following
content.
{
"name": "Ignore.BusinessCentral.LinterCop",
"description": "Suppressing some BusinessCentral.LinterCop rules.",
"rules": [
{
"id": "LC0047",
"action": "None",
"justification": "Locked Label must have a suffix Tok."
},
{
"id": "LC0025",
"action": "None",
"justification": "Procedure must be either local, internal or define a documentation comment."
},
{
"id": "LC0010",
"action": "None",
"justification": "Cyclomatic Complexity."
}
]
}
Once the file is in place, it becomes much easier to activate or deactivate rules.
LinterCop Configuration
Place the following file LinterCop.json
in the root of your project.
You can then configure the settings below. Maybe you want the thresholds to be bigger.
{
"cyclomaticComplexityThreshold": 8,
"maintainabilityIndexThreshold": 20,
"enableRule0011ForTableFields": false,
"enableRule0016ForApiObjects": false
}
Custom Rule Cop
CompanialCop is an interesting solution to missing rules.
If you are interested in diving deeper into this, I suggest watching this video on YouTube Automating Dynamics 365 Business Central Code Quality: A Guide to Using Custom Code Analysis for AL by Tine Staric.
You can download the CompanialCopAnalyzer.dll
file from the CompanialCop/releases page and
place it in the AL Extension folder. For Example %userprofile%\.vscode\extensions\ms-dynamics-smb.al-7.4.502459\bin\
.
Add the CompanialCopIgnore.ruleset.json
file to the .rulesets
folder with the following
content.
{
"name": "Ignore.Companial.CodeCop",
"description": "Suppressing some Companial.CodeCop rules.",
"rules": [
{
"id": "CM0002",
"action": "None",
"justification": "Locked Label must have a suffix Tok."
},
{
"id": "CM0012",
"action": "None",
"justification": "Procedure must be either local or internal."
},
{
"id": "CM0017",
"action": "None",
"justification": "Object should not have empty sections. Test functions are allowed to have empty sections."
},
{
"id": "CM0018",
"action": "None",
"justification": "Objects need to have the Access/Extensible properties defined."
},
{
"id": "CM0023",
"action": "None",
"justification": "The identifier must have at least one of the mandatory affixes."
}
]
}
What rules should you ignore?
You should not choose to follow all their rules. Which brings up a good question.
How do I know which rules to follow, and which not to follow?
The answer is consistency.
Is the rule consistent with what Microsoft does?
In general, I recommend to stay as close as possible to the Microsoft way of programming.
Clean AL Code Initiative
- Part 1: Rulesets in Business Central
- Part 2: Namespaces in AL
- Part 3: VS Code Extensions for AL
- Part 4: Automated Tests in AL
- Part 5: Advanced CodeCop Analyzer and Custom Rulesets
- Part 6: How to make a code review
- Part 7: Preconditions and TryFunctions in AL