Best practices tool

Completed

Best practices help keep X++ code clean and uniform so that future developers can easily read the code and understand its customizations. They also help reduce issues during future upgrades. To enable a best practice check, go to Visual Studio > Extensions > Dynamics 365 > Options > Best Practices and view the best practices for each model. You can also turn off best practices for specific models on the Best Practices page, but correcting a finding is preferable to disabling its rule.

Screenshot of the Visual studio best practices page.

The best practice check runs during the build. In the Error List window in Visual Studio, you can see the best practice issues and the location of each issue. You can also run the best practice check from the command prompt. You can run best practices for all forms, specific elements, all items in a model, and items in all models, and then write the output to a log file. The following examples show how to run best practices against different elements in the command prompt.

  • Run best practice on all forms in a module - xppbp -module:FleetManagement form:*

  • Run best practice on specific elements - xppbp -module:FleetManagement class:MyClass form:MyForm

  • Run best practice on all items in one model in the module - xppbp -module:FleetManagement -model:FleetManagement -all

  • Run best practice on all items in all models in the module - xppbp -module:FleetManagement -all

  • Write the best practice output to log files - xppbp -module:FleetManagement -all -xmllog=Log.xml -log=Log.txt

Use the Customization Analysis Report to investigate warnings

The Customization Analysis Report (CAR) analyzes customization and extension models with a predefined set of best practice rules. Its Excel workbook includes an Issues List with code and metadata findings. Use it to identify the rule and affected element, and then make an appropriate correction in code or metadata. Producing the workbook isn't the same as resolving its findings or proving that the application works correctly.

  1. Prepare the analysis scope. In an approved development environment, identify the installed xppbp.exe tool, local metadata/packages folder, package name, and model name. Choose writable output locations for the diagnostic XML and CAR workbook. Use the actual environment configuration rather than copying a sample drive path.

  2. Generate the report for that model. Use the following Command Prompt template, replacing every angle-bracket placeholder with your environment configuration before running it:

    xppbp.exe -metadata="<local packages folder>" -all -model="<ModelName>" -xmlLog="<XML log path>" -module="<PackageName>" -car="<CAR workbook path.xlsx>"
    

    If your custom model references an ISV model, also supply -PackagesRoot="<packages root folder>". If the required tool or model configuration is unavailable, record that prerequisite rather than treating a missing report as a successful analysis.

  3. Investigate each relevant finding. On Issues List, use the rule name, severity, and element or method information to locate the affected code or metadata. Confirm that the finding belongs to the model you maintain. Apply corrections through supported extension mechanisms; don't treat legacy overlayering language in examples as permission to edit Microsoft application code.

Resolve representative code and metadata findings

Choose a correction that preserves the required business behavior, not just one that removes a diagnostic. Some findings can't or shouldn't be resolved; in those cases, justify the exception. The following examples connect named CAR rules to substantive changes and validation. Apply each pattern to the findings and business behavior in your own model.

Nested data access and joins

For BPCheckNestedLoopInCode and BPCheckSelectwithJoin, inspect nested data-access loops or selects that can be joined. Refactor to a suitable join only when it preserves the required records and processing logic. Validate cases with no related rows, one related row, and several related rows. An inner join and an exists join don't return the same information. If a refactor changes the required business behavior, document the reason for an exception rather than mechanically replacing the loop.

Function calls in select statements

For BPFunctionCallwithSelect, assign the function's return value to a local variable before the select and use that variable in the query. For example, compute the filter value before executing the query instead of invoking the function in the filter expression. Confirm that the value is calculated in the correct context and compare the selected records before and after the change.

Packed parameter state

For BPCheckPackReturnsConnull, inspect the pack method of the affected SysPackable class. Replace connull() with the appropriate packed-state return value, or use super() only when the inherited implementation is applicable. Don't prescribe return super() for every class. Rebuild and test that the required parameter state survives packing and unpacking.

Display method caching

For BPCheckDisplayMethodCached, inspect the affected form control and data method. One correction is to set Cache Data Method to Yes; another is Auto with the SysClientCacheDataMethodAttribute on the corresponding table method. Select the pattern appropriate for the method rather than applying caching indiscriminately. Check the displayed value after the relevant data changes, as well as rerunning the warning check.

Alternate key metadata

For BPCheckAlternateKeyAbsent, identify the appropriate unique index on the affected table and set Alternate Key to Yes. Review the intended key fields and table relationships before changing metadata. Rebuild and exercise the business scenarios that depend on the key; don't select an arbitrary index solely to remove the warning.

Treat a suggested bypass as a separate design decision. CAR includes performance advice involving skip methods and exception-handling patterns. A warning or informational message isn't sufficient justification to bypass required data methods, delete actions, or exception behavior. Review those cases against the relevant language documentation and the application requirement before changing them.

Suppress a diagnostic only for an approved exception

You can suppress best practice diagnostics for each model, but suppression isn't a first choice. Right-click your project and select Edit best practice suppressions to open the suppression XML file. Add a specific diagnostic by using the DiagnosticType, Severity, Path, Moniker, and Justification tags. Copy the diagnostic information from the model's BuildModelResult.xml file, which is created during the build, into the suppression XML file.

The following example shows the diagnostic structure. Replace the justification text with the approved reason and validation evidence for the specific exception before using it.

<Diagnostic>
    <DiagnosticType>BestPractices</DiagnosticType>
    <Severity>Warning</Severity>
    <Path>AxForm/AssetWarehouseTransfer/Design</Path>
    <Moniker>BPErrorFormDesignPatternUnspecified</Moniker>
    <Justification>Replace with the approved reason and validation evidence for this diagnostic.</Justification>
</Diagnostic>

Rebuild, recheck, and justify remaining exceptions

After a code or metadata correction, rebuild the affected model or project with the relevant best practice rules still enabled, and rerun the analysis with the same model scope. Compare the relevant rule and element in the diagnostic output and regenerated CAR. Confirm that the finding disappears because the underlying issue is corrected, not because the rule is disabled or its output is suppressed. Also check for newly introduced diagnostics.

Run the relevant functional or unit tests and record the results separately from the diagnostic result. For a query refactor, compare returned records and business outcomes. For packed state, caching, or key metadata, use the corresponding checks described earlier in this unit. A clean warning list or a generated workbook isn't evidence that these tests pass.

If a finding isn't applicable and the team approves an exception, keep the suppression specific to the diagnostic and provide a meaningful justification. Disabling all checks or copying a speculative justification doesn't resolve a warning. Preserve the reason, reviewer decision, and validation evidence with the change.