Previous Post : Best Practices in Dynamics AX
The X++ Best Practices tool allows us to create our own set of rules. The classes used to check for rules are named SysBPCheck<ElementKind>. You call the init, check, and dispose methods once for each node in the AOT for the element being compiled.
If we want that our custom BP check should work for all the X++ code like class method, macro or form method, we have to use class SysBPCheckMemberFunction.
In our scenario we will see how to create a custom best practice which will check if nested while select is used in any class methods or in form methods and then generate a BP warning for that.
Steps:
1. Go to AOT > Data Dictionary > Table SysBPParameters
Add CheckNestedWhile field of Enum Type NoYes
2. Go to AOT > Forms > SysBPsetup
In method buildSelectionTree add the below written code at specific node
element.addNode(tmpNode, fieldNum(SysBPParameters, CheckNestedWhile), parameter.CheckNestedWhile);
Here I have added the code under Classes node.
To verify go to Tools > Options and then the Best Practices button in Development section.
3. Now define a Macro for Nested While Select
Go to AOT > Macros > SysBPCheck
#define.BPErrorNestedWhileSelect(10) // Use Join instead of nested While loops.
You can change the error number accordingly. Here I am using 10.
4. Now go to AOT > Classes > SysBPCheckMemberFunction and add a new method checkNestedWhile.
So our pattern is nested while select statement and I have created a Regular Expression to match the pattern.
[Ww]hile\s*[Ss]elect[^\{^\;]*\{\s*[Ww]hile\s*[Ss]elect
(You can write your own logic here)
5. Now in check method of the same class (SysBPCheckMemberFunction), Add this code
if(parameters.CheckNestedWhile)
{
this.checkNestedWhile();
}
Now when you will compile any class which is having nested while select loops you will get Best Practice warning like this.
The X++ Best Practices tool allows us to create our own set of rules. The classes used to check for rules are named SysBPCheck<ElementKind>. You call the init, check, and dispose methods once for each node in the AOT for the element being compiled.
If we want that our custom BP check should work for all the X++ code like class method, macro or form method, we have to use class SysBPCheckMemberFunction.
In our scenario we will see how to create a custom best practice which will check if nested while select is used in any class methods or in form methods and then generate a BP warning for that.
Steps:
1. Go to AOT > Data Dictionary > Table SysBPParameters
Add CheckNestedWhile field of Enum Type NoYes
2. Go to AOT > Forms > SysBPsetup
In method buildSelectionTree add the below written code at specific node
element.addNode(tmpNode, fieldNum(SysBPParameters, CheckNestedWhile), parameter.CheckNestedWhile);
Here I have added the code under Classes node.
To verify go to Tools > Options and then the Best Practices button in Development section.
3. Now define a Macro for Nested While Select
Go to AOT > Macros > SysBPCheck
#define.BPErrorNestedWhileSelect(10) // Use Join instead of nested While loops.
You can change the error number accordingly. Here I am using 10.
4. Now go to AOT > Classes > SysBPCheckMemberFunction and add a new method checkNestedWhile.
So our pattern is nested while select statement and I have created a Regular Expression to match the pattern.
[Ww]hile\s*[Ss]elect[^\{^\;]*\{\s*[Ww]hile\s*[Ss]elect
(You can write your own logic here)
5. Now in check method of the same class (SysBPCheckMemberFunction), Add this code
if(parameters.CheckNestedWhile)
{
this.checkNestedWhile();
}
Now when you will compile any class which is having nested while select loops you will get Best Practice warning like this.
No comments:
Post a Comment