Code Analysis Tools

Guidelines for Partners

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.  AL code analyzers are used in order for the code to be more readable and orderly, most of the rules, if enabled, return warnings, but there are information messages and errors as well.

 

The AL Language extension for Visual Studio Code contains four analyzers:

  • 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.
  • AppSourceCop is an analyzer that enforces rules that must be respected by extensions meant to be published to Microsoft AppSource.
  • UICop is an analyzer that enforces rules that must be respected by extensions meant to customize the Web Client.

 

CodeCop Analyzer

 

CodeCop is an analyzer that enforces the official AL Coding Guidelines. There are a lot of rules which improve code readability and general AL solution design. Enabled CodeCop rules return info, warning end error messages.

Example of information rule:

CodeCop rule

If rule AA0072 is enabled, it returns information messages about variable names that are compatible with the requested naming convention. Here PrevCust should be corrected, PrevCustomer and CustomerPrev are already correct:

CodeCop Analyzer

 

Examples of CodeCop rules which returns warnings:

CodeCop rules

CodeCop rules

CannotBeSmallerThan label with two warnings (rule AA0074 and rule AA0470) and CannotBeSmallerThanErr label which is correct one as has Err suffix and comment part:

CannotBeSmallerThan

 

PerTenantExtensionCop Analyzer

 

PerTenantExtensionCop is an analyzer that enforces rules that must be respected by extensions meant to be installed for individual tenants. PerTenantExtensionCop analyzer returns mostly errors and a few warnings. The majority of these rules are for better object and package validation.

PTE0008 rule for mandatory  field’s ApplicationArea property:

3. PerTenantExtensionCop Analyzer

 

AppSourceCop Analyzer

 

AppSourceCop is an analyzer that enforces rules that must be respected by extensions meant to be published to Microsoft AppSource. AppSourceCop returns the majority of errors but there are some warning and info messages. Rules are about extensibility, upgrade, design, and some configuration.

AS0062 rule for mandatory  field’s ApplicationArea property (this returns a similar error as PerTenantExtensionCop rule PTE0008 – some rules are important for more than one analyzer):

AppSourceCop Analyzer

 

UICop Analyzer

 

UICop is an analyzer that enforces rules that must be respected by extensions meant to customize the Web Client. Returns mostly warnings, bet there are some info and error messages.

Example of information message about missing Image property for page action (rule AW0005):

UICop Analyzer

 

Enabling Code Analyzers

 

In order to enable code analyzers press File -> Preferences -> Settings. Choose Workspace, write EnableCodeAnalysis in search and select Enable Code Analysis option:

6. Enabling Code Analyzers

 

Settings file should be created for you:

EnableCodeAnalysis

 

Add code analyzers you want to use and other settings like “al.backgroundCodeAnalysis” or “al.enableCodeActions”.

al.enableCodeActions

 

“al.backgroundCodeAnalysis” setting enables code analysis in real-time when coding. 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.

“al.enableCodeActions” setting enables suggestions (quick fixes) to fix something in the solution:

quick fixes

 

Modifying Code Analyzer Rules

 

There might be some rules which are not relevant for one solution but are extremely important for the other. That is why it is possible to create a unique ruleset for each project. For example, CodeCop rule AA0008 is warning about function calls with recommended parenthesis used. Let’s say that this rule is not important for us and we do not want to have a warning about it. Also, let’s say that rule AA0137 which is warning about unused variables in solution is extremely important for us and warning is not enough, we want it to be an error. We can create a new file ThisProject.ruleset.json

Modifying Code Analyzer Rules

 

We can modify setting.json file to enable ThisProject.ruleset.json file:

modify .json file

 

Setting.json and ThisProject.ruleset.json files:

settings .jason files

 

Best practices

 

Some rules of AL Code analyzers could be very useful to enable. Rules can help to check the solution features and quality.

Probably the most used rules of AppSourceCop are the ones related to affixes. If suffix or prefix should be added on some project, the easy way to check the solution is to enable AppSouceCop analyzer and filter on rules AS0011 and AS0079. This way all the places where the affix is missing can be identified easily.

AppSouceCop analyzer and filter

Rules

CodeCop has a rule (AA0137) that identifies unused variables – that is a great help if you want to clean up your solution.

unused variables

Another useful CodeCop rule helps to identify risky assignments in your code, this is rule AA0139.

identify risky assignments

Also, there are rules about identical names for local and global variables (CodeCop rule AA0198), CalcFields function usage on a field that is not a FlowField or a field of type Blob (CodeCop rule AA0211), ToolTip property of fields must be filled (CodeCop rule AA0220), not displaying request page of XMLPorts (UICop rule AW0001).

 


 

There are four code analyzers CodeCop, UICop, AppSourceCop, PerTenantExtensionCop which could help to make your solution more readable and clean. Also, there is a possibility to modify some rules, make them more or less important, and in such a way check the quality and how requirements are met in your solution.

 

Useful links: