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:
- Run a code review to find cyclic dependencies.
- Recognize when a cyclic dependency has a supplied quick fix.
- Apply a quick fix to resolve a cyclic dependency:
- See a list of changes to be made.
- View the existing and refactored code for each change.
- 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:
- On the toolbar in the Code Review view click the Manage Rules icon,
.

- In the Select Code Review list, click Structural Analysis Code Review.
- Expand the Structural Analysis folder and clear everything but the Cyclic Dependency subfolder.
- 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.

Selecting a code base to review
To select the workspace as the code base to review:
- On the toolbar in the Code Review view click the Review icon (
) >Review Workspace.
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.

- Expand the Structural Analysis: Cyclic Dependency folder and note the following findings in it, as shown in the next screen capture:

- Each cyclic dependency has the highest severity level of problem (
) assigned to it.
- Two of the cyclic dependencies have a quick fix (
) to eliminate the dependency.
- 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.
- Click Quick Fix: Move static field from base class.

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:
- 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:
- Classes that are part of the tangle
- Dependencies between classes in the tangle
- Click Generate a new Java class and type:
- src/ for the source folder name
- com.ibm.r2a.rules.rsa.examples for the package name
- Defaults for the class name

- Click Preview to see the code that the quick fix will change.
- 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.

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

- Click OK to apply the quick fix to all of the selected changes in the list.
- After the quick fix has been applied, you see a checkmark as confirmation so you know that the problem is resolved.

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:
- Identify and evaluate cyclic dependencies.
- Eliminate the dependency quickly with an automated quick fix.
- See a list of exactly what changes the quick fix would make to your code.
- Fix the cyclic dependency consistently each time.
Wrapping up Exercise 1.4
You have completed Exercise 1.4: Resolving a structural problem. In it you performed the following tasks:
- Ran a code review to find cyclic dependencies.
- Recognized when a cyclic dependency has a supplied quick fix.
- Applied a quick fix to resolve a cyclic dependency:
- Saw a list of changes to be made.
- Viewed the existing and refactored code for each change.
- Got a confirmation that the quick fix has been applied.
Finish the tutorial by reviewing the learning objectives in the Summary.