Implementing evolution: Refactoring

Similar documents
Implementing evolution: Refactoring

Patterns in Software Engineering

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture

Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring

Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004

How We Refactor, and How We Know It

Software Design COSC 4353/6353 D R. R A J S I N G H

void printowing(double amount) { printbanner(); printdetails(); void printdetails(double amount) {

Refactoring. Chen Tang March 3, 2004

Software LEIC. Lecture 23

SOFTWARE ENGINEERING SOFTWARE EVOLUTION. Saulius Ragaišis.

Understading Refactorings

Designing with patterns - Refactoring. What is Refactoring?

Objectives: On completion of this project the student should be able to:

CSE 403 Lecture 21. Refactoring and Code Maintenance. Reading: Code Complete, Ch. 24, by S. McConnell Refactoring, by Fowler/Beck/Brant/Opdyke

Refactoring, 2nd Ed. A love story. Michael Hunger

Refactoring with Eclipse

Refactorings. Refactoring. Refactoring Strategy. Demonstration: Refactoring and Reverse Engineering. Conclusion

DAT159 Refactoring (Introduction)

McCa!"s Triangle of Quality

Refactoring. Software Engineering, DVGC18 Faculty of Economic Sciences, Communication and IT Eivind Nordby

Refactoring Framework for Instance Code Smell Detection

Code Smells & Refactoring

COURSE 11 DESIGN PATTERNS

Template-based Reconstruction of Complex Refactorings

Refactoring. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 11/29/11. University of Colorado, 2011

Refactoring. Section (JIA s) OTHER SOURCES

Beyond the Refactoring Browser: Advanced Tool Support for Software Refactoring

Refactoring Practice: How it is and How it Should be Supported

MSO Refactoring. Hans Philippi. October 2, Refactoring 1 / 49

A Study of Bad Smells in Code

Maintainability and Agile development. Author: Mika Mäntylä

AntiPatterns. EEC 421/521: Software Engineering. AntiPatterns: Structure. AntiPatterns: Motivation

Refactoring 101. By: Adam Culp

Software Engineering I (02161)

Refactoring. George Dinwiddie idia Computing, LLC

F. Tip and M. Weintraub REFACTORING. Thanks go to Andreas Zeller for allowing incorporation of his materials

Refactoring. Refactoring Techniques

Code Smells & Refactoring

Refactoring Tools: Fitness for Purpose

CSC 408F/CSC2105F Lecture Notes

Reengineering II. Transforming the System

Detecting Structural Refactoring Conflicts Using Critical Pair Analysis

Tutorial 02: Writing Source Code

Improving Evolvability through Refactoring

What? reorganising its internal structure

Small changes to code to improve it

Refactoring and Rearchitecturing

Imagine you ve written a piece of code but then accidentally deleted and lost it.

Overview of Eclipse Lectures. Module Road Map

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

Chapter 7. Modular Refactoring. 7.1 Introduction to Modular Refactoring

JUnit 3.8.1, 64. keep it simple stupid (KISS), 48

Continuous Integration / Continuous Testing

Visualizing Software Metrics for increased Refactoring

CHAPTER 9 DESIGN ENGINEERING. Overview

Er. Himanshi Vashisht, Sanjay Bharadwaj, Sushma Sharma

HOW AND WHEN TO FLATTEN JAVA CLASSES?

Refactoring. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 04/19/11. University of Colorado, 2011

Keywords Code clean up, code standard, maintainability, extendibility, software refactoring, bad smell code.

Automating Big Refactorings for Componentization and the Move to SOA

3 Prioritization of Code Anomalies

Lecture Notes. Polymorphism. Polymorphism. If Polymorphism is Not Available. To be placed at:

Refactoring Exercise

A few more things about Agile and SE. Could help in interviews, but don t try to bluff your way through

Hugbúnaðarverkefni 2 - Static Analysis

SAFEREFACTOR Tool for Checking Refactoring Safety

How We Refactor, and How We Know it. Emerson Murphy-Hill, Chris Parnin, Andrew P. Black ICSE 2009

Exam Questions. Object-Oriented Design, IV1350. Maximum exam score is 100, grade limits are as follows. Score Grade 90 A 80 B 70 C 60 D 50 E

Analysis of the Test Driven Development by Example

Living and Working with Aging Software. Ralph Johnson. University of Illinois at Urbana-Champaign

Kerievsky_book.fm Page 355 Thursday, July 8, :12 PM. Index

Inheritance and Substitution (Budd chapter 8, 10)

CSE 70 Final Exam Fall 2009

Object-Oriented Design

Design Patterns Reid Holmes

Implementing evolution: Aspect-Oriented Programming

Software Design & Evolution. Lecture 04. You cannot control what you cannot measure. Metrics & Problem Detection. Michele Lanza

Exploring Software Refactoring Decisions in a Lean Startup

Inheritance (Chapter 7)

Refactoring. Paul Jackson. School of Informatics University of Edinburgh

Agenda. Tool-Supported Detection of Code Smells in Software Aspectization. Motivation. Mistakes in Software Aspectization. Advice-related mistakes

arxiv: v1 [cs.se] 8 Jul 2016

CHAPTER 1 COPYRIGHTED MATERIAL. Finding Your Way in the Inventor Interface

Part 7 - Object Oriented Concepts Classes, Objects, Properties and Methods

Metaprogramming and Reflection Refactoring

Compositional Model Based Software Development

What are the characteristics of Object Oriented programming language?

Program Correctness and Efficiency. Chapter 2

CHAPTER 4 HEURISTICS BASED ON OBJECT ORIENTED METRICS

Architectural Styles. Reid Holmes

PROGETTAZIONE DI SISTEMI ORIENTATI AGLI OGGETTI (OOD)

Software Reengineering P1: Intro & Organization. Martin Pinzger Delft University of Technology

Structural specification

Recap: Class Diagrams

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT OBJECT ORIENTED PROGRAMMING

GA-driven Automatic Refactoring based on Design Patterns

OVERVIEW OF DATABASE DEVELOPMENT

Refactoring. Herez Moise Kattan NUSP: João S. Brito Júnior NUSP:

CSC207 Week 3. Larry Zhang

Transcription:

2IS55 Software Evolution Sources Implementing evolution: Refactoring Alexander Serebrenik / SET / W&I 17-5-2010 PAGE 1 Last week Problem: changing code is difficult Assignment 6 Deadline: Today Assignment 7 Have you looked at it? How to implement evolution Last week: evolution strategies and decision making Today: refactoring Next week: No class (Whit Monday) Correct code can be far from perfect: Bad structure Code duplication Bad coding practices We need to change it Undisciplined code modification may introduce bugs and does not guarantee that the code will actually be improved! Manual work, not clear how to support it beyond copy/paste and replace all / SET / W&I 17-5-2010 PAGE 2 / SET / W&I 17-5-2010 PAGE 3 Refactoring Examples of refactorings Refactoring a disciplined technique for restructuring code, altering its internal structure without changing its external behavior. External behavior not changed New bugs are not introduced Old ones are not resolved! Aims at improving maintainability, performance If similar series or steps are repeatedly executed, create a separate method Rename method If the method s name no longer corresponds to its purpose/behaviour, rename the method Pull up Move the functionality common to all subclasses to the/a superclass Push down If the functionality is needed only in some subclasses move it to the subclass / SET / W&I 17-5-2010 PAGE 4 / SET / W&I 17-5-2010 PAGE 5 1

Refactoring catalogue [Fowler]: Example Name: Inline Temp Applicability: A temp is assigned to once with a simple expression, and it is getting in the way of other refactorings. Replace all references with the expression Motivation: simplifies other refactorings, e.g., Extract Method Steps (Java): Declare the temp as final, and compile Find references to the temp and replace them Compile and test after each change Why would you declare the temp as final? Remove the declaration and the assignment of the temp Compile and test How many refactorings are there? Author Year Language Number Fowler book and website Thompson et al. website 2000 Java 93 Haskell 20 * 3 categories Garrido 2000 C 29 Serebrenik, Schrijvers, Demoen 2008 Prolog 21 Fields et al. 2009 Ruby >70 One has to organize refactorings by categories We will discuss some of the refactorings in more detail! / SET / W&I 17-5-2010 PAGE 6 / SET / W&I 17-5-2010 PAGE 7 Categories of refactorings [Fowler] Closer look: Pull Up Field / Push Down Field Composing methods (extract method, inline temp) Moving features between objects (move field, remove middle man) Organizing data (change value to reference) Simplifying conditional expressions Making method calls simpler (rename method) Dealing with generalization (pull up field) Big refactorings (separate domain from presentation) Pull Up Push Down When would you use each one of the refactorings? Pull Up: field is common to all subclasses Push Down: field is used only in some subclasses / SET / W&I 17-5-2010 PAGE 8 / SET / W&I 17-5-2010 PAGE 9 Pull Up: Seems simple Inspect all uses of the candidate fields Ensure they are used in the same way. If the fields do not have the same name, rename The candidate fields should have the name you want to use for the superclass field. Compile and test. Create a new field in the superclass. If the fields are private, protect the superclass field The subclasses should be able to refer to it. Delete the subclass fields. Compile and test. Consider using Self Encapsulate Field on the new field. / SET / W&I 17-5-2010 PAGE 10 Another example: : Without parameters printf ( " Customer Owes \n" ) ; printf ( "amount %s \n", outst) ; / SET / W&I 17-5-2010 PAGE 11 void printbanner() { printf ( " Customer Owes \n" ) ; printf ( "amount %s \n", outst ) ; 2

: With input parameters : With output parameters void printbanner() { printf ( " Customer Owes \n" ) ; printf ( "amount %s \n", outst ) ; / SET / W&I 17-5-2010 PAGE 12 void printdetails(double outst) { printf ( "amount %s \n", outst ) ; printdetails(outst); void printdetails(double outst) { printf ( "amount %s \n", outst ) ; printdetails(outst); / SET / W&I 17-5-2010 PAGE 13 double getoutst(enumeration e, double outst) { outst = getoutst(e, outst) ; printdetails(outst); : Further simplification double getoutst(enumeration e, double outst) { outst = getoutst(e, outst) ; printdetails(outst); / SET / W&I 17-5-2010 PAGE 14 double getoutst() { printdetails(getoutst()); But is the new program really better than the old one? Assume that we want to improve maintainability Metrics Old New LOC 18 26 Comments 3 3 Ave McCabe 2 5/4 Halstead volume Maintainability index / SET / W&I 17-5-2010 PAGE 15 156 226 57 77 Difficult to maintain Average maintainability The refactoring process How to identify bad smells? Select the maintainability metrics Recall: Goal Question Metrics! Refactoring loop Calculate maintainability metrics Identify a problem: bad smell Check that the refactoring is applicable Refactor Compile and test Recall: without changing its external behavior Recalculate the maintainability metrics Software metrics Size: Large class, large method, long parameter list Dependencies: feature envy, inappropriate intimacy % comments: comments Code duplication Changes (based on version control) Divergent change (one class is changed in different ways for different reasons) Shotgun surgery (many small changes) Parallel inheritance hierarchies / SET / W&I 17-5-2010 PAGE 16 / SET / W&I 17-5-2010 PAGE 17 3

[Tahvildari and Kontogiannis] Feature envy [Simon, Steinbrückner, Lewerentz] Start with complex and tightly coupled classes Fields boxes, methods methods Green Class A, blue Class B Distance 1 p( X ) p( Y ) p( X ) p( Y ) p(x) properties of X Method: the method, methods called and fields used Field: the field and methods that use it Feature envy / SET / W&I 17-5-2010 PAGE 18 / SET / W&I 17-5-2010 PAGE 19 How to identify bad smells? Querying the structure [Tourwe, Mens] Structural problems: obsolete parameters, inappropriate interfaces, Program Query DB of facts Results Parameter p of C.m is obsolete if Neither C.m itself uses p Nor any of the classes inheriting from C and reimplementing m uses p Naïve approach: check all parameters of all methods of all classes Not feasible Better ideas? Query (a la Datalog): obsolete(class,method,parameter):- classimplements(class,method), parameterof(class,method,parameter), forall(subclassimplements(class,method,subclass), not(methodusesparameter(subclass,method,parameter))) Advantage: Once the DB is populated one can look for different smells / SET / W&I 17-5-2010 PAGE 20 / SET / W&I 17-5-2010 PAGE 21 Another example: Inappropriate interface AbstractTerm cannot be easily extended not clear which subclass should implement terms Query commonsubclint(class,int,subcs) :- classinterface(class,classint), allsubclasses(class,subclist), sharedinterface(subclist,commonint,subcs), difference(commonint,classint,int) How to chose appropriate refactorings? Bad smell Comments Duplicated code Feature envy Long method Refactoring Introduce assertion Extract class Pull Up method Form Template method Move method Decompose conditional Akin to critique tables discussed last week! / SET / W&I 17-5-2010 PAGE 22 / SET / W&I 17-5-2010 PAGE 23 4

Refactoring never comes alone Usually one can find many different bad smells And for each one many different refactorings Guidelines when refactorings should be applied Still even with strict guidelines [DuBois 2004] org.apache.tomcat.{core,startup 12 classes, 167 methods and 3797 lines of code Potential refactorings Extract Method 5 Move Method 9 Replace Method with Method Object 1, Replace Data Value with Object 3 Extract Class 3 Refactoring never comes alone Which one is better? The most beneficial for the maintainability metrics we want to improve We can do this a posteriori but the effort will be lost! So we would like to assess this a priori from multiple methods decreases LOC decreases #dependencies on other classes / SET / W&I 17-5-2010 PAGE 24 / SET / W&I 17-5-2010 PAGE 25 Refactorings and metrics The refactoring process Select the quality metrics maintainability, performance, Recall: Goal Question Metrics! CDE class definition entropy N number of strings in the class f i frequency of the string i CDE DAC number of ADTs in a class WMC WMC/McCabe / SET / W&I 17-5-2010 PAGE 26 = f i fi ln N N Refactoring loop Calculate the metrics value Identify a problem: bad smell Check that the refactoring is applicable Refactor Compile and test Recall: without changing its external behavior Recalculate the metrics value / SET / W&I 17-5-2010 PAGE 27 Inconsistency Interface preservation by refactorings Refactoring can introduce inconsistency In tests by breaking the interfaces In models by making them out-of-date We need to detect such inconsistencies A priori: using classification of refactorings We know when the things will go wrong A posteriori: Using a logic formalism Inconsistency = unsatisfiability of a logic formula Using change logs erose Refactoring can violate the interface Classify refactorings [Moonen et al.] Composite: series of small refactorings, Compatible: interface is not changed Backwards compatible: interface is extended Make backwards compatible: interface can be modified to keep it backwards compatible Incompatible: interface is broken, tests should be adapted / SET / W&I 17-5-2010 PAGE 28 / SET / W&I 17-5-2010 PAGE 29 5

Refactoring and tests But tests are also code! Compatible Backwards compatible Make backwards compatible Incompatible To which group belong Replace Exception with Test Self Encapsulate Field Inline temp Extract class Decompose conditional Push down/pull up field Add/Remove parameter Rename/Move method Inline method Inline class Smells [Moonen et al.] Mystery guest (dependency on an external resource) Resource optimism (availability of resources) Test run war (concurrent use of resources) General fixture (too many things are set-up) Eager test (several methods are tested together) Lazy tests (the same method for the same fixture) Assertions roulette (several assertions in the same test with no distinct explanation) For testers only (production code used only for tests) Sensitive equality (tostring instead of equal) Test code duplication / SET / W&I 17-5-2010 PAGE 30 / SET / W&I 17-5-2010 PAGE 31 Smells are there, what about refactorings? Alternative: A posteriori inconsistency Refactoring Bad smell Inline resource Mystery guest Setup External Resource Resource optimism Make resource unique Test run war Reduce data General fixture Add assertion explanation Assertions roulette Introduce equality method Sensitive equality Sometimes we do not know what refactorings took place Van Der Straeten et al.: inconsistencies in UML models using encoding as logic formulas Similar technique can be used for code/model Syntax adapted: inconsistent(classdiagram,seqdiagram,class,obj) :- class(class), not(innamespace(class,classdiagram)), instanceof(class,obj), innamespace(obj,seqdiagram) / SET / W&I 17-5-2010 PAGE 32 / SET / W&I 17-5-2010 PAGE 33 Putting it all together: refactoring browsers Putting it all together: refactoring browsers IntelliJ IDEA first popular commercial refactoring browser for Java Today: additional languages A number of alternatives Eclipse MS Visual Studio IntelliJ IDEA first popular commercial refactoring browser for Java Today: additional languages Today: a number of alternatives Eclipse MS Visual Studio file:///c:/documents%20and%20settings/aserebre/my %20Documents/Downloads/refactorings8.html / SET / W&I 17-5-2010 PAGE 34 / SET / W&I 17-5-2010 PAGE 35 6

Refactoring browsers have a lot of potential but are they used? How do people refactor [Murphy Hill and Black] Students [Murphy Hill and Black] 16 used Eclipse, 2 used refactoring 42 used Eclipse, 6 used refactoring Professionals 112 agile programmers, 68% used refactoring Traditional programmers are expected to be less enthusiastic Are refactoring browsers fit to what the developers want to do? Floss refactorings: Root canal refactorings: frequent, intertwined concentrated refactoring effort, with usual development infrequent, no usual development activities activites take place Regular flossing prevents root canal treatment Programmers prefer to floss [Weißgerber, Diehl] / SET / W&I 17-5-2010 PAGE 36 / SET / W&I 17-5-2010 PAGE 37 We need to focus on floss refactorings Eclipse revisited 1. Choose the desired refactoring quickly, 2. Switch seamlessly between program editing and refactoring, 3. View and navigate the program code while using the tool, 4. Avoid providing explicit configuration information, and 5. Access all the other tools normally available in the development environment while using the refactoring tool. Lengthy menus: refactoring selection is slow (Req. 1) / SET / W&I 17-5-2010 PAGE 38 / SET / W&I 17-5-2010 PAGE 39 Eclipse revisited Eclipse revisited Configuration dialog (Req. 4) Lengthy menus: interrupts refactoring the regular development selection is slow activities (Req. (Req. 1) 2) and does not allow to view the code (Req. 3). / SET / W&I 17-5-2010 PAGE 40 Configuration Preview dialog (good (Req. idea) 4) but the basic Lengthy menus: interrupts functionality refactoring the regular development is limited: no hover-on selection is slow activities (Req. (Req. documentation 1) 2) and does (Req. not allow 5) to view the code (Req. 3). / SET / W&I 17-5-2010 PAGE 41 7

Fast access means limited choice Proposed solution: Refactoring Cues Which refactorings are actually used? Column refactoring Row developer Colour times used Leaders Rename, move, pull up ModifyParameters ExtractLocalVariable ExtractInterface ExtractConstant Short menu (Req. 1) Switch is easy (Req. 2) Code is visible (Req. 3) Dialog is non-modal (Req. 5) Configuration (Req. 4) is an issue / SET / W&I 17-5-2010 PAGE 42 / SET / W&I 17-5-2010 PAGE 43 No explicit configuration: X-Develop Up: Original source code Down: After the extract method refactoring Default method name: m The name is preselected: the rename method refactoring is intended Conclusion Refactoring a disciplined technique for restructuring code, altering its internal structure without changing its external behavior. Refactoring loop Calculate maintainability metrics Identify a problem: bad smell Check that the refactoring is applicable Refactor Compile and test Recalculate the maintainability metrics Refactoring browsers should better support flossing / SET / W&I 17-5-2010 PAGE 44 / SET / W&I 17-5-2010 PAGE 45 8