Refactoring Assistants

(Last Update: 2020-03-02)
 
In this session, we will learn about some different tools available for refactoring assistance. By using tools, we can plan ahead the refactorings activities that could improve our design. We will search for bad smells (symptoms of design problems) that give us hints on where and how to refactor. When planning refactoring activities, keep in mind the pattern "Keep it Simple" (OORP, p.37), as it a common mistake for people to over-complicate the design of a refactored artifact. Another important pattern to remember when refactorings is “Most Valuable First” (OORP, p.29), as in you should prioritize the refactorings that bring more benefits first.
 
During this session, we will perform simple refactoring tasks. However, it is important to remember that for the Reengineering Course we focus on Strategic Refactoring, i.e., we should refactor with a clear reason or goal in mind. 
 
 

Materials & Tools Used for this Session

  • Session slides here.
  • IntelliJ IDE (you can use Eclipse at your discretion, but it may require some adaptations for the project we are using during the lab sessions)
  • JPacman repository.
  • CodeScene - *no* installation necessary, but it requires a GitHub account. This tool integration with GitHub allows it to visualize your repositories. The Technical Debt part show refactoring targets. The Code Biomarkers show a more detail analysis of smells but it is only available to paid subscribers.
  • SonarQube is a tool/platform that performs static analysis on source codes.  
  •  
 

Auxiliary Tools

Auxiliary tools are not required for the lab session itself, but they may be useful to get additional information (or alternatives) on a project. Use them at your own discretion. 

  • RefactoringMiner is an API/tool that detects the history of refactorings applied to a project.
  • PMD is a tool for analyzing source code, it can detect some smells and bad practices.
  • SpotBugs is a tool for finding bugs in Java code. Some of its detection categories can be considered smells (instead of actual bugs). Therefore, this tool is useful to find code smells and bad programming practices that may require refactoring. This is the continuation of the FindBugs tool which has been stale since 2015.
  • JDeodorant is an Eclipse plugin that detects some Code Smells and recommends appropriate refactorings to "fix" them. It supports these design smells: Type Checks, Feature Envy, God Class, Long Parameter. JDeodorant is famous in academia, so much that everyone doing work on refactoring/smells compares it with JDeodorant.
  • JMove is another Eclipse plugin, specialized in performing only move method refactorings. Therefore, it is a good tool to detect the Feature Envy smell.
 
 

Setup / Preparation

Be sure to follow the setup from the first session (Metrics and Visualization Lab Session) if you had not done so (summarizing it would be: (i) install IntelliJ, (ii) fork JPacman repository, (iii) build & run JPacman). Also if have not already, download the book for this course "Object-Oriented Reengineering Patterns(Note: OORP, p.xx refers to a page in the pdf version of this book)

Get/Install the tools & plugins detailed in the Materials above (not the auxiliary tools, they are optional and will not be used in this lab session).

 

Task 1 -- JPacman on CodeScene

 
For our first task, we are going to use CodeScene to suggest which artifacts are in need of refactoring. If you did the setup from the Metrics and Visualization lesson, you should already have JPacman in your CodeScene. If not, go to the CodeScene, login (or create a new free account), and add JPacman so you can see it on the CodeScene interface.
 
Click in the "Technical Debt" menu on the left side, and see the hotspot visualization. In this visualization, the hotspots are artifact with a lot of commit activities. On that visualization, you can check the tab on refactoring targets (attention that is different from the "refactoring targets" on the submenu of "Technical Debt"). Look at the recommended refactorings targets.
 
Now click in the submenu "Refactoring targets" (under "Technical Debt") on the left side. This shows a list of the files recommended for refactoring. Click on the "X-ray" button for details on method-level complexity for the red artifacts (I recommend especially the Level.java). 
 
Questions:
  • Did CodeScene visualization help you identify possible targets for refactoring?
  • Did CodeScene give you hints or clues on how to refactor the proposed targets?
 
 

