Exercise 1.2: Running a code review and applying a quick fix
This exercise assumes you have completed Exercise 1.1: Importing the required resources. In the exercise you read a user scenario first. Then you assume the role of the software developer described in the user scenario.
User scenario
A large group of geographically dispersed developers is coding a new software application. It is important that the developers routinely run code reviews to check for problems in their code.
One of the developers wants to run a code review to see how he is doing in general. To review newly written code to assess adherence to best practices in several areas, the developer runs an automated quick code review. This review applies several categories of supplied rules to code. Each category of rules checks the quality of the code in a specific area, such as performance.
When the code review finishes, you see a list of findings. Each finding represents a string of code that does not adhere strictly to an applied rule. One of the findings has a quick fix available for it, so the developer applies the automated solution and corrects the problem right away.
In the first part of the exercise, you perform the following tasks to run a code review:
- Select a code review to run.
- View the rules applied in the code review.
- Choose what code to run the review on.
- Run the code review.
- View the findings of the code review.
- Select a finding to see the following information for it:
- Source code.
- Description, examples, and solutions.
Next, to apply a quick fix to one finding in the code review you perform the following tasks:
- Recognize when a quick fix is available for a finding.
- See a list of changes that the quick fix will make to the code.
- Preview the original and refactored code before you apply the quick fix.
- Apply the quick fix to refactor the code.
- Get a confirmation after the quick fix has been applied.
Exercise
Selecting a code review
To select a quick code review:
- On the toolbar in the Code Review view click the Manage Rules icon,
.

- In the Select Code Review list, click Quick Code Review. The folders of rules for the code review you selected are displayed, as shown in the following screen capture:

- To see one of the rules that will be applied in the code review, expand the J2SE Best Practices folder and then the Null subfolder. The Null folder shows one rule with a problem severity level, as shown in the following screen capture:

As a review, the severity level icons are shown in the following illustration:

- Click OK to choose the Quick Code Review.
Selecting a code base to review
To select the project as the code base to review:
- On the toolbar in the Code Review view click the Review icon (
) > Projects > Review CodeReview_Examples.
Running the code review
Once you select the code base to review, the code review runs. You can track its status by checking the progress bar in the lower-right corner of the view.
Viewing the code review findings
When the code review is finished, the findings are shown in the Code Review view,as shown in the following screen capture:

The Code Review view provides the following information:
- Code review statistics: The line above the findings displays information about the most recent code review: type, scope, number of rules and files included, and number and severity of findings.
- Code review findings: The findings from the code review are listed in the Code Review view, within folders. Each folder name tells you the category of rules applied and the number of findings.
Getting more information on a code review finding
To get more information on a finding in the code review:
- In the Code Review view, scroll to the J2SE Best Practices: Comparison folder. Then expand the folder to show the findings in it, as shown in the following screen capture:

- The first finding begins with EqualsHashCode_Example.java. The rule that was applied is noted after it:
Always override both java.lang.Object.equals() and java.lang.Object.hashCode()
- Double-click the first finding. Details about it appear in two places, as outlined in the following points and screen capture:
- Source code: Displays the code where the finding occurs and highlights the exact location of it.
- Code Review Details view: Describes the finding in more detail and provides examples and solutions to correct it.

Selecting a finding that has a quick fix
You can tell that both findings in the Best Practices: Comparison folder have a quick fix by their icons. As a review, the quick fix icons are shown in the following illustration:

- Right-click the first finding in the list, as shown in the next screen capture.
- The Quick Fix pop-up menu choice varies depending upon the solution. For the finding you selected, the fix is to implement hashCode and equals.

- Click Quick Fix: Implement hashCode and equals.
Applying the quick fix
The quick fix for the finding you selected is to implement hashCode and equals.
To review and apply the quick fix to the finding:
- You see a side-by-side view of the code, as shown in the following screen capture. The original source code is on the left and the refactored source code that would be created by the quick fix is on the right. If you decide to apply the quick fix, it will append the missing lines of code that are highlighted.

- In the Changes to be performed section expand the list to see exactly what the quick fix will change and how, as shown in the following screen capture:

- Review the changes in the list. Then click OK to apply the quick fix to all the selected changes in the list.
- After the quick fix has been applied, you see a checkmark next to the finding you resolved.

Next to the checkmark you see the following information:
- The quick fix you applied
- The line number in the source code where the finding is located
- The rule in the code review that had not been adhered to
You have completed Exercise: Running a code review and applying a quick fix.
Exercise wrap-up
You have performed all the tasks in Exercise: Running a code review and applying a quick fix.
Tasks for running a code review
When you ran this code review, you performed the following tasks:
- Selected a code review to run.
- Viewed rules applied in the code review.
- Chose a body of code to run the review on.
- Ran the code review.
- Viewed the findings of the code review.
- Selected a finding to see the following information for it:
- Source code.
- Description, examples, and solutions.
Tasks for applying a quick fix
When you applied the quick fix, you performed the next set of tasks:
- Recognized when a quick fix is available for a finding.
- Saw a list of changes that the quick fix would make to the code.
- Previewed the original and refactored code.
- Applied the quick fix to refactor the code.
- Got a confirmation that the quick fix had been applied.
Leveraging the power of a code review
By proactively running code reviews, you are able to analyze the findings early. This means you can also address them early, before they lead to the following problems:
- Affect your application's performance, maintenance, or scalability
- Cost your company money, time, and resources
Leveraging the power of a quick fix
By applying a supplied quick fix, you have an automated way to resolve a common finding. Quick fixes help you in the following ways:
- Correct a problem consistently each time
- Free you up to code and spend less time fixing bugs
Finish the tutorial by reviewing the learning objectives in Summary: Running a code review.