refactor to the limit!

66
Refactor to the Limit!

Upload: jim-bethancourt

Post on 12-Apr-2017

397 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Refactor to the Limit!

Refactor to the Limit!

Page 2: Refactor to the Limit!

2

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Agenda

Page 3: Refactor to the Limit!

3

Your Code Stinks!

The following are from Object Oriented Metrics in Practice, Lanza & Marinescu (2004)

God Class – Performs too much work on its own and centralizes the intelligence of a system. Reduces reusability, understandability, and maintainability of a class or subsystem.

Feature Envy – Method that is more interested in the data of other classes than its own class. The method may be misplaced and should be moved to another class.

Data Class – “Dumb” data holders without complex functionality but other classes strongly rely on them.

Brain Method – Centralizes the functionality of a class, and is often hard to maintain and understand. Is long, has excessive branching, deep nesting, and uses many local and instance variables.

Brain Class – Similar to God Classes in that they are large and complex, but do not abusively access the data of “satellite” classes or are more cohesive. They have at least one Brain Method.

Intensive Coupling – A single client method is strongly coupled with only a few provider classes.

Dispersed Coupling – A single method is excessively tied to many other operations dispersed among many classes.

Shotgun Surgery – A single method has many methods in other classes depending on it.

Refused Parent Bequest – Child class does not specialize its parent class

Tradition Breaker – Child class does not enhance / specialize the functionality of the parent class

Disharmonies We’ll be Talking About Today

Page 4: Refactor to the Limit!

4

Your Code Stinks!

How the Disharmonies are Related

From Object Oriented Metrics in Practice, Lanza & Marinescu (2004)

Page 5: Refactor to the Limit!

5

Your Code Stinks!

God Classes • 5 – 7 times more change prone• 4 – 19 times more bug proneDispersed Coupling• Points to defect and maintenance prone classes

From the Technical Debt webinar at https://fc-d.umd.edu/sites/default/files/Publications/ReportsAndPresentations/TD_webinar_11_11_10.pdf

The Worst Offenders

Page 6: Refactor to the Limit!

6

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 7: Refactor to the Limit!

7

JDeodorant

Detects several code smells and provides actionable suggestions to remedy them:

• God Class

• Long Method

• Type Checking

• Feature Envy

• Duplicated Code (not discussed here)

Project site: http://users.encs.concordia.ca/~nikolaos/jdeodorant/

Written by a team of professors and PhD students

About JDeodorant

Page 8: Refactor to the Limit!

8

JDeodorant

1) Install from the update site http://users.encs.concordia.ca/~nikolaos/update/

2) Open the desired smell detection strategy from the Bad Smells menu

How to Use JDeodorant

Page 9: Refactor to the Limit!

9

JDeodorant

3) Select the project you would like scanned from the Package Explorer

4) Click on the i button in the opened smell view to start the scan

How to use JDeodorant

Page 10: Refactor to the Limit!

10

JDeodorant

Identifies God classes and proposes class extraction refactorings

Took 11 minutes (w/3GB RAM) to analyze JHotDraw 7.6 (120K LOC) and produced 115 suggestions

God Class

Page 11: Refactor to the Limit!

11

JDeodorant

Iidentifies Feature Envy smells and suggests Move Method refactorings

Took about 5 min (w/3GB RAM) to run against JHotDraw 7.6

Feature Envy

Page 12: Refactor to the Limit!

12

JDeodorant

A long method may not be entirely cohesive and it may be possible to extract one or more blocks of logic into a separate method, making the method easier to understand and maintain.

Took 1 hour and 40 minutes to analyze JHotDraw 7.6 (w/3GB RAM) and didn’t find any instances of this smell. May run faster with more memory.

Long Method

Picture from JDeodorant Site

Page 13: Refactor to the Limit!

13

JDeodorant

The Type Checking scan identifies where polymorphism or the strategy pattern should be used

Took less than 10 seconds to analyze JHotDraw 7.6 and did not find any instances of this smell.

Type Checking

Picture from JDeodorant Site

Page 14: Refactor to the Limit!

14

JDeodorant

