Lesson 2: Assignment Within an IF Statement
We will now demonstrate how to build a rule that flags instances where assignment is used in IF statement condition. While using assignment within if statement condition is legal (so it won't be caught by a compiler), developers that use a=b in IF statement condition usually intended to use a==b, but made a typographical error. In these cases, using assignment would prevent the code from functioning as intended.
Designing the Rule Pattern
Creating the Parent Node
First, open RuleWizard (if it is not already open).
To start creating this rule, open Statements, right-click the if node, then choose Create Rule from the shortcut menu.
After you choose Create Rule, you will see the following parent rule node in the right pane of the GUI:
We now have the basic building block for a rule about IF statements.
Adding Further Qualifications to the Parent Rule Node
To specify that you want to write a rule about something used in IF statement condition, right-click the if rule node and choose Condition from the shortcut menu.
This will create a Condition line and a Condition box that contains the value -none-.
To specify what value you do not want used in if statement condition, right-click the Condition box, then choose Modify from the shortcut menu.
The Modify Node window will open. Because you want to specify that you do not want assignment used in IF statement condition, choose the assignment node (a=b) by opening Expressions> Assignment, clicking the a=b node, then clicking the OK button.
Your rule should now look like this:
At this point, the rule says to look for instances where assignment is used in IF statement condition. This is what we want the rule to look for, so we will stop modifying the rule and begin specifying output.
Specifying an Error Message
We now need to specify what text Jtest should print when this rule is violated. The first step in doing this is right-clicking the parent rule node (here, the if rule node), then choosing Create Output from the shortcut menu.
This action opens the Customize Output window. Enter the message that you want Jtest to deliver when this rule is violated. In this example, you might enter "Avoid assignment in IF statement condition." After you have entered a message, click OK.
Your rule should now look like this:
Your rule now tells Jtest to report the specified error message when a developer uses assignment in IF statement condition. Your rule is now complete. At this point, you may want to customize rule properties such as severity, description, author, etc.; these properties can be specified in the Rule Property panel that is accessible by choosing Rule> Properties. You should at least specify a header and Rule ID so that Jtest knows how to classify your rule and report rule violations.
Saving and Enabling Your Rule
Before you begin composing another rule, or before you exit the program, you will want to save your rule (Jtest only enforces rules that have been saved).
To save your rule, choose Rule> Save or Rule> Save As. This command will invoke a file chooser in which you can specify the rule's filename and path. Be sure to give each rule you save a .rule extension. If you do not use this exact extension, Jtest will not load your rules properly. Also, be sure to save your rules within the default directory (<Jtest installation directory>/jrules). If a rule is not contained in this directory, Jtest will not enforce it.
After you save your rule, exit RuleWizard. To enable your rule:
- Click Global in either of Jtest's UIs.
- In the Global Test Parameters window, go to Static Analysis> Rules> User Defined Rules, then right-click User Defined Rules. A shortcut menu will open.
- Choose Reload Rules from the shortcut menu that opens.
Your rule will then be enabled.
Enforcing Your Rule Automatically
If you look at the Global Test Parameters tree's Static Analysis> Rules> User Defined Rules branch, you will see that Jtest automatically added and enabled the rule you just created.
This rule will now be included in the set of rules Jtest applies to your class or classes during static analysis.
Note: This rule is included with the product; it's under Possible Bugs as "Avoid assignment within an `if' condition."
|