Task 2 -- JPacman on SonarQube

 
For the second task, we are going to use a more complex and dedicated tool to find refactoring targets. Download SonarQube (if you haven't already), and unpacked it on your computer. We will need to use command lines to start the SonarQube tool. Open a Terminal (Command Prompt on Windows) and make sure you are currently on the SonarQube folder. Then, you should go into the bin folder like for example:
cd bin
 Inside the bin folder of SonarQube, you will find specific folders for each major operating system. Go into the appropriate folder for your machine operating system. For example, on a machine running Mac OsX the command will be:
cd macosx-universal-64
Now you can start the SonarQube using the command:
[ Linux / Mac ] ./sonar.sh console 
[ Windows ] StartSonar.bat
If you are having problems, please check the SonarQube Documentation (here is the Getting Started guide). If everything went ok, then you should see in your terminal a message like "<date> INFO yyy [ xxxx ] SonarQube is up". Now open your browser and type the following address on it:
http://localhost:9000
Click on "Log In" (top-right corner). The login is "admin" and password is also "admin" (you can change later if you want). After the login, you can click on "Create New Project". For the Project Key and Display Name type "JPacman" (if you change the name, you will need to adapt other commands later, as they use the Key to identify the project). 

Now you will see the page for "Analyze you Project". In the first step, for the token name type "jpacmantoken". Click on "generate" and then "continue" (remember this generated code for step 2).

If everything was ok, then you should see step 2. Select "Java" and then "Gradle". You can follow the instructions given but for this lab session, you can just keep following here. So the first thing to do is update your build Gradle file (if you fetched the last version of the repository, this is not necessary). Open your IntelliJ with the JPacman sources, and in the project root folder you will find the "build.gradle" file. Open the file and add this line after line 7 (inside the "plugins" element):
id "org.sonarqube" version "2.7"
Now you can use the terminal inside IntelliJ for the command given by SonarQube, place all in the same line (adapt the command accordingly for Windows):
./gradlew sonarqube -Dsonar.projectKey=JPacman  
   -Dsonar.host.url=http://localhost:9000 -Dsonar.login=<generated token code>
If you are having problems with the above command, try this one instead:
./gradlew sonarqube -Dsonar.projectKey=JPacman  
    -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=admin
If everything was ok, then the page on SonarQube should update in a while with JPacman source code information. Here is a screenshot of SonarQube once it finishes its analysis on JPacman.
 
Click on the "Code Smells" and analyze the detected smells. You can also see that SonarQube also gives some explanation of the smells ("Why is this an issue?"). 
 
Questions:
  • Which one is more useful to find refactoring targets: CodeScene or SonarQube?
  • Which one provides better reasoning/explanation on the possible refactoring targets: CodeScene or SonarQube?
  • Are there many common artifacts found in the Code Smells in SonarQube and Refactoring targets in CodeScene?
 
 

Task 3 -- JPacman Strategic Refactoring


For the Reengineering Course, we value the concept of Strategic Refactoring, which is refactoring with a goal. Tools can help identify artifacts with smells that could lead to potential issues. However, only a developer can really identify the necessary artifacts for a specific goal. Let's do that for JPacman. 
 
As you probably noticed, JPacman is missing many features that were present in the original Pacman game. Now, the idea is to start refactoring the code to support one of those missing features: Extra Lives. In the original Pacman, the player started with 3 lives. In JPacman, there is only 1 life, and if lose it: game over. If you look at the code (Player.java) there is no attribute to store how many lives the player has left. 
 
Look at JPacman's artifacts and plan the necessary refactoring(s) to support multiple lives. You can start on the simplest refactorings as to not "break" the code or go big according to the pattern "Most Valuable First". There is no wrong path here, do whichever you think is easier or most logical for you. Please note, you need only to "plan" the refactorings for this lab session there is no need to actually apply the refactorings on the code (of course, if you want to do it, go ahead --- just remember that planning is the important part here).
 
Questions:
  • Which were your strategies and reengineering patterns for planning this refactoring? 
  • Why did you consider these refactorings important for your goal?
  • Did the previous tools (CodeScene or SonarQube) identify the refactoring targets you deemed necessary for this goal?
  • Do you prefer to refactor just to improve code quality or to refactor with a goal?
 
 

Optional Task -- JPacman Strategic Refactoring (part 2)


For this optional task, you should apply the planned refactorings from the last task on the source code. Remember to assure that your refactorings are not breaking the application.
 
 

Additional Reading Material


In case you want to dive deeper into refactoring, here are some extra materials about it.

  • M. Fowler, K. Beck, J. Brant, W. Opdyke, and D. Roberts. Refactoring: Improving the Design of Existing Code. Object Technology Series. Addison-Wesley, 1 edition, June 1999.
  • M. Lanza and R. Marinescu. Object-Oriented Metrics in Practice - Using Software Metrics to Characterize, Evaluate, and Improve the Design of Object-Oriented Systems. Springer, 2006.
  • M. Fowler and J. Kerievsky. Smells to refactorings quick reference guide. reference sheet, 2005.: http://www.industriallogic.com/blog/smells-to-refactorings-cheatsheet/
  • F. Khomh, M. D. Penta, Y.-G. Guéhéneuc, and G. Antoniol. An exploratory study of the impact of antipatterns on class change- and fault-proneness. Empirical Softw. Engg., 17(3):243–275, June 2012. http://link.springer.com/article/10.1007%2Fs10664-011-9171-y