ProsOpen Sourced under the Eclipse Public License at https://github.com/tsantalis/JDeodorant

Works in the most recent release of Eclipse

Provides UML visualizations

At version 5.0 and is mature and full-featured

Successfully works in most recent release of Eclipse (Mars R.2)

Once project is parsed for a smell, incremental scans can quickly be re-run against individual packages or classes

ConsCan’t run in the background

Some detections can take a long time for medium and large projects

Pros and Cons

Page 15: Refactor to the Limit!

15

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 16: Refactor to the Limit!

16

JMove

• JMove identfies Feature Envy smells and suggests Move Method refactorings

• If the method is could be moved to multiple classes, a recommendation is made to move it to the class where it will be the most similar to the other methods in that class

• Project page: http://aserg.labsoft.dcc.ufmg.br/jmove/ (links to paper and plugin)

• Took 45 minutes against JHotDraw 7.6 w/3GB RAM and provided 13 suggestions

• Checks if a method has more similarities to the methods in another class than its current class. It takes dependencies of the following types between the current class and possible target classes into account:

• Method Calls• Field accesses• Object Instantiations• Local Declarations• Return Types• Exceptions• Annotations

About JMove

Page 17: Refactor to the Limit!

17

JMove

1) Download from http://aserg.labsoft.dcc.ufmg.br/jmove/ and unzip in your Eclipse installation root directory.

2) Start Eclipse and select Bad Smells -> JMove or Refactorings -> JMove from the menu.

3) Select the project you’d like scanned from the Package Explorer.

4) Click on the i button in the JMove view to start the scan

How to use JMove

Page 18: Refactor to the Limit!

18

JMove

Jmove in Action

Page 19: Refactor to the Limit!

19

JMove

Pros• Tool authors found that JMove‘s precision and recall were 60% and 81%, compared to 26% and 54%

for JDeodorant. Authors found it produced fewer recommendations, but they were better.

• Open sourced under the MIT license at https://github.com/vitormsales/JMove

• Easy to install and use

Cons• Required to scan an entire Eclipse project – no incremental re-scans

• Can’t be run in the background

• Has been at Beta 0.1 for 3 years

• Not under active development

• Stops working mid-analysis in the Eclipse Mars.2 (4.5.2) but works in Mars (4.5.0)

Pros and Cons

Page 20: Refactor to the Limit!

20

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 21: Refactor to the Limit!

21

JSpIRIT

Identifies 10 of the 11 code disharmonies presented in Lanza & Merinescu’s “Object Oriented Metrics in Practice” The Significant Duplication disharmony was not included

Found 1349 disharmonies in JHotDraw 7.6 in 1 minute (with 2 GB RAM)

Identified disharmonies can be clicked on to show where the disharmony is located in the codebase

About JSpIRIT

Page 22: Refactor to the Limit!

22

JSpIRIT

1) Download the non-agglomeration version from the JSpIRIT page at https://sites.google.com/site/santiagoavidal/projects/jspirit

2) Copy it to the dropins directory of your Eclipse installation and start Eclipse

3) Select the project you’d like to analyze, right click it, and select JSpIRIT -> Find Code Smells

How to use JSpIRIT

Page 23: Refactor to the Limit!

23

JSpIRIT

How to use JSpIRIT

The JSpIRIT View will pop up once the analysis is completeClicking on the item in the view will bring you to the afflicted class or method

Page 24: Refactor to the Limit!

24

JSpIRIT

Pros

Works with most recent release of Eclipse (Mars.2 / 4.5.2)

Ranks which design flaws are the worst

Clicking on the design flaw brings you to the element in the code

Ranking is configurable

Free as in beer

Cons

Must be run on entire project. No incremental analysis is possible

Does not propose fixes or specifics about the problem

Eclipse not responsive during analysis

Agglomeration view produces errors in the Agglomeration version

Not open source

Pros and Cons

Page 25: Refactor to the Limit!

25

JSpIRIT

Similar to JSpIRIT in its goal of detecting same disharmonies

Available fromhttps://sewiki.iai.uni-bonn.de/research/cultivate/tutorial_exploring_smells_and_metrics

Unfortunately it is a bit difficult to set up and use

Similar Tool: Cultivate

Page 26: Refactor to the Limit!

26

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 27: Refactor to the Limit!

27

Stench Blossom

Identifies code smells as you develop and makes you aware of them without getting in your way.

Detects 10 code smells and indicates their severity:

• Data Clumps

• Feature Envy

• Message Chain

• Switch Statement

• Typecast

• InstanceOf

• Long Method

• Large Class

• Primitive Obsession

• Magic Number

• (Long) Comments

• Duplicated Code

About Stench Blossom

Page 28: Refactor to the Limit!

28

Stench Blossom

Install the Stench Blossom Eclipse plugin fromhttps://github.com/CaptainEmerson/refactoring-tools/raw/master/installables/update_sites/stench_blossom

Enable the Stench Blossom Smell Indicator in the Eclipse Toolbar

Installing Stench Blossom

Page 29: Refactor to the Limit!

29

Stench Blossom

Less obvious smells -> orange and above the horizontal line

Obvious smells -> blue and below the horizontal line

Horizontal (thin) line appears if the cursor is not in the editor

Each smell has a specific wedge

Worse the smell, the larger the radius

Understanding the Ambient View

Page 30: Refactor to the Limit!

30

Stench Blossom

Hovering your mouse over a wedge shows which smell it is indicating

Clicking on the tooltip will enable the Exploration View

Using the Active View

Page 31: Refactor to the Limit!

31

Stench Blossom

Using the Explanation View

References to methods and variables of external classes are highlighted

Suggestions not provided on purpose to allow developer to come up with the best solution

Explanation views can be pinned / prevented from scrolling by clicking on the staple icon

Page 32: Refactor to the Limit!

32

Stench Blossom

Code elements are boxed in the editor when hovered in the Explanation view

Using the Exploration View

Page 33: Refactor to the Limit!

33

Stench Blossom

Pros

Identifies smells as you work, avoiding the need to stop working and wait for analysis to complete

Does not slow the IDE down

Open Sourced at https://github.com/DeveloperLiberationFront/refactoring-tools

Documented in https://pdfs.semanticscholar.org/5705/85ed977bf37b586d6a01e2845a388afb1a26.pdf

All features work in Eclipse Mars R.2

Cons

Not sure if it works with Java 8 features since it was last updated over 2 years ago

Pros and Cons

Page 34: Refactor to the Limit!

34

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 35: Refactor to the Limit!

35

TopicViewer

Analyzes words used in source files. Can optionally include Javadoc and comments in vocabulary extraction.

Produces a colorful map with recommendations of classes that belong together.

Project site at https://code.google.com/archive/p/topic-viewer/

I’ve exported the code from Google Code to https://github.com/jimbethancourt/topic-viewer and recently improved semantic clustering performance by more than 2X (2:10 mins -> 43sec onJHotDraw) for version under source control.

Downloadable from https://dl.dropboxusercontent.com/u/67114006/TopicViewer.rar (from Google Code project site)

Vocabulary Extraction (1st step) took about 30 seconds for JHotDraw 7.6 (using defaults)

Semantic Clustering (2nd step) took less than 1 minute and 30 seconds for downloaded version

About TopicViewer

Page 36: Refactor to the Limit!

36

TopicViewer

Our Goal: A Distribution Map

Page 37: Refactor to the Limit!

37

TopicViewer

1) Download Topic Viewer from https://dl.dropboxusercontent.com/u/67114006/TopicViewer.rar and unzip

2) Double-click the TopicViewer.jar and TopicViewer starts. To use more memory than the default heap size of your JVM, you will need to launch it from the command line or a batch file.

Installing TopicViewer

Page 38: Refactor to the Limit!

38

TopicViewer

Activities -> Configure Workspace

Step 1: Configure Workspace

Page 39: Refactor to the Limit!

39

TopicViewer

Select a workspace directory where the project files for a single codebase analysis will live

Click Open Workspace

You should get the message “Workspace has just been configured” once complete

Step 2: Select a Workspace

Page 40: Refactor to the Limit!

40

TopicViewer

Step 3: Extract the Project Vocabulary

Page 41: Refactor to the Limit!

41

TopicViewer

Click the Select button (on the top right) and select desired root directory of your codebase. Be careful you don’t include test directories if you don’t want them included in the analysis.

Set desired analysis options and click the Start button.

Step 4: Run Semantic Analysis of Codebase

Page 42: Refactor to the Limit!

42

TopicViewer

Click on Activities -> Semantic Clustering

Specify parameters (I take the defaults)

Click Select on the Top right, select workspace/lsi/src-root-directory-name.matrix, and click Open

Click Start on the bottom left

Step 5: Run Semantic Clustering

Page 43: Refactor to the Limit!

43

TopicViewer

Select Activities -> View Results -> Distribution Map

Click Select in the top left and select workspace/correlation/src-root-directory-name.matrix

Classes that may belong together in the same package have the same color / number

Step 6: Open Distribution Map

Page 44: Refactor to the Limit!

44

TopicViewer

ProsProduces insightful and helpful suggestions

Simple

ConsNo guided workflow

App not responsive during analysis

Progress bar doesn’t update until it’s complete

Medium and large codebases require more memory and time.

Attempts implement concurrency and streams have failed miserably.

Notes

Downloaded version run faster for large codebases since it uses Sparse matrices in some spots and needs less memory.

May be worth backing out the Parallel Colt migration and re-applying code changes made in the HierarchicalClustering class (mostly fixing equals / not equals checks)

Pros and Cons

Page 45: Refactor to the Limit!

45

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 46: Refactor to the Limit!

46

iPlasma

Tool for performing quality analysis of object-oriented software systems

Development led by the authors of the book Object Oriented Metrics in Practice

Project page: http://loose.upt.ro/reengineering/research/iplasma

Download from http://loose.upt.ro/iplasma/iplasma.zip

Once unzipped, run unzip_Dir\iPlasma6\tools\iPlasma\insider.bat to launch iPlasma

About iPlasma

Page 47: Refactor to the Limit!

47

iPlasma

Click Load -> Java Sources

Using iPlasma

Page 48: Refactor to the Limit!

48

iPlasma

Enter the path of the root directory of the codebase you want to analyze in the Source path textbox. You can also navigate to it and select it by clicking on the “…” button

The Cache file name textbox can be left blank

Click OK and the model will load. JHotDraw 7.6 loaded in just over a minute (1:04)

Using iPlasma

Page 49: Refactor to the Limit!

49

iPlasma

Once codebase is loaded, you’re presented with an Overview Pyramid.

Quick explanation available at https://sewiki.iai.uni-bonn.de/research/cultivate/tutorial_exploring_smells_and_metrics#overview_pyramid

Using iPlasma

Page 50: Refactor to the Limit!

50

iPlasma

Click on the directory you’re interested in learning more about and the Group menu option will activate:

Using iPlasma

Page 51: Refactor to the Limit!

51

iPlasma

The Group menu allows you to select a group of metrics (most but not all work).

iPlasma’s Group Menu

Page 52: Refactor to the Limit!

52

iPlasma

Options in the Group and Property menus change depending on what element is clicked in the navigation tree tree in the top left.

Group menu options show new children in the navigation tree

Property menu options show new sortable columns

The Group and Property Menus Change

Page 53: Refactor to the Limit!

53

iPlasma

Clicking a class will show details about the class, the disharmonies the class is suffering from, and recommendations of improvements that could be made to it.

Click a Class to Learn More About It

Page 54: Refactor to the Limit!

54

iPlasma

Click on an item under Root and then select an option from the Filter menu to filter in (show) only classes with that property

Filter Menu

Page 55: Refactor to the Limit!

55

iPlasma

ProsParses source quickly

Open sourced at https://sourceforge.net/p/iplasma-horia/code/HEAD/tree/ (no license specified)

Finds a number of issues in codebase that are not found by other tools

ConsOlder – Parser may not support Java 8 code

Somewhat clunky and not the most intuitive tool to use

Not all features are fully implemented (though most appear to be)

Pros and Cons

Page 56: Refactor to the Limit!

56

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 57: Refactor to the Limit!

57

The Plan

On an day-to-day basis

• Refactor as you develop – there’s no better time than the present

• Focus on delivering business value for your effort and minimize business disruption

• Don’t refactor all problems.

Only stop work and refactor

• Code that changes frequently• See Ranking Refactoring Suggestions based on Historical Volatility http://users.encs.concordia.ca/~

nikolaos/publications/CSMR_2011.pdfor the presentation at http://www.slideshare.net/tsantalis/ranking-refactoring-suggestions-based-on-historical-volatility

• Code that is bug prone • It will likely have bugs in the future if not refactored

Coordinate move method & move class refactorings with your team to avoid surprises since these refactorings may impact a significant number of classes.

What / When Should I Refactor?

Page 58: Refactor to the Limit!

58

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 59: Refactor to the Limit!

59

If I Had Time

Another tool for identifying Extract Method opportunities, but it doesn’t work

Authors indicated recall and precision was significantly higher than JDeodorant

Project page: http://aserg.labsoft.dcc.ufmg.br/jextract/ (with downloadable source)

Paper: http://homepages.dcc.ufmg.br/~mtov/pub/2014_cbsoft_jextract.pdf

Video: https://www.youtube.com/watch?v=6htJOzXwRNA

JExtract

Page 60: Refactor to the Limit!

60

If I Had Time

Analyzes source code commit history and identifies co-change clusters. Idea is that code that changes together belongs together.

Project site: http://aserg.labsoft.dcc.ufmg.br/modularitycheck/

Source code: https://github.com/aserg-ufmg/ModularityCheck

ModularityCheck

Page 61: Refactor to the Limit!

61

If I Had Time

Eclipse plugin that makes software remodularization / class move suggestions:

https://www.sg.ethz.ch/media/publication_files/2014_Modularity_1.pdf

https://sourceforge.net/projects/somomoto/ (plugin doesn’t work in present state) GPL 3 license

From the paper above:

SOMOMOTO

Test case: the remodularization of JGRAPHX (a JAVA graphical library). The JAVA classes are depicted as circles, while their color reflects the corresponding package membership (same color, same package). (a) original. (b) after remodularization by SOMOMOTO.

Page 62: Refactor to the Limit!

62

If I Had Time

Dr. Jehad Delal’s publication Identifying Refactoring Opportunities in Object-Oriented Code: A Systematic Literature Review (http://www.isc.ku.edu.kw/drjehad/identifying%20refactoring%20opprtunities%20IST.pdf) lists a number of papers that discuss refactoring opportunity discovery tools.

Investigate all Papers on Tools

Page 63: Refactor to the Limit!

63

1 Your Code Stinks!

2 JDeodorant

3 JMove

4 JSpIRIT

5 Stench Blossom

6 TopicViewer

7 iPlasma

8 The Plan

9 If I Had Time

10 Wrapup

Page 64: Refactor to the Limit!

64

Wrapup

Not all tools will detect that the same piece of code needs to be refactored

Not every instance of every detected smell may be a valid one

Be careful that move method & move class refactorings do not introduce cyclic dependencies

There is no substitute for YOU and your experience

Most of these tools will likely move you away from package-by-layer and towards package-by-feature (which is usually a good thing!)

Keep in Mind

Page 65: Refactor to the Limit!

65

Wrapup

JDeodorant: http://users.encs.concordia.ca/~nikolaos/jdeodorant/

Jmove : http://aserg.labsoft.dcc.ufmg.br/software/

JSpIRIT: https://sites.google.com/site/santiagoavidal/projects/jspirit

Stench Blossom: https://github.com/CaptainEmerson/refactoring-tools/raw/master/installables/update_sites/stench_blossom

TopicViewer: https://dl.dropboxusercontent.com/u/67114006/TopicViewer.rar

My TopicViewer repo: https://github.com/jimbethancourt/topic-viewer

iPlasma: http://loose.upt.ro/iplasma/iplasma.zip

http://www.oreilly.com/programming/free/files/software-architecture-patterns.pdf

Take a Picture of THIS Page

Page 66: Refactor to the Limit!

Thank You!