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:

  1. Select a code review to run.
  2. View the rules applied in the code review.
  3. Choose what code to run the review on.
  4. Run the code review.
  5. View the findings of the code review.
  6. Select a finding to see the following information for it:

Next, to apply a quick fix to one finding in the code review you perform the following tasks:

  1. Recognize when a quick fix is available for a finding.
  2. See a list of changes that the quick fix will make to the code.
  3. Preview the original and refactored code before you apply the quick fix.
  4. Apply the quick fix to refactor the code.
  5. Get a confirmation after the quick fix has been applied.


Exercise

Selecting a code review

To select a quick code review:

  1. On the toolbar in the Code Review view click the Manage Rules icon, text.
  2. Code Review view

  3. 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:
  4. Preference window, Quick Code Review selected

  5. 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:
  6. Preference window, J2SE Best Practices category, Comparison folder expanded

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

    text

  7. Click OK to choose the Quick Code Review.


Selecting a code base to review

To select the project as the code base to review:



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:

Code Review findings, collapsed

The Code Review view provides the following information:



Getting more information on a code review finding

To get more information on a finding in the code review:

  1. 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:
  2. Code Review findings, J2SE Best Practices category, Comparison folder expanded

  3. The first finding begins with EqualsHashCode_Example.java. The rule that was applied is noted after it:
  4. Always override both java.lang.Object.equals() and java.lang.Object.hashCode()

  5. Double-click the first finding. Details about it appear in two places, as outlined in the following points and screen capture:
  6. Details for selected code review finding.



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:

text

  1. Right-click the first finding in the list, as shown in the next screen capture.
  2. 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.
  3. Quick Fix pop-up menu

  4. 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:

  1. 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.
  2. Quick Fix collapsed list of changes with original and refactored code

  3. 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:
  4. Quick Fix expanded list of changes

  5. Review the changes in the list. Then click OK to apply the quick fix to all the selected changes in the list.
  6. After the quick fix has been applied, you see a checkmark next to the finding you resolved.
  7. Code Review findings with checkmark

    Next to the checkmark you see the following information:

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:

  1. Selected a code review to run.
  2. Viewed rules applied in the code review.
  3. Chose a body of code to run the review on.
  4. Ran the code review.
  5. Viewed the findings of the code review.
  6. Selected a finding to see the following information for it:


Tasks for applying a quick fix

When you applied the quick fix, you performed the next set of tasks:

  1. Recognized when a quick fix is available for a finding.
  2. Saw a list of changes that the quick fix would make to the code.
  3. Previewed the original and refactored code.
  4. Applied the quick fix to refactor the code.
  5. 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:



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:

Finish the tutorial by reviewing the learning objectives in Summary: Running a code review.



Terms of use | Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.