Exercise 1.4: Resolving a structural problem

This exercise assumes you have completed Exercise 1.1: Importing the required resources. In Exercise 1.4 you read a user scenario first. Then you assume the role of the software architect described in the user scenario and use the project you imported in Exercise 1.1 to complete the exercise.



User scenario

To check code specifically for cyclic dependencies, the architect runs a structural analysis code review that looks only for such dependencies. The code review does find some cyclic dependencies and the architect notices that quick fixes exist for a couple of them. A quick fix is a supplied automated way to repair a common finding. To refactor the code to be free of cycles, the architect applies the quick fix to one cyclic dependency.

In the final exercise, the architect runs a code review and fixes one of the findings.



Exercise

In this exercise you perform the following tasks:

  1. Run a code review to find cyclic dependencies.
  2. Recognize when a cyclic dependency has a supplied quick fix.
  3. Apply a quick fix to resolve a cyclic dependency:
  4. Get a confirmation that the quick fix has been applied.


Selecting a code review to check for cyclic dependencies

To select a code review that checks for cyclic dependencies:

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

  3. In the Select Code Review list, click Structural Analysis Code Review.
  4. Expand the Structural Analysis folder and clear everything but the Cyclic Dependency subfolder.
  5. Expand the Cyclic Dependency subfolder to see the rules that will be applied in the code review, as shown in the following screen capture. Click OK.
  6. Preferences window



Selecting a code base to review

To select the workspace 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 list of cyclic dependencies found

The code review found four cyclic dependencies, as shown in the next screen capture. A cyclic dependency is an undesirable anti-pattern that should be avoided. In a cyclic dependency, a group of objects is so interconnected that a change to any object could affect all of the others.

Code Review findings, one collapsed folder

  1. Expand the Structural Analysis: Cyclic Dependency folder and note the following findings in it, as shown in the next screen capture:
  2. Code Review findings, one expanded folder

  3. Right-click the third finding in the list. The Quick Fix pop-up menu choice varies depending upon the solution. For the cyclic dependency you selected, the fix is to move the static field from the base class to another class, thereby eliminating the cyclic dependency.
  4. Click Quick Fix: Move static field from base class.
  5. Quick Fix pop-up menu for selected finding



Applying the quick fix

The quick fix for the cyclical dependency you selected is to move the static field from the base class to another class. You can choose to move the field to an existing class or to a new class.

To move the field to a new class:

  1. In the next screen capture, review the read-only information about the cyclic dependency, also referred to as a tangle, that you are going to fix:
  2. Click Generate a new Java class and type:
  3. Specify class to move code to

  4. Click Preview to see the code that the quick fix will change.
  5. Expand the Changes to be performed list, as shown in the following screen capture, to see exactly what changes the quick fix will make to the code when it moves the static field to the new class.
  6. Changes to be performed

  7. Click the fourth change in the list, that begins with Font.java, to see a side-by-side view of the code. The original code is on the left and the refactored code that will be created by the quick fix is on the right.
  8. Original and refactored code for selected change

  9. Click OK to apply the quick fix to all of the selected changes in the list.
  10. After the quick fix has been applied, you see a checkmark as confirmation so you know that the problem is resolved.
  11. Code Review, checkmark next to resolved finding

You have completed Exercise 1.4: Resolving a structural problem.



Leveraging the power of quick fixes

Quick fixes are supplied for some common findings in code reviews. By applying a supplied quick fix, you have an automated way to resolve a cyclic dependency. You can:



Wrapping up Exercise 1.4

You have completed Exercise 1.4: Resolving a structural problem. In it you performed the following tasks:

  1. Ran a code review to find cyclic dependencies.
  2. Recognized when a cyclic dependency has a supplied quick fix.
  3. Applied a quick fix to resolve a cyclic dependency:
  4. Got a confirmation that the quick fix has been applied.

Finish the tutorial by reviewing the learning objectives in the Summary.



Feedback
(C) Copyright IBM Corporation 2004, 2005. All Rights Reserved.