REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.

Slides:



Advertisements
Similar presentations
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Advertisements

A practical guide John E. Boal TestDrivenDeveloper.com.
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
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.
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)
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
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
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.
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.
Refactoring Improving the structure of existing code Refactoring1.
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.
CSE403 ● Software engineering ● sp12 Week 7-10 MondayTuesdayWednesdayThursdayFriday Reading dueGroups Beta due SectionProgress report due Readings out.
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.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Refactoring1 Improving the structure of existing code.
The effectiveness of refactoring based on a compatibility testing taxonomy and a dependency graph Steve Counsell and Robert Hierons, Brunel University,
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Object Oriented Software Development
Informatics 122 Software Design II
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
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.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
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.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring: Improving the Quality of Existing Code
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
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.
Catalog of Refactoring (6) Making Method Calls Simpler.
Principles and examples
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Inheritance and Polymorphism
Behavioral Design Patterns
Code Smells 1.
Improving the structure of existing code
Refactoring Types Blake Duncan.
CIS 199 Final Review.
Refactoring.
Presentation transcript:

REFACTORINGREFACTORING

Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies are code-centered, resulting in more frequent code changes Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies are code-centered, resulting in more frequent code changes

Origin of Refactoring Martin Fowler is the thought-leader on this topic His definition of refactoring: “a change made to the internal structure of the software to make it easier to understand and cheaper to modify without changing its observable behavior.” Martin Fowler is the thought-leader on this topic His definition of refactoring: “a change made to the internal structure of the software to make it easier to understand and cheaper to modify without changing its observable behavior.”

Reasons to Refactor Code is duplicated A routine is too long A loop is too long or too deeply nested A class has poor cohesion A class interface does not provide a consistent level of abstraction Code is duplicated A routine is too long A loop is too long or too deeply nested A class has poor cohesion A class interface does not provide a consistent level of abstraction

Reasons to Refactor (cont’d) A parameter list has too many parameters Changes within a class tend to be compartmentalized Changes require parallel modifications to multiple classes Inheritance hierarchies have to be modified in parallel A parameter list has too many parameters Changes within a class tend to be compartmentalized Changes require parallel modifications to multiple classes Inheritance hierarchies have to be modified in parallel

Reasons to Refactor (cont’d) Case statements have to be modified in parallel Related data items that are used together are not organized into classes A routine uses more features of another class than of its own class A primitive data type is overloaded Case statements have to be modified in parallel Related data items that are used together are not organized into classes A routine uses more features of another class than of its own class A primitive data type is overloaded

Reasons to Refactor (cont’d) A class doesn’t do very much A chain of routines passes tramp data A middleman object doesn’t do anything One class is overly intimate with another A routine has a poor name A class doesn’t do very much A chain of routines passes tramp data A middleman object doesn’t do anything One class is overly intimate with another A routine has a poor name

Reasons to Refactor (cont’d) Data members are public A subclass uses only a small percentage of its parent’s routines Comments are used to explain difficult code Global variables are used A routine uses setup code before a method call or takedown code after a method call Data members are public A subclass uses only a small percentage of its parent’s routines Comments are used to explain difficult code Global variables are used A routine uses setup code before a method call or takedown code after a method call

Reasons to Refactor (cont’d) A program contains code that seems like it might be needed someday

Data-level Refactorings Replace a magic number with a named constant Rename a variable with a clearer or more informative name Move an expression inline Replace an expression with a method Introduce an intermediate variable Replace a magic number with a named constant Rename a variable with a clearer or more informative name Move an expression inline Replace an expression with a method Introduce an intermediate variable

Data-level Refactorings (cont’d) Convert a multi-use variable to multiple single-use variables Use a local variable for local purposes rather that as a parameter Convert a data primitive to a class Convert a set of type codes to a class or an enumeration Convert a multi-use variable to multiple single-use variables Use a local variable for local purposes rather that as a parameter Convert a data primitive to a class Convert a set of type codes to a class or an enumeration

Data-level Refactorings (cont’d) Convert a set of type codes to a class with subclasses Change an array to an object Encapsulate a collection Replace a traditional record with a data class Convert a set of type codes to a class with subclasses Change an array to an object Encapsulate a collection Replace a traditional record with a data class

Statement-level Refactorings Decompose a boolean expression Move a complex boolean expression into a well-named boolean function Consolidate fragments that are duplicated within different parts of a conditional Use break or return instead of a loop control variable Decompose a boolean expression Move a complex boolean expression into a well-named boolean function Consolidate fragments that are duplicated within different parts of a conditional Use break or return instead of a loop control variable

Statement-level Refactorings (cont’d) Return as soon as you know the answer instead of assigning a return value with nested if-then-else statements Replace conditionals (especially repeated case statements) with polymorphism Create and use null objects instead of testing for null values Return as soon as you know the answer instead of assigning a return value with nested if-then-else statements Replace conditionals (especially repeated case statements) with polymorphism Create and use null objects instead of testing for null values

Routine-level Refactorings Extract routine/extract method Move a routine’s code inline Convert a long routine into a class Substitute a simple algorithm for a complex algorithm Extract routine/extract method Move a routine’s code inline Convert a long routine into a class Substitute a simple algorithm for a complex algorithm

Routine-level Refactorings (cont’d) Add a parameter Remove a parameter Separate query operations from modification operations Combine similar routines by parameterizing them Add a parameter Remove a parameter Separate query operations from modification operations Combine similar routines by parameterizing them

Routine-level Refactorings (cont’d) Separate routines whose behavior depends on parameters passed in Pass a whole object rather than specific fields Pass specific fields rather than a whole object Encapsulate downcasting Separate routines whose behavior depends on parameters passed in Pass a whole object rather than specific fields Pass specific fields rather than a whole object Encapsulate downcasting

Class Implementation Refactorings Change value objects to reference objects Change reference objects to value objects Replace virtual routines with data initialization Change value objects to reference objects Change reference objects to value objects Replace virtual routines with data initialization

Class Implementation Refactorings (cont’d) Change member routine or data placement –Pull a routine up into its superclass –Pull a field up into its superclass –Push a routine down into its derived class –Push a field down into it derived class –Push a constructor body down into is derived class Change member routine or data placement –Pull a routine up into its superclass –Pull a field up into its superclass –Push a routine down into its derived class –Push a field down into it derived class –Push a constructor body down into is derived class

Class Implementation Refactorings (cont’d) Extract specialized code into a subclass Combine similar code into a superclass Extract specialized code into a subclass Combine similar code into a superclass

Class Interface Refactorings Move a routine to another class Convert one class to two Eliminate a class Hide a delegate Remove a middleman Move a routine to another class Convert one class to two Eliminate a class Hide a delegate Remove a middleman

Class Interface Refactorings (cont’d) Replace inheritance with delegation Replace delegation with inheritance Introduce a foreign routine Introduce an extension class Encapsulate an exposed member variable Replace inheritance with delegation Replace delegation with inheritance Introduce a foreign routine Introduce an extension class Encapsulate an exposed member variable

Class Interface Refactorings (cont’d) Remove Set() routines for field that cannot be changed Hide routines that are not intended to be used outside the class Encapsulate unused routines Collapse a superclass and subclass if their implementations are very similar Remove Set() routines for field that cannot be changed Hide routines that are not intended to be used outside the class Encapsulate unused routines Collapse a superclass and subclass if their implementations are very similar

System-Level Refactorings Create a definitive data source for data you can’t control Change unidirectional class association to bidirectional class association Change bidirectional class association to unidirectional class association Provide a factory method rather than a simple constructor Repalce error codes with exceptions or vice versa Create a definitive data source for data you can’t control Change unidirectional class association to bidirectional class association Change bidirectional class association to unidirectional class association Provide a factory method rather than a simple constructor Repalce error codes with exceptions or vice versa

Refactoring Safely Save the code you start with Keep refactorings small Do refactorings one at a time Make a list of steps you intend to take Make a parking lot Make frequent checkpoints Save the code you start with Keep refactorings small Do refactorings one at a time Make a list of steps you intend to take Make a parking lot Make frequent checkpoints

Refactoring Safely (cont’d) Use your compiler warnings Retest Add test cases Review the changes Adjust your approach depending on the risk level of the refactoring Use your compiler warnings Retest Add test cases Review the changes Adjust your approach depending on the risk level of the refactoring

When Not to Refactor Don’t use refactoring as a cover for code and fix –This is a misuse of refactoring –Refactoring applies to changes in working code Avoid refactoring instead of rewriting –Should the code be tossed out and completely redesigned and rewritten? Don’t use refactoring as a cover for code and fix –This is a misuse of refactoring –Refactoring applies to changes in working code Avoid refactoring instead of rewriting –Should the code be tossed out and completely redesigned and rewritten?

Refactoring Strategies Focus on the refactorings that produce the highest level of benefit Refactor when you add a method Refactor when you add a class Refactor when you fix a defect Focus on the refactorings that produce the highest level of benefit Refactor when you add a method Refactor when you add a class Refactor when you fix a defect

Refactoring Strategies (cont’d) Target error-prone modules Target highest complexity modules In a maintenance environment, improve the parts you touch Define an interface between clean code and ugly code, and then move code across the interface Target error-prone modules Target highest complexity modules In a maintenance environment, improve the parts you touch Define an interface between clean code and ugly code, and then move code across the interface

Refactoring Legacy Systems Any time you touch messy code in the legacy system: –Bring it across to the ideal side of the interface –Bring the code up to current coding standards –Provide clear variable names –Create classes with high cohesion –Etc. Any time you touch messy code in the legacy system: –Bring it across to the ideal side of the interface –Bring the code up to current coding standards –Provide clear variable names –Create classes with high cohesion –Etc.