Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.

Slides:



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

A practical guide John E. Boal TestDrivenDeveloper.com.
Software Testing and Maintenance 1 Today’s Agenda  Course Evaluation  HW 4 Return  HW 5 Correction  Quiz 4 Next Class  Software Refactoring.
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 3 Moving Features Between Objects Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Software Engineering Refactoring Software Engineering 2011 Department of Computer Science Ben-Gurion university Based on slides of: Mira Balaban.
A Small Exercise Suppose you need to implement a movie rental system Your movie rental system must deal with Three kind of movie rental Three kind of movie.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
CPSC150 Abstract Classes and Interfaces Chapter 10.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Refactoring, continue. Announcements Due today Resubmit HW5 (feedback on HW5 in SVN) HW7 HW8 will be up tonight Due Saturday at 5:59! Quiz 9 Spring 15.
Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,
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.
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.
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design State Pattern.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Software Engineering Refactoring Software Engineering 2012 Department of Computer Science Ben-Gurion university Based on slides of: Mira Balaban.
CS4723 Software Engineering Lecture 12 Software Design Quality.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Agile Software Development Lab Spring 2008 R O O T S 1 Sina Thomas ) XP: Extreme.
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
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.
Today’s Agenda  More refactoring patterns Software Testing and Maintenance 1.
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.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
Design Patterns.
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)
Refactoring. Announcements HW7 due today, HW8 coming up tomorrow (I’m taking a late day on posting HW8) Grades and feedback for HW0-5 in Homework Server.
Session 6 Comments on Lab 3 & Implications of Inheritance.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
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.
Refactoring Improving code after it has been written.
Module 9. Dealing with Generalization Course: Refactoring.
Catalog of Refactoring (1) Composing Methods. Code Smells Long methods Dubious temporary variables Dubious methods.
ICONFINDER ICONFINDER Founded Django based web application -PostgreSQL -Elasticsearch -Amazon Elastic Compute.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Module Road Map Refactoring Why Refactoring? Examples
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Inheritance and Polymorphism
CS 350 – Software Design The Strategy Pattern – Chapter 9
Testing and Debugging.
Outline A. What is Refactoring? B. Why do we Refactor?
A Very Common Series of Techniques
Refactoring and Code Smells
Overview of Eclipse Lectures
Refactoring.
Reusability 11/29/2018© 2006 ITT Educational Services Inc.
private double amountFor(Rental aRental)
CSE3311 Software Design State Pattern.
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Refactoring and Code Smells
Refactoring and Code Smells
CS 325: Software Engineering
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring (1)

Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes easier Simpler, cleaner, and more performant code

Refactoring is Changing Working Code! Management resistance If time is spent “cleaning up” working code, less time is spent implementing useful features. Risky It is risky to change working code as it may introduce new bugs. One risks lose sight of the original goal while keep changing the code

What is Refactoring The process of changing a software system in such a way that it does not alter the external behavior of the code yet improves it internal structure. Disciplined way to clean up code Minimizes the changes of introducing bugs.

Refactoring as Patterns Principled Systemmatic Incremental, simple steps Safe

First Example A program to calculate and print a statement of a customer's charges at a video rental store. Movies Type of movies: regular, children's, new releases Frequent renter points

Initial Class Diagram

What's your impression of the code?

User Needs Changed! Want the statement shown on the Web with nice HTML formatting Charging rule is changed Movie classification is changed, the changes affect the charging rule and the points calculation...

First Step in Refactoring Write solid tests! Tests must be self checking Either pass test Or print detailed list of failures

Decomposing and redistributing statement() Find logic chunk of code and extract them into its own method switch statement is a good candidate  Extract to amountFor private method Find local variables  Not modified: pass in as parameter in new method each  Modified: if only one, return it ThisAmount Compile and test

Renaming Variables in Extracted Methods Good names communicate intention A key to clear code Changed: each -> aRental thisAmount -> result

Moving Amount Calculation amountFor method doesn't use information from Customer, so it shouldn't be in Customer class It uses information from Rental Move the code over Rename it getCharge Change customer.amountFor to test it If test passes, replace amountFor with getCharge everywhere Since it is not public, otherwise, leave the delegation

Extract Frequent Renter Points Again look for local variables Find the appropriate place to put the method frequentRenterPoints += each.getFrequentRenterPoints();

Remove Temporary Variables Temps encourage complex and long methods Replace them with new method calls thisAmount totalAmount frequentRenterPoints Trade-off with performance

Reuse of New Methods public String htmlStatement() { Enumeration rentals = _rentals.elements(); String result = " Rentals for " + getName() + " </ H1> \n"; while (rentals.hasMoreElements()) { Rental each = (Rental) rentals.nextElement(); //show figures for each rental result += each.getMovie().getTitle()+ ": " + String.valueOf(each.getCharge()) + " \n"; } //add footer lines result += " You owe " + String.valueOf(getTotalCharge()) + " \n"; result += "On this rental you earned " + String.valueOf(getTotalFrequentRenterPoints()) + " frequent renter points "; return result; }

Replace Conditional with Polymorphism The big switch is bad What if the classification movie type changes? Not good to witch on other object's data Move getCharge to Movie instead Do the same for getFrequentRenderPonits Now things varies with the type are in the type Ready for Movie inheritance?

What about this? Doesn't work well, as a movie's classification may be changed (e.g. from new release to regular)

Use State Pattern Add abstraction of Price Enable runtime change

Step One Make sure all the use of type code goes through getter / setter Create the Price classes abstract class Price { abstract int getPriceCode();} class ChildrensPrice extends Price { int getPriceCode() { return Movie.CHILDRENS; } class NewReleasePrice extends Price { int getPriceCode() { return Movie.NEW_RELEASE; } class RegularPrice extends Price { int getPriceCode() { return Movie.REGULAR; }

Step Two Change movie's accessors for the price code to use the new Price class public void setPriceCode(int arg) { switch (arg) { case REGULAR: _price = new RegularPrice(); break; case CHILDRENS: _price = new ChildrensPrice(); break; case NEW_RELEASE: _price = new NewReleasePrice(); break; default: throw new IllegalArgumentException("Incorrect Price Code"); }

Step Three Move each case to its corresponding concrete classes class RegularPrice... double getCharge(int daysRented){ double result = 2; if (daysRented > 2) result += (daysRented - 2) * 1.5; return result; }...