Business Central development requires tools that ensure code quality and integrity. Code analyzers are the best way to ensure that. In this article, you will learn what are they, how to use them in Business Central, and how to bend the rules to fit our specific needs.
- What Are Code Analyzers
- Code Analyzers For Business Central
- Modifying The Rules (RuleSet)
What Are Code Analyzers
A code analyzer is a tool that inspects your code for any potential problems such as syntax errors, potential bugs, or areas where best practices are not followed. These tools can help developers maintain a higher quality of code, adhere to standards, and often help in detecting issues before they arise in a runtime environment.
Code Analyzers For Business Central
The AL Language extension for Microsoft Dynamics 365 Business Central for Visual Studio Code contains four analyzers:
- AppSourceCop: is an analyzer that enforces rules that must be respected by extensions meant to be published to Microsoft AppSource.
- CodeCop: is an analyzer that enforces the official AL coding guidelines.
- PerTenantExtensionCop: is an analyzer that enforces rules that must be respected by extensions meant to be installed for individual tenants.
- UICop: is an analyzer that enforces rules that must be respected by extensions meant to customize the web client.
Basically, they will raise warnings and errors in your code to ensure that rules are met.
You can access and enable them via user settings or workspace settings.
If you want to learn more about workspaces and save time when deploying dependencies follow the article below.
Modifying The Rules (RuleSet)
Code Analyzers allow you to customize their rules using a RuleSet. You’re not required to adhere to every rule strictly.
For this, create a .json object and use the snippet “truleset“.
Pick the ID of the code analyzer which is shown in the console. For this example, it’s “AS0051”.
Set the path of your ruleset file in your settings under the “al.ruleSetPath” property like the following:
Once set correctly, reload your window, and in this case, the error should disappear.
Also, you can set an error to be a warning and a warning to be an error. For example:
If you want to go even further, there is an extension called LinterCop by Stefan Maron with some rules for best coding practices.
For it to work, you have to download the .dll file, place it in a folder, and add the code analyzer to your settings like the following.
Once you reload, you will see that the analyzer ID starts with “LC“.
You can find all the information about LinterCop in the GitHub repository:
Also, you can find all the information about code analyzers in general in Microsoft’s documentation.
That´s all. Hope you find it helpful.