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.

Slides:



Advertisements
Similar presentations
Module 7. Simplifying Conditional Expressions Course: Refactoring.
Advertisements

A practical guide John E. Boal TestDrivenDeveloper.com.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 2 Composing Methods Steve Chenoweth Office Phone: (812) Cell: (937)
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.
Steve Chenoweth Office Phone: (812) Cell: (937)
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
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
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.
Chapter 6: Code Refactoring Omar Meqdadi SE 3860 Lecture 6 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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.
Refactoring1 Refactoring DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY February 6, 2009.
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.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
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.
Introduction to Refactoring Jim Cooper Falafel Software.
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.
Refactoring and such ● (Side note) Specialization ● Key terms ● Abstraction, state, persistence and association and their relationship to software development.
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.
NJIT 1 Test Driven Development and Refactoring Larman, Chapter 21.
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 Advanced Software Engineering Dr Nuha El-Khalili.
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.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
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 (5) Simplifying Conditional Expressions.
Code Refactoring Milan Vukoje Soprex SkfOffice2 SkfOffice3 Big5 Quality oriented We are hiring…
Catalog of Refactoring (1) Composing Methods. Code Smells Long methods Dubious temporary variables Dubious methods.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Catalog of Refactoring (6) Making Method Calls Simpler.
Principles and examples
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Low Budget Productions, LLC
Software Construction and Evolution - CSSE 375 Composing Methods
Overview of Eclipse Lectures
Code Smells 1.
Improving the structure of existing code
Refactoring Strategies
Advanced Programing practices
Refactoring.
Presentation transcript:

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 of 6; a and b are factors of ab ● fac·tor·ing – To determine or indicate explicitly the factors of

SE: Factoring ● fac·tor – The individual items that combined together form a complete software system: ● identifiers ● contents of function ● contents of classes and place in inheritance hierarchy ● fac·tor·ing – Determining the items, at design time, that make up a software system

Refactoring ● 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 structure [Fowler'99] ● A program restructuring operation to support the design, evolution, and resuse of object oriented frameworks that preserve the behaviorial aspects of the program [Opdyke'92]

Specifics ● Source to source transformation ● Remain inside the same language, e.g., C++ to C++ ● Does not change the programs behavior ● Originally designed for object-oriented languages, but can also be applied to non- object oriented language features, i.e., functions

Levels of Software Changes ● High Level - – Features to be added to a system – e.g., New feature ● Intermediate Level – Change design (factoring) – e.g., Move a member function ● Low Level – Change lines of code – e.g., Changes in (a least) two classes

Relationship to Design ● Not the same as “cleaning up code” – May cause changes to behavioral aspects – Changes often made in a small context or to entire program ● Key element of entire process in agile methodologies ● Views design as an evolving process ● Strong testing support to preserve behavioral aspects

Quick Examples ● Introduce Explaining Variable ● Rename Method ● Move Method ● Pullup Method ● Change Value to Reference ● Remove Parameter ● Extract Hierarchy

Why: Design Preservation ● Code changes often lead to a loss of the original design ● Loss of design is cumulative: – Difficulties in design comprehension -> – Difficulties in preserving design -> – More rapid decay of design ● Refactoring improves the design of existing code

Why: Comprehension ● Developer's are most concerned with getting the program to work, not about future developers ● Refactoring makes existing code more readable ● Increases comprehension of existing code, leading higher levels of code comprehension ● Often applied in stages

Why: Debugging ● Greater program comprehension leads to easier debugging ● Increased readability leads to the discovery of possible errors ● Understanding gained during debugging can be put back into the code

Why: Faster Programming ● Counterintuitive argument made by Fowler ● Good design is essential for rapid development ● Poor design allows for quick progress, but soon slows the process down – Spend time debugging – Changes take longer as you understand the system and find duplicate code

When? ● Adding Functionality – Comprehension of existing program – Preparation for addition ● Debugging – Comprehension of existing program ● Code Review – Preparation for suggestions to other programmers – Stimulates other ideas

Catalog ● Collected by Fowler ● Refactoring entry composed of: – Name – Summary – Motivation – Mechanics – Examples ● Based on Java

Categories ● Composing Methods – Creating methods out of inlined code ● Moving Features Between Objects – Changing of decisions regarding where to put reponsibilities ● Organizing Data – Make working with data easier

Categories II ● Simplifying Conditional Expressions ● Making Method Calls Simpler – Creating more straightforward interfaces ● Dealing with Generalization – Moving methods around hierarchies ● Big Refactorings – Refactoring for larger purposes

Composing Methods ● Extract Method ● Inline Method ● Inline Temp ● Replace Temp with Query ● Introduce Explaining Variables ● Split Temporary Variable ● Remove Assignments to Parameters ● Replace Method with Method Object ● Substitute Algorithm

Example ● Remove Assignments to Parameters int discount (int inputVal, int quantity, int yearToDate) { if (inputVal > 50) inputVal -= 2;... int discount (int inputVal, int quantity, int yearToDate) { int result = inputVal; if (inputVal > 50) result -= 2;...

Moving Object Features ● Move Method ● Move Field ● Extract Class ● Inline Class ● Hide Delegate ● Remove Middle Man ● Introduce Foreign Method ● Introduce Local Extension

Organizing Data ● Self Encapsulate Field ● Replace Data Value with Object ● Change Value to Reference ● Change Reference to Value ● Replace Array with Object ● Duplicate Observed Data ● Change Unidirectional Association to Bidirectional ● Change Bidirectional Association to Unidirectional

Organizing Data II ● Replace Magic Number with Symbolic Constant ● Encapsulate Field ● Encapsulate Collection ● Replace Record with Data Class ● Replace Type Code with Class ● Replace Type Code with Subclasses ● Replace Type Code with State/Strategy ● Replace Subclass with Fields

Simplifying Conditional Expr. ● Decompose Conditional ● Consolidate Conditional Expression ● Consolidate Duplicate Conditional Fragments ● Remove Control Flag ● Replace Nested Conditional with Guard Clauses ● Replace Conditional with Polymorphism ● Introduce Null Object ● Introduce Assertion

Example ● Decompose Conditional if (date.before (SUMMER_START) || date.after(SUMMER_END)) charge = quantity * _winterRate + _winterServiceCharge; else charge = quantity * _summerRate; if (notSummer(date)) charge = winterCharge(quantity); else charge = summerCharge(quantity);

Simplifying Method Calls ● Rename Method ● Add Parameter ● Remove Parameter ● Seperate Query from Modifier ● Parameterize Method ● Replace Parameter with Explicit Methods ● Preserve Whole Object ● Replace Parameter with Method

Simplying Method Calls II ● Introduce Parameter Object ● Remove Setting Method ● Hide Method ● Replace Constructor with Factory Method ● Encapsulate Downcast ● Replace Error Code with Exception ● Replace Exception with Test

Dealing with Generalization ● Pull Up Field ● Pull Up Method ● Pull Up Constructor Body ● Push Down Method ● Push Down Field ● Extract Subclass ● Extract Superclass ● Extract Interface

Dealing with Generalization II ● Collapse Hierarchy ● Form Template Method ● Replace Inheritance with Delegation ● Replace Delegation with Inheritance

Big Refactorings ● Tease Apart Inheritance – Split an inheritance hierarchy that is doing two jobs at once ● Convert Procedural Design to Objects ● Separate Domain from Presentation – GUI classes that contain domain logic ● Extract Hierarchy – Create a hierarchy of classes from a single class where the single class contains many conditional statements

Convert Procecedural Design ● Take each record type and turn it into a dumb data object with accessors ● Take all procedural code and put it into a single class ● Take each long method and apply Extract Method and the related factorings to break it down. As you break down the procedures use Move Method to move each one to the appropriate dumb data class ● Continue until all behavior is removed from the original class

Extract Method ● Create a new method, and name it after the intention of the method (name it by what it does, not by how it does it) ● Copy the extracted code from the source method into the new target method ● Scan the extracted code for references to any variables that are local in scope to the source method

Extract Method II ● See whether any temporary variables are used only within this extracted code. If so, declare them in the target method as temporary variables ● Look to see whether any local-scope variable are modified by the existing code (See Split Temporary Variable and Replace Temp with Query) ● Pass into the target method as parameters local scope variables that are read from the extracted code

Extract Method III ● Compile when you have dealt with all the locally-scoped variables ● Replace the extracted code in the source method with a call to the target method ● Compile and test

Tools ● Smalltalk Refactoring Browser – Development environment written in Smalltalk – Allows for Smalltalk source code to transform Smalltalk source code – Comments as a first-class part of the language ● XRefactory – Allows standard refactorings for C++

Challenges ● Preservation of documentary structure (comments, white space etc.) ● Processed code (C, C++, etc.) ● Integration with test suite ● Discovery of possible refactorings ● Creation of task-specific refactorings

Limitations ● Tentative list due to lack of experience ● Database – Database schema must be isolated, or schema evolution must be allowed ● Changing Published Interfaces – Interfaces where you do not control all of the source code that uses the interface – Must support both old and new interfaces – Don't publish interfaces unless you have to

Design Patterns ● Source – Design Patterns: Discovered in source designs – Refactorings: Discovered in informal version histories of source code ● Provides standard names – Design Patterns: GOF – Refactorings: Fowler's Catalog

Design Patterns II ● Provides Mechanics – Design Patterns: Static program structure – Refactorings: Algorithmic changes ● Simple to Complex – Design Patterns: Complex patterns are a combination of simpler patterns – Refactorings: Complex refactorings are a combination of simpler refactorings