D365 Business Central : Code Analysis Tool

In D365 business central, Developers can use the code analysis tool to make sure that their code has followed the standard Microsoft AL programming guidelines and the best practice programming conventions.

Here will discuss about static code analysis tool on an AL project from within Visual Studio Code. For this demonstration will create one new sample project ALTEST and see how we can use Code Analysis Tool.

Enabling code analysis:

 1. Press Alt + A, Alt + L to create a new project.

2. Open the Command Palette Ctrl+Shift+P and choose either User Settings or Workspace Settings.

3. Copy the setting al.enableCodeAnalysis to the settings file and set it to true: "al.enableCodeAnalysis": true.

4. Copy the setting al.codeanalyzers to the settings file and then use Ctrl+Space to pick from the available code analyzers. Separate the list of code analyzers with commas. As of now I have added four Analyzers (AppSourceCop, CodeCop, PerTenantExtensionCop, and UICop) in my Settings.Json.

  1. Now the selected analyzers will be run on your project and then add some code to the project. In the following example, be used to demonstrate a violation of the code analysis rule -
    AA0072: The name of GLSetup is not valid. The name of variables and parameters must be suffixed with the type or object name.

AA0215: The file HelloWorld.al has an incorrect name. The valid name is CustomerListExt.PageExt.al.

Adding your own code to the project

In the Visual Studio Code Explorer, open the HelloWorld.al file and replace the existing code with the following:

Viewing the results of the code analysis

You will see the faulty expression underlined and the warning “The name of GLSetup is not valid. The name of variables and parameters must be suffixed with the type or object name.” and “The file HelloWorld.al has an incorrect name. The valid name is CustomerListExt.PageExt.al.” will be displayed. You can also view the list of issues by selecting the View tab of Visual Studio Code and choosing the Problems option.

Using the Ctrl+Shift+B shortcut to build your project will run the code analysis tools on the entire project and the detected issues will be displayed in the Output window of Visual Studio Code

Code analyzers:

A code analyzer is a library that builds on the compiler's functionality to offer enhanced analysis of the syntax and semantics of your code at build time. The AL Language extension for Visual Studio Code contains four analyzers:

  • CodeCop is an analyzer that enforces the official AL Coding Guidelines. For more information about the CodeCop rules.
  • PerTenantExtensionCop is an analyzer that enforces rules that must be respected by extensions meant to be installed for individual tenants. For more information about the PerTenantExtensionCop rules.
  • AppSourceCop is an analyzer that enforces rules that must be respected by extensions meant to be published to Microsoft AppSource. For more information about the AppSourceCop rules.
  • UICop is an analyzer that enforces rules that must be respected by extensions meant to customize the Web Client. For more information about the UserInterfaceCop rules.

Enabling code analysis on large projects

In order to improve performance when running code analysis on large projects, you can switch off running code analysis in the background. To do so, open the Command Palette Ctrl+Shift+P and choose either User Settings or Workspace Settings. Then, specify the setting "al.backgroundCodeAnalysis": false.

Hope this helps!!

Comment List
Anonymous
Related
Recommended