Exercise 4: Refactoring in an IDE

This short exercise explores how some of the refactorings done in the previous exercise can be performed much more easily with the aid of the IntelliJ IDE. For more information on automated refactoring using IntelliJ, please see the following help pages:


  1. Download the code for this exercise and unzip the file. This should create a directory named exercise4. The code in this directory is the car rental example from the lectures and previous exercise, before any refactorings have been applied.

  2. Follow the IntelliJ instructions from the Unit Testing exercise to import the exercise4 directory as an IntelliJ project and run the unit tests.

Extract Method

  1. Open Customer.java and locate the statement() method. Use the mouse to select the switch statement inside the loop and the preceding definition of the thisAmount variable. Then choose RefactorExtractMethod, or press Ctrl+Alt+M on Linux/Windows (⌥+⌘+M on Mac).

    When the Extract Method dialog appears, change the method name to amountFor, uncheck the ‘Generate annotations’ box, then click the Refactor button. Examine the code to review the changes that IntelliJ has made.

  2. Run the tests and make sure that they still pass.

Renaming Variables

  1. In the editor, go to the newly-created amountFor() method. Click on the definition of the thisAmount variable inside this method. Choose RefactorRename from the menubar or press Shift+F6.

    You won’t see a dialog appear because renaming is done directly in place, within the editor. Simply type amount as the new name and press Esc to finish. All occurrences in the method will be changed.

    Run the tests and make sure that they still pass.

  2. Use the Commit tool window to commit the code changes. Before you click the Commit button, you’ll need to use the checkboxes to select the changes that should be included in the commit, and you’ll need to update the ‘Commit message’ text area with a new message.

  3. Press Ctrl+V to open the VCS Operations pop-up menu. Choose the ‘Push’ option from this menu to open the Push Commits dialog. Review the commits to be pushed, then click the Push button at the bottom-right of the dialog to push those commits to gitlab.com.

Move Method

In the lectures, we moved the definition of the amountFor() method from Customer to Rental, renaming it in the process. This can be achieved with two steps in IntelliJ.

  1. Position the cursor on the name of the amountFor() method, then choose RefactorMove Instance Method…, or press F6. On the resulting Move Instance Method dialog, choose ‘Public’ as the visibility, then click Refactor.

    Run the tests and make sure that they still pass.

  2. Now change the name of the moved method to getCharge(), following the same approach as used earlier in Renaming Variables. Notice how the method call made in the statement() method of Customer is also changed.

    Once again, run the tests and make sure that they still pass.

Inline Temp

In the lectures, we eliminated the temporary variable thisAmount by applying Inline Temp. We can do the same very easily in IntelliJ.

  1. Edit Customer.java and go to the statement() method. Position the cursor on the thisAmount variable – either on the line where it is defined or on of the two subsequent lines where it is used.

  2. Choose RefactorInline Variable, or press Ctrl+Alt+N on Linux/Windows (⌥+⌘+N on Mac). Observe the changes that have been made. Then run the tests and make sure that they still pass.