The effectiveness of refactoring based on a compatibility testing taxonomy and a dependency graph Steve Counsell and Robert Hierons, Brunel University,

Slides:



Advertisements
Similar presentations
Test Driven Refactoring by Andreas Thies. XP Test Driven Refactoring2 Overview Refactoring Bad Smells Unit Tests Unit Tests and Refactoring Special.
Advertisements

12-Dec-14 Refactoring IV. Previously discussed bad smells Duplicated code — and other forms of redundancy Long method — use short methods that delegate.
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Gregor Gisler-Merz BrownBag Session Refactoring.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Lectures 17 and 18 A Refactoring Micro-Example FOR0383 Software Quality Assurance 5/16/20151Dr Andy Brooks Refactoring is really easy using this tool.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
George Blank University Lecturer. REFACTORING Improving the Design of Existing Code Supplement to Ian Sommerville, Software Engineering, Chapter 20 Prepared.
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
A Survey of Software Refactoring Tom Mens, Tom Tourwé
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
C++ Code Analysis: an Open Architecture for the Verification of Coding Rules Paolo Tonella ITC-irst, Centro per la Ricerca Scientifica e Tecnologica
What is Refactoring? CSE301 University of Sunderland Harry R. Erwin, PhD.
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Software Engineering Modern Approaches Eric Braude and Michael Bernstein 1.
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Refactoring Improving the structure of existing code Refactoring1.
17-Oct-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
Small changes to code to improve it. Refactoring Defined A change made to the internal structure of software to make it easier to understand and cheaper.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
SWE 316: Software Design and Architecture Objectives Lecture # 20 Improving the existing design: Refactoring SWE 316: Software Design and Architecture.
Refactoring1 Improving the structure of existing code.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Introduction to Refactoring Jim Cooper Falafel Software.
Object Oriented Software Development
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring and such ● (Side note) Specialization ● Key terms ● Abstraction, state, persistence and association and their relationship to software development.
Refactoring: Code Smells. Admin Notes REGISTER FOR BLACKBOARD Watch blackboard site for updates on class as hurricane season approaches.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
E L EARNING E NVIRONMENT FOR S OFTWARE E NGINEERING E DUCATION (R EFACTORING A GENT ) A.Stoyanova-Doycheva University of Plovdiv г. 10th Workshop.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Module 3. Smells Between Classes Course: Refactoring.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
March 1, 2004CS WPI1 CS 509 Design of Software Systems Lecture #6 Monday, March 1, 2004.
CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
Refactoring. 2 Process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Module 9. Dealing with Generalization Course: Refactoring.
CS223: Software Engineering Lecture 18: The XP. Recap Introduction to Agile Methodology Customer centric approach Issues of Agile methodology Where to.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Principles and examples
Module Road Map Refactoring Why Refactoring? Examples
Code Smells 1.
Improving the structure of existing code
Refactoring Types Blake Duncan.
Refactoring.
Presentation transcript:

The effectiveness of refactoring based on a compatibility testing taxonomy and a dependency graph Steve Counsell and Robert Hierons, Brunel University, Rajaa Najjar, George Loizou and Youssef Hassoun Birkbeck, London TAIC PART (Cumberland Lodge 31 st August 2006)

Introduction A key software engineering discipline to emerge over recent years is that of ‘refactoring’  a change made to software to improve its structure without necessarily changing the program’s semantics Benefits include reduced complexity and increased comprehensibility of the code ‘Refactoring is the reversal of software decay’ 72 refactorings proposed by Fowler (1999):  Each has specific mechanics: to ‘rename a method’ so that the purpose of the method is expressed more clearly, the method’s name is changed and all references to the originally-named method are changed also

Introduction (cont.) Each requires testing to be undertaken after each step  For ‘rename method’, we also have to consider the case of any subclasses or super class influences  Testing required after each logical step For more complex refactorings, there are more steps in the mechanics, more cases to consider and more testing The real pain:  The mechanics of undertaking refactoring X require the use of refactoring Y, which itself requires the use of refactoring Z….. Research question:  How can we understand these inter-relationships, with specific focus on ‘minimising’ the testing burden?

van Deursen and Moonen’s (VD&M) Taxonomy Refactoring should preserve program semantics VD&M’s stance on this:  “Ideally, this is verified by ensuring that all the tests pass before and after a refactoring. In practice, it turns out that such verification is not always possible: some refactorings restructure the code in such a way that tests can only pass after the refactoring if they are modified.” They suggest a taxonomy to describe which refactorings preserve tests and which do not; initially described by two categories:  Incompatible: The refactoring destroys the original interface. All tests which rely on the old interface must be adjusted in some way to accommodate the new interface.  Backwards Compatible: The refactoring extends the original interface. The tests keep running via the original interface and will pass if the refactoring preserves tested behaviour. In fact, four categories can be identified…….

The VD&M Taxonomy (cont.) Compatible: Refactorings that do not change the original interface (Type B refactorings) Backwards Compatible: Refactorings that change the original interface and are inherently backwards compatible since they extend the interface (Type C refactorings) Make Backwards Compatible: Refactorings that change the original interface and can be made backwards compatible by adapting the old interface (Type D refactorings) Incompatible: Refactorings that change the original interface and are not backwards compatible because they may make such considerable changes that they are not compatible in any sense (Type E refactorings)

The VD&M Taxonomy (cont.) Type B refactorings (Preferable) Change Bi-directional Association to Unidirectional, Replace Magic Number with Symbolic Constant, Replace Nested Conditional with Guard Clauses, Consolidate Duplicate Conditional Fragments, Replace Conditional with Polymorphism, Replace Delegation with Inheritance, Replace Inheritance with Delegation, Replace Method with Method Object, Remove Assignments to Parameters, Replace Data Value with Object, Introduce Explaining Variable, Replace Exception with Test, Change Reference to Value, Split Temporary Variable, Decompose Conditional, Introduce Null Object, Preserve Whole Object, Remove Control Flag, Substitute Algorithm, Introduce Assertion, Extract Class, Inline Temp. Lots of ‘Replacing’, some ‘preserving’ and ‘substituting’ Plenty of raw code refactorings

The VD&M Taxonomy (cont.) Type C refactorings (Not too bad) Consolidate Conditional Expression, Replace Delegation with Inheritance, Replace Inheritance with Delegation, Replace Record with Data Class, Introduce Foreign Method, Pull Up Constructor Body, Replace Temp with Query, Duplicate Observed Data, Self Encapsulate Field, Form Template Method, Extract Super class, Extract Interface, Push Down Method, Push Down Field, Extract Method, Pull Up Method, Pull up Field. Still some ‘Replacing’ refactorings Of interest is the high number of inheritance- based refactorings in this category  Tend to stick together

The VD&M Taxonomy (cont.) Type D refactorings (Not a disaster) Change Unidirectional Association to Bi-directional, Replace Parameter with Explicit Methods, Replace Parameter with Method, Separate Query from Modifier, Introduce Parameter Object, Parameterize Method, Remove Middle Man, Remove Parameter, Rename Method, Add Parameter, Move Method. A strong emphasis on parameter-based manipulations  The addition or removal of parameters easily masked by a wrapper on the original code

The VD&M Taxonomy (cont.) Type E refactorings (Bad news) Replace Constructor with Factory Method, Replace Type Code with State/Strategy, Replace Type Code with Subclasses, Replace Error Code with Exception, Replace Subclass with Fields, Replace Type Code with Class, Change Value to Reference, Introduce Local Extension, Replace Array with Object, Encapsulate Collection, Remove Setting Method, Encapsulate Downcast, Collapse Hierarchy, Encapsulate Field, Extract Subclass, Hide Delegate, Inline Method, Inline Class, Hide Method, Move Field. Of note is the high number of encapsulation-based refactorings  The power of encapsulation as an OO concept can work both- ways

VD&M in perspective While useful and interesting, what is the influence of a nested refactoring?  For example, the ‘Introduce Parameter Object’ refactoring, applicable when a group of parameters are lumped to form an object, requires the use of the ‘Add Parameter’ refactoring  Both are of Type D (which is not a particular problem)  The Extract Class refactoring (Type B) must use the ‘Move Field’ refactoring (Type E) – this is a problem: ‘Use Move Field on each field you wish to move’ and ‘Use Move Method to move methods over from old to new’. We thus need to re-appraise each of the Type B,C, D and E refactorings previously stated

A Dependency Graph To assess the inter-relationships between refactorings, we developed a dependency graph showing which refactorings used other refactorings The graph took one person three months to develop and required all seventy-two refactorings to be parsed, analyzed and checked From the graph so produced, we could then determine:  Which Type B refactorings only used other Type B refactorings?  Which Type C refactorings only used other Type C refactorings and/ore Type B refactorings?  Which Type D refactorings used only combinations of Type D, Type C and Type B refactorings? We could also eliminate any refactoring for which a ‘chain’ of refactorings emanate and hence invalidate the three questions

The VD&M Taxonomy, Re-visited Type B refactorings (the eight survivors)  Consolidate Duplicate Conditional Fragments, Replace Inheritance with Delegation, Remove Assignments to Parameters, Introduce Explaining Variable, Replace Exception with Test, Introduce Null Object, Substitute Algorithm, Inline Temp. We can use these refactorings without fear of having to use any C,D or E type refactorings Many of these are ‘pure code’ refactorings Type C refactorings (the eight survivors)  Replace Inheritance with Delegation, Self Encapsulate Field, Extract Interface, Push Down Method, Push Down Field, Extract Method, Pull Up Method, Pull Up Field. We can use these refactorings without fear of having to use any D or E type refactorings Perhaps inheritance isn’t that bad after all (arguably, 6 fall into this category)

The VD&M Taxonomy, Re-visited Type D refactorings (the ten survivors):  Change Unidirectional Association to Bi-directional, Replace Parameter with Explicit Methods, Separate Query from Modifier, Introduce Parameter Object, Parameterise Method, Remove Middle Man, Remove Parameter, Rename Method, Add Parameter, Move Method. Some of these are ‘core’ refactorings  Commonly used and ‘sinks’ in the dependency graph We don’t care about Type E refactorings!  Whatever refactorings they use

Implications of the results Try to choose Type B refactorings from the revised list  Within Type B refactorings Orphan refactorings  Then Type C, then Type D from the respective revised lists Avoid Type E refactorings We have assumed that each Type B,C or D Type refactoring have equivalent complexities in terms of mechanics:  Choose Type B refactorings with the least complex mechanics Of course, the developer may have no choice as to which refactoring they would like to undertake  In which case our advice may be pointless

Two follow-up studies….

Empirical Study Built a tool that automatically extracted refactoring data from seven Java open-source systems of different application types. For each release of these systems:  Parsed the Java source code  Saved information about fifteen refactorings  Used heuristics to determine if any of those fifteen refactorings had been applied Plotted the data thus extracted

1) Encapsulate Downcast 2) Push Down Method 3) Extract Subclass 4) Encapsulate Field 5) Hide Method 6) Pull Up Field 7) Extract Super class 8) Remove Parameter 9) Push Down Field 10) Pull Up Method 11) Move Method 12) Add Parameter 13) Move Field 14) Rename Method 15) Rename Field.

TypeApplicable Refactorings BNone CPull Up Method (10-65), Push Down Field (9-26), Extract Superclass (7- 23), Pull Up Field (6-14), Push Down Method (2-6), D Move Method ( ), Add Parameter ( ), Rename Method ( ), Rename Field ( ), Remove Parameter (8-24), E Move Field (13-135), Hide Method ( ), Encapsulate Field ( 4 -12), Extract Subclass ( 3 -6), Encapsulate Downcast ( 1 -0) Perhaps developers do avoid Type E refactorings!

The VD&M Taxonomy (cont.) Type B refactorings (Preferable) Change Bi-directional Association to Unidirectional, Replace Magic Number with Symbolic Constant, Replace Nested Conditional with Guard Clauses, Consolidate Duplicate Conditional Fragments, Replace Conditional with Polymorphism, Replace Delegation with Inheritance, Replace Inheritance with Delegation, Replace Method with Method Object, Remove Assignments to Parameters, Replace Data Value with Object, Introduce Explaining Variable, Replace Exception with Test, Change Reference to Value, Split Temporary Variable, Decompose Conditional, Introduce Null Object, Preserve Whole Object, Remove Control Flag, Substitute Algorithm, Introduce Assertion, Extract Class, Inline Temp. Lots of ‘Replacing’, some ‘preserving’ and ‘substituting’ Plenty of raw code refactorings

Code Smell Analysis What is a code smell?  structures in the code that ‘sometimes scream for’ the possibility of refactoring (c.f. ‘stenches’) Fowler specifies 22 code smells Example: Long parameter list Signify constant change Difficult to understand Difficult to use To remedy ‘apply deodorant’ this smell, we may apply a set of refactorings Question: Which of these 22 smells can be remedied by using only refactorings drawn the revised Type B, C and D lists.

Bad Smell (BS)Refactorings Remedies Type Profile Alternative Classes with Different Interfaces Rename Method, Move Method D, D Refused Bequest Replace Inheritance with Delegation B/C (remedy appears in both categories)

Bad Smell (BS)Refactorings Remedies Type Profile Primitive ObsessionReplace Data Value with Object, Extract Class, Introduce Parameter Object, Replace Array with Object, Replace Type Code with Class, Replace Type Code with Subclasses, Replace Type Code with State/Strategy B, B, D, E, E, E, E Data ClassMove Method, Encapsulate Field, Encapsulate Collection D, E, E

Some threats….. We still don’t know if developers actually refactor consciously How can we expect to manage OSS development?:  Do our results confirm that OSS developers avoid high-level (inheritance-based) refactorings?  Are we being too naïve wrt testing efficiency and strategies?  Do developers always just go for the most smelliest smells rather than weigh up the impact that will have on testing effort?

Conclusions/Future work What use is the research to developers?: From a testing perspective:  Choose individual refactorings carefully You may end up with a chain  Choose the eradication of smells carefully Your testing effort required may be the worst smell Future work:  Extending analysis of the relationship types ‘may’ and ‘must’  Refactoring dependency list analysis Which refactorings are ‘used’ or ‘used by’ other refactorings

Thanks for listening!