Refactoring. In essence we improve the design of the code after it has been written. That's an odd turn of phrase.

Size: px
Start display at page:

Download "Refactoring. In essence we improve the design of the code after it has been written. That's an odd turn of phrase."

Transcription

1 Refactoring Process of changing software in such a way that it does not alter the external behavior of the code yet improves its internal structure. Disciplined way to clean up code that minimizes the chances of introducing bugs. In essence we improve the design of the code after it has been written. That's an odd turn of phrase. Well good design comes first, and the coding comes second. Over time the code will be modified, and its structure according to that design, gradually fades. The code slowly sinks from engineering to hacking. Refactoring is the opposite of this practice. With refactoring you can take a bad design, chaos even, and rework it into well-designed code.

2 Example A program to calculate and print a statement of a customer's charges at a video store. The program is told which movies a customer rented and for how long. It then calculates the charges, which depend on how long the movie is rented, and identifies the type movie. There are three kinds of movies: regular, children's, and new releases. In addition to calculating charges, the statement also computes frequent renter points, which vary depending on whether the film is a new release.

3 Some design

4 Some code that works Movie public class Movie { public static final int CHILDRENS = 2; public static final int REGULAR = 0; public static final int NEW_RELEASE = 1; private String _title; private int _pricecode; public Movie(String title, int pricecode) { _title = title; _pricecode = pricecode; public int getpricecode() { return _pricecode; public void setpricecode(int arg) { _pricecode = arg; public String gettitle (){ return _title; ;

5 class Rental { private Movie _movie; private int _daysrented; Rental and Customer public Rental(Movie movie, int daysrented) { _movie = movie; _daysrented = daysrented; public int getdaysrented() { return _daysrented; public Movie getmovie() { return _movie; class Customer { private String _name; private Vector _rentals = new Vector(); public Customer (String name){ _name = name; ; public void addrental(rental arg) { _rentals.addelement(arg); public String getname (){ return _name; ;

6 Statement method in Customer class Customer { public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { double thisamount = 0; Rental each = (Rental) rentals.nextelement(); //determine amounts for each line switch (each.getmovie().getpricecode()) { case Movie.REGULAR: thisamount += 2; if (each.getdaysrented() > 2) thisamount += (each.getdaysrented() - 2) * 1.5;

7 case Movie.NEW_RELEASE: thisamount += each.getdaysrented() * 3; case Movie.CHILDRENS: thisamount += 1.5; if (each.getdaysrented() > 3) thisamount += (each.getdaysrented() - 3) * 1.5; // add frequent renter points frequentrenterpoints ++; // add bonus for a two day new release rental if ((each.getmovie().getpricecode() == Movie.NEW_RELEASE) && each.getdaysrented() > 1) frequentrenterpoints ++; //show figures for this rental result += "\t" + each.getmovie().gettitle()+ "\t" + thisamount + "\n"; totalamount += thisamount; //end of while

8 //add footer lines result += "Amount owed is " + totalamount + "\n"; result += "You earned " + frequentrenterpoints + " frequent renter points"; return result; What are your impressions about the design of this program?

9 Comments Well, not well designed and certainly not object oriented. That long statement routine in the Customer class does far too much. Many of the things that it does should really be done by the other classes. Even so the program works! So, is it just about aesthetics? It is until we want to change the system. The compiler doesn't care whether the code is ugly or clean. But when we change the system, there is a human involved, and humans do care. A poorly designed system is hard to change. Hard because it is hard to figure out where the changes are needed. If so, there is a strong chance that the programmer will make a mistake and introduce bugs.

10 Change comes Now they want a statement printed in HTML so that the statement can be Web enabled and fully buzzword compliant What s the impact of this change? It s impossible to reuse any of the behavior of the current statement method for an HTML statement. The only recourse is to write a whole new method that duplicates much of the behavior of statement. Just copy the statement method and make whatever changes you need?! But what happens when the charging rules change? We have to fix both statement and htmlstatement and ensure the fixes are consistent.

11 First step Find a logical clump of code and use Extract Method. An obvious piece here is the switch statement. It would make a good chunk to extract into its own method. If we do the extraction badly, we could introduce a bug into the program. How to do it safely? First look for any variables that are local in scope to the method we are looking at, i.e. local variables and parameters. In this case there are: each and thisamount. each is not modified by the code thisamountis modified. Any non-modified variable we can pass in as a parameter. Modified variables need more care. If there is only one, we can return it.

12 public String statement() { double totalamount = 0; Before int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { double thisamount = 0; Rental each = (Rental) rentals.nextelement(); //determine amounts for each line switch (each.getmovie().getpricecode()) { case Movie.REGULAR: thisamount += 2; if (each.getdaysrented() > 2) thisamount += (each.getdaysrented() - 2) * 1.5; case Movie.NEW_RELEASE: thisamount += each.getdaysrented() * 3; case Movie.CHILDRENS: thisamount += 1.5; if (each.getdaysrented() > 3) thisamount += (each.getdaysrented() - 3) * 1.5;

13 public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { double thisamount = 0; Rental each = (Rental) rentals.nextelement(); thisamount = amountfor(each); After private int amountfor(rental each) { int thisamount = 0; switch (each.getmovie().getpricecode()) { case Movie.REGULAR: thisamount += 2; if (each.getdaysrented() > 2) thisamount += (each.getdaysrented() - 2) * 1.5; case Movie.NEW_RELEASE: thisamount += each.getdaysrented() * 3; case Movie.CHILDRENS: thisamount += 1.5; if (each.getdaysrented() > 3) thisamount += (each.getdaysrented() - 3) * 1.5; return thisamount;

14 Tests Whenever we make a change like this, we compile and test. We didn't get off to a very good start We did the return type an int as opposed to double Why is this important? private double amountfor(rental each) { double thisamount = 0; switch (each.getmovie().getpricecode()) { Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug.

15 Variable renaming Some of the of the variable names in amountfor could be better. private double amountfor(rental each) { double thisamount = 0; switch (each.getmovie().getpricecode()) { case Movie.REGULAR: thisamount += 2; if (each.getdaysrented() > 2) thisamount += (each.getdaysrented() - 2) * 1.5; case Movie.NEW_RELEASE: thisamount += each.getdaysrented() * 3; case Movie.CHILDRENS: thisamount += 1.5; if (each.getdaysrented() > 3) thisamount += (each.getdaysrented() - 3) * 1.5; return thisamount; private double amountfor(rental arental) { double result = 0; switch (arental.getmovie().getpricecode()) { case Movie.REGULAR: result += 2; if (arental.getdaysrented() > 2) result += (arental.getdaysrented() - 2) * 1.5; case Movie.NEW_RELEASE: result += arental.getdaysrented() * 3; case Movie.CHILDRENS: result += 1.5; if (arental.getdaysrented() > 3) result += (arental.getdaysrented() - 3) * 1.5; return result;

16 Shifting responsibilities As we look at amountfor, we can see that it uses information from the Rental, but doesn t use information from the Customer. private double amountfor(rental arental) { double result = 0; switch (arental.getmovie().getpricecode()) { case Movie.REGULAR: result += 2; if (arental.getdaysrented() > 2) result += (arental.getdaysrented() - 2) * 1.5; case Movie.NEW_RELEASE: result += arental.getdaysrented() * 3; case Movie.CHILDRENS: result += 1.5; if (arental.getdaysrented() > 3) result += (arental.getdaysrented() - 3) * 1.5; return result; Method is in the wrong object! A method should be on the object whose data it uses. This method should be moved to the Rental. To do this we use Move Method.

17 amountfor() getcharge() in Rental class Rental... double getcharge() { double result = 0; switch (getmovie().getpricecode()) { case Movie.REGULAR: result += 2; if (getdaysrented() > 2) result += (getdaysrented() - 2) * 1.5; case Movie.NEW_RELEASE: result += getdaysrented() * 3; case Movie.CHILDRENS: result += 1.5; if (getdaysrented() > 3) result += (getdaysrented() - 3) * 1.5; return result; In this case fitting into its new home means removing the parameter. I also renamed the method as I did the move.

18 Next? I can now test to see whether this method works. To do this I replace the body of Customer.amountFor to delegate to the new method. class Customer... private double amountfor(rental arental) { return arental.getcharge(); I can now compile and test to see whether I've broken anything.

19 Replace references to old method The next step is to find every reference to the old method and adjust the reference to use the new method, as follows. class Customer... public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { double thisamount = 0; Rental each = (Rental) rentals.nextelement(); class Customer... public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { double thisamount = 0; Rental each = (Rental) rentals.nextelement(); thisamount = amountfor(each); thisamount = each.getcharge();

20 thisamount? public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { double thisamount = 0; Rental each = (Rental) rentals.nextelement(); thisamount = each.getcharge(); // add frequent renter points frequentrenterpoints ++; // add bonus for a two day new release rental if ((each.getmovie().getpricecode() == Movie.NEW_RELEASE) && each.getdaysrented() > 1) frequentrenterpoints ++; //show figures for this rental result += "\t" + each.getmovie().gettitle()+ "\t" + String.valueOf(thisAmount) + "\n"; totalamount += thisamount;

21 thisamount? //add footer lines result += "Amount owed is " + totalamount + "\n"; result += "You earned " + frequentrenterpoints + " frequent renter points"; return result; thisamount is now redundant. It is set to the result of each.charge and not changed afterward. Thus we can eliminate thisamount by using Replace Temp with Query. i.e. replace it by each.getcharge(). It s advisable we get rid of temporary variables as much as possible. We can easily lose track of what they are there for. They are particularly insidious in long methods. Of course there is a performance price to pay; here the charge is now calculated twice. But it is easy to optimize that in the rental class, and we can optimize much more effectively when the code is properly factored.

22 More refactoring: frequentrenterpoints public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { Rental each = (Rental) rentals.nextelement(); // add frequent renter points frequentrenterpoints ++; // add bonus for a two day new release rental if ((each.getmovie().getpricecode() == Movie.NEW_RELEASE) && each.getdaysrented() > 1) frequentrenterpoints ++; Reasonable to put the responsibility on the Rental.

23 class Customer... public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { Rental each = (Rental) rentals.nextelement(); frequentrenterpoints += each.getfrequentrenterpoints(); class Rental... int getfrequentrenterpoints() { if ((getmovie().getpricecode() == Movie.NEW_RELEASE) && getdaysrented() > 1) return 2; else return 1;

24 Removing Temps class Customer... public String statement() { double totalamount = 0; int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { Rental each = (Rental) rentals.nextelement(); frequentrenterpoints += each.getfrequentrenterpoints(); //show figures for this rental result += "\t" + each.getmovie().gettitle()+ "\t" + String.valueOf(each.getCharge()) + "\n"; totalamount += each.getcharge(); //add footer lines result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"; return result;

25 class Customer... Removing totalamount public String statement() { int frequentrenterpoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getname() + "\n"; while (rentals.hasmoreelements()) { Rental each = (Rental) rentals.nextelement(); frequentrenterpoints += each.getfrequentrenterpoints(); //show figures for this rental result += "\t" + each.getmovie().gettitle()+ "\t" + String.valueOf(each.getCharge()) + "\n"; //add footer lines result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n"; result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"; return result; private double gettotalcharge() { double result = 0; Enumeration rentals = _rentals.elements(); while (rentals.hasmoreelements()) { Rental each = (Rental) rentals.nextelement(); result += each.getcharge(); return result;

Refactoring: Improving the Design of Existing Code

Refactoring: Improving the Design of Existing Code Refactoring: Improving the Design of Existing Code Martin Fowler fowler@acm.org www.martinfowler.com www.thoughtworks.com What is Refactoring A series of small steps, each of which changes the program

More information

Lecture 12, part 2: Refactoring. NCCU Fall 2005 Dec. 13, 2005

Lecture 12, part 2: Refactoring. NCCU Fall 2005 Dec. 13, 2005 Lecture 12, part 2: Refactoring NCCU Fall 2005 Dec. 13, 2005 1 Refactoring 什? 不 行 理 降 行 不 Unit Testing! Martin Fowler (and Kent Beck, John Brant, William Opdyke, Don Roberts), Refactoring- Improving the

More information

Software Engineering Refactoring

Software Engineering Refactoring Software Engineering Refactoring Software Engineering 2012-2013 Department of Computer Science Ben-Gurion university Based on slides of: Mira Balaban Department of Computer Science Ben-Gurion university

More information

Code Refactoring. CS356 Object-Oriented Design and Programming November 21, 2014

Code Refactoring. CS356 Object-Oriented Design and Programming   November 21, 2014 Code Refactoring CS356 Object-Oriented Design and Programming http://cs356.yusun.io November 21, 2014 Yu Sun, Ph.D. http://yusun.io yusun@csupomona.edu The Problem: Software Drift Over many phases of maintenance,

More information

Refactoring, Part 2. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 12/01/09. University of Colorado, 2009

Refactoring, Part 2. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 12/01/09. University of Colorado, 2009 Refactoring, Part 2 Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 12/01/09 University of Colorado, 2009 1 Introduction Credit where Credit is Due Some of the material for

More information

Introduction to Refactoring

Introduction to Refactoring Introduction to Refactoring Sutee Sudprasert 1 Credits Refactoring : Improving the design of existing code - Martin Fowler Design Patterns - GOF 2 What is refactoring? Refactoring is the process of changing

More information

Refactoring Exercise

Refactoring Exercise Refactoring Exercise Maria Grazia Pia INFN Genova, Italy Maria.Grazia.Pia@cern.ch http://www.ge.infn.it/geant4/training/apc2017/ Exercise: The Video Store Grab basic concepts Get into the habit of refactoring

More information

Example of OO Design Refactoring

Example of OO Design Refactoring Example of OO Design Refactoring Robert B. France Colorado State University Fort Collins Colorado Robert B. France 1 What is design refactoring? Often an initial design is not a good design Design may

More information

CS247. Today s topics. Design matters. What is refactoring?

CS247. Today s topics. Design matters. What is refactoring? Today s topics CS247 Refactoring Adapted from Martin Fowler s text and Mike Godfrey s slides. What is refactoring and why do we care? The rule of three. A simple case study that applies some refactorings.

More information

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

Refactoring. Software Engineering, DVGC18 Faculty of Economic Sciences, Communication and IT Eivind Nordby Refactoring Faculty of Economic Sciences, Communication and IT 2011-09-21 Why Refactor Refactoring is one factor in keeping your system in shape Without continuous refactoring, the system will rot It takes

More information

Refactoring. Chen Tang March 3, 2004

Refactoring. Chen Tang March 3, 2004 Refactoring Chen Tang March 3, 2004 What Is Refactoring (Definition) Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet

More information

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

AntiPatterns. EEC 421/521: Software Engineering. AntiPatterns: Structure. AntiPatterns: Motivation AntiPatterns EEC 421/521: Software Engineering Definition: An AntiPattern describes a commonly occurring solution to a problem that generates decidedly negative consequences Refactoring Reference: Refactoring

More information

On the automation of challenging refactorings through advanced method extraction techniques

On the automation of challenging refactorings through advanced method extraction techniques On the automation of challenging refactorings through advanced method extraction techniques Ran Ettinger (RanE@il.ibm.com), IBM Haifa Research Lab In Software Engineering Course, Ben-Gurion University

More information

On the automation of challenging refactorings through advanced method extraction techniques

On the automation of challenging refactorings through advanced method extraction techniques On the automation of challenging refactorings through advanced method extraction techniques Ran Ettinger (RanE@il.ibm.com, https://researcher.ibm.com/researcher/view.php?person=il-rane) IBM Research Haifa

More information

Refactoring 101. By: Adam Culp

Refactoring 101. By: Adam Culp By: Adam Culp Twitter: @adamculp https://joind.in/14927 2 About me PHP 5.3 Certified Consultant at Zend Technologies Organizer SoFloPHP (South Florida) Organized SunshinePHP (Miami) Long distance (ultra)

More information

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

Objectives: On completion of this project the student should be able to: ENGI-0655/5232 Software Construction and Evolution Project 1 Reverse Engineering Refactoring & Object Oriented Design Due date November 10, 2009-4:00 pm 1. Aims The aim of this project is to give you more

More information

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

void printowing(double amount) { printbanner(); printdetails(); void printdetails(double amount) { Refactoring References: Martin Fowler, Refactoring: Improving the Design of Existing Code; ; Bruce Wampler, The Essence of Object-Oriented Oriented Programming with Java and UML A recent OO technique that

More information

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

MSO Refactoring. Hans Philippi. October 2, Refactoring 1 / 49 MSO Refactoring Hans Philippi October 2, 2018 Refactoring 1 / 49 This lecture What is refactoring?... or how to deal with the horrible code your colleagues have created... or how to deal with the horrible

More information

PRÁCTICO 1: MODELOS ESTÁTICOS INGENIERÍA INVERSA DIAGRAMAS DE CLASES

PRÁCTICO 1: MODELOS ESTÁTICOS INGENIERÍA INVERSA DIAGRAMAS DE CLASES PRÁCTICO 1: MODELOS ESTÁTICOS INGENIERÍA INVERSA DIAGRAMAS DE CLASES Una parte importante dentro del proceso de re-ingeniería de un sistema es la ingeniería inversa del mismo, es decir, la obtención de

More information

Refactoring. Refactoring Techniques

Refactoring. Refactoring Techniques Refactoring Refactoring Techniques Code Quality is Important! Refactoring is... A disciplined technique for restructuring an existing body of code, altering its internal structure without changing its

More information

INF5120 Modellbasert Systemutvikling. F10-2: Architectural Patterns, Design Patterns and Refactoring. Lecture Arne-Jørgen Berre

INF5120 Modellbasert Systemutvikling. F10-2: Architectural Patterns, Design Patterns and Refactoring. Lecture Arne-Jørgen Berre INF5120 Modellbasert Systemutvikling F10-2: Architectural Patterns, Design Patterns and Refactoring Lecture 28.03.2011 2011 Arne-Jørgen Berre Patterns: From Analysis to Implementation Analysis Design Implementation

More information

INF5120 Modellbasert Systemutvikling

INF5120 Modellbasert Systemutvikling INF5120 Modellbasert Systemutvikling F07-2: Architectural Patterns, Design Patterns and Refactoring Lecture 27.02.2017 Arne-Jørgen Berre Patterns: From Analysis to Implementation Analysis Design Implementation

More information

Refactoring Without Ropes

Refactoring Without Ropes Refactoring Without Ropes Roger Orr OR/2 Limited The term 'refactoring' has become popular in recent years; but how do we do it safely in actual practice? Refactoring... Improving the design of existing

More information

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change Chapter01.fm Page 1 Monday, August 23, 2004 1:52 PM Part I The Mechanics of Change The Mechanics of Change Chapter01.fm Page 2 Monday, August 23, 2004 1:52 PM Chapter01.fm Page 3 Monday, August 23, 2004

More information

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson More on Design CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson Outline Additional Design-Related Topics Design Patterns Singleton Strategy Model View Controller Design by

More information

Refactoring. Section (JIA s) OTHER SOURCES

Refactoring. Section (JIA s) OTHER SOURCES Refactoring Section 7.2.1 (JIA s) OTHER SOURCES Code Evolution Programs evolve and code is NOT STATIC Code duplication Outdated knowledge (now you know more) Rethink earlier decisions and rework portions

More information

MSO Lecture 6. Wouter Swierstra. September 24, 2015

MSO Lecture 6. Wouter Swierstra. September 24, 2015 1 MSO Lecture 6 Wouter Swierstra September 24, 2015 2 LAST LECTURE Case study: CAD/CAM system What are design patterns? Why are they important? What are the Facade and Adapter patterns? 3 THIS LECTURE

More information

Refactoring. Improving the Design of Existing Code. Second Edition. Martin Fowler with contributions by Kent Beck

Refactoring. Improving the Design of Existing Code. Second Edition. Martin Fowler with contributions by Kent Beck Refactoring Improving the Design of Existing Code Second Edition Martin Fowler with contributions by Kent Beck Contents at a Glance Foreword to the First Edition Preface Chapter 1: Refactoring: A First

More information

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

Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Refactoring April 24, 2007 Lots of material taken from Fowler, Refactoring: Improving the Design of Existing Code 1 Evolving Software

More information

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

Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004 CMSC 433 Programming Language Fall 2004 Project 3 coming out Midterm October 28 Administrivia Refactoring October 14, 2004 Lots of material taken from Fowler, Refactoring: Improving the Design of Existing

More information

Reverse Engineering and Refactoring Related Concept in Software Engineering

Reverse Engineering and Refactoring Related Concept in Software Engineering Reverse Engineering and Refactoring Related Concept in Software Engineering Ajit kumar and Navin kumar. Research scholars. B. R. Ambedkar Bihar University. Muzaffarpur, Bihar (India). Guide:-Dr.D.L.Das.

More information

McCa!"s Triangle of Quality

McCa!s Triangle of Quality McCa!"s Triangle of Quality Maintainability Portability Flexibility Reusability Testability Interoperability PRODUCT REVISION PRODUCT TRANSITION PRODUCT OPERATION Correctness Usability Reliability Efficiency

More information

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

Software Design COSC 4353/6353 D R. R A J S I N G H Software Design COSC 4353/6353 D R. R A J S I N G H Week 5 Refactoring What is Refactoring? Code Smells Why Refactoring? Techniques IDEs What is Refactoring? Art of improving the design of existing code

More information

COURSE 11 DESIGN PATTERNS

COURSE 11 DESIGN PATTERNS COURSE 11 DESIGN PATTERNS PREVIOUS COURSE J2EE Design Patterns CURRENT COURSE Refactoring Way refactoring Some refactoring examples SOFTWARE EVOLUTION Problem: You need to modify existing code extend/adapt/correct/

More information

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

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture Credit where Credit is Due Lecture 25: Refactoring Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 6448 - Spring Semester, 2002 Some of the material for this lecture and lecture 26 is taken

More information

Refactoring. George Dinwiddie idia Computing, LLC

Refactoring. George Dinwiddie idia Computing, LLC Refactoring George Dinwiddie idia Computing, LLC http://idiacomputing.com http://blog.gdinwiddie.com What is Refactoring? Refactoring is a disciplined technique for restructuring an existing body of code,

More information

TUTORIAL MADCAP FLARE Top Navigation

TUTORIAL MADCAP FLARE Top Navigation TUTORIAL MADCAP FLARE 2018 Top Navigation Copyright 2018 MadCap Software. All rights reserved. Information in this document is subject to change without notice. The software described in this document

More information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 103 Unit 11. Linked Lists. Mark Redekopp 1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++

More information

Think like an Elm developer

Think like an Elm developer Think like an Elm developer Piper Niehaus Denver, CO, USA Backpacker / skier Nonprofit board chair Software Engineer at Pivotal Pivotal Tracker team Elm in Production since 2016 Internal Products and Services

More information

MSO Lecture 6. Wouter Swierstra (adapted by HP) September 28, 2017

MSO Lecture 6. Wouter Swierstra (adapted by HP) September 28, 2017 1 MSO Lecture 6 Wouter Swierstra (adapted by HP) September 28, 2017 2 LAST LECTURE Case study: CAD/CAM system What are design patterns? Why are they important? What are the Facade and Adapter patterns?

More information

Arduino IDE Friday, 26 October 2018

Arduino IDE Friday, 26 October 2018 Arduino IDE Friday, 26 October 2018 12:38 PM Looking Under The Hood Of The Arduino IDE FIND THE ARDUINO IDE DOWNLOAD First, jump on the internet with your favorite browser, and navigate to www.arduino.cc.

More information

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3). CIT Intro to Computer Systems Lecture # (//) Functions As you probably know from your other programming courses, a key part of any modern programming language is the ability to create separate functions

More information

Frequently Asked Questions

Frequently Asked Questions Frequently Asked Questions This PowerTools FAQ answers many frequently asked questions regarding the functionality of the various parts of the PowerTools suite. The questions are organized in the following

More information

Badge#8: Geek Refactoring

Badge#8: Geek Refactoring Badge#8: Geek Refactoring Nuno Pombo, Qualidade de Software, 2018/19 1 When To Refactor? Rule of Three When adding a feature When fixing a bug During a code review 2 How To Refactor? The code should become

More information

Annotation Hammer Venkat Subramaniam (Also published at

Annotation Hammer Venkat Subramaniam (Also published at Annotation Hammer Venkat Subramaniam venkats@agiledeveloper.com (Also published at http://www.infoq.com) Abstract Annotations in Java 5 provide a very powerful metadata mechanism. Yet, like anything else,

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

CS61BL Summer 2013 Midterm 2

CS61BL Summer 2013 Midterm 2 CS61BL Summer 2013 Midterm 2 Sample Solutions + Common Mistakes Question 0: Each of the following cost you.5 on this problem: you earned some credit on a problem and did not put your five digit on the

More information

Ch. 11: References & the Copy-Constructor. - continued -

Ch. 11: References & the Copy-Constructor. - continued - Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed

More information

beyond the install 10 Things you should do after you install WordPress by Terri Orlowski beyond the office

beyond the install 10 Things you should do after you install WordPress by Terri Orlowski beyond the office beyond the install 10 Things you should do after you install WordPress by Terri Orlowski beyond the install 1. Install a backup plugin It may seem silly but the very first thing that I recommend after

More information

Credit where Credit is Due. Lecture 29: Test-Driven Development. Test-Driven Development. Goals for this lecture

Credit where Credit is Due. Lecture 29: Test-Driven Development. Test-Driven Development. Goals for this lecture Lecture 29: Test-Driven Development Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 6448 - Spring Semester, 2003 Credit where Credit is Due Some of the material for this lecture is taken from

More information

Stage 11 Array Practice With. Zip Code Encoding

Stage 11 Array Practice With. Zip Code Encoding A Review of Strings You should now be proficient at using strings, but, as usual, there are a few more details you should know. First, remember these facts about strings: Array Practice With Strings are

More information

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions. Handout 2 Functions, Lists, For Loops and Tuples [ ] Functions -- parameters/arguments, "calling" functions, return values, etc. Please make sure you understand this example: def square(x): return x *

More information

Software Engineering

Software Engineering Software Engineering Lecture 13: Testing and Debugging Testing Peter Thiemann University of Freiburg, Germany SS 2014 Recap Recap Testing detect the presence of bugs by observing failures Recap Testing

More information

This chapter is intended to take you through the basic steps of using the Visual Basic

This chapter is intended to take you through the basic steps of using the Visual Basic CHAPTER 1 The Basics This chapter is intended to take you through the basic steps of using the Visual Basic Editor window and writing a simple piece of VBA code. It will show you how to use the Visual

More information

Software Engineering Testing and Debugging Testing

Software Engineering Testing and Debugging Testing Software Engineering Testing and Debugging Testing Prof. Dr. Peter Thiemann Universitt Freiburg 08.06.2011 Recap Testing detect the presence of bugs by observing failures Debugging find the bug causing

More information

This excerpt is protected by copyright law. It is your responsibility to obtain permissions necessary for any proposed use of this material.

This excerpt is protected by copyright law. It is your responsibility to obtain permissions necessary for any proposed use of this material. This excerpt is protected by copyright law. It is your responsibility to obtain permissions necessary for any proposed use of this material. Please direct your inquiries to permissions@oreilly.com. #15

More information

Designing with patterns - Refactoring. What is Refactoring?

Designing with patterns - Refactoring. What is Refactoring? Designing with patterns - Refactoring Bottom up based application of patterns Improving the design after it has been written What is Refactoring? Two definitions, the object and act of change in software

More information

Refactoring. What to refactor Refactor to what How to conduct the refactoring. This website is also very informative

Refactoring. What to refactor Refactor to what How to conduct the refactoring. This website is also very informative Refactoring What to refactor Refactor to what How to conduct the refactoring This website is also very informative https://refactoring.com/catalog/ Definitions Changing/improving the code structure w/o

More information

6.170 Recitation #5: Subtypes and Inheritance

6.170 Recitation #5: Subtypes and Inheritance 6.170 Recitation #5: Subtypes and Inheritance What is true subtyping? True Subtyping is not exactly the same as Inheritance. As seen in an earlier lecture, class A is a true subtype of class B if and only

More information

CS 142 Style Guide Grading and Details

CS 142 Style Guide Grading and Details CS 142 Style Guide Grading and Details In the English language, there are many different ways to convey a message or idea: some ways are acceptable, whereas others are not. Similarly, there are acceptable

More information

The first program: Little Crab

The first program: Little Crab Chapter 2 The first program: Little Crab topics: concepts: writing code: movement, turning, reacting to the screen edges source code, method call, parameter, sequence, if-statement In the previous chapter,

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

DEADLY SINS. Of Document. 7 Production THE. automated document assembly

DEADLY SINS. Of Document. 7 Production THE.   automated document assembly automated document assembly 7 Production THE DEADLY SINS Of Using document automation technology to generate documents is still not a common practice in many organisations. Instead, staff resort to a range

More information

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

Refactorings. Refactoring. Refactoring Strategy. Demonstration: Refactoring and Reverse Engineering. Conclusion Refactorings Refactoring What is it? Why is it necessary? Examples Tool support Refactoring Strategy Code Smells Examples of Cure Demonstration: Refactoring and Reverse Engineering Refactor to Understand

More information

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices

More information

MSO Lecture 9. Wouter Swierstra (adapted by HP, AL) October 12, 2017

MSO Lecture 9. Wouter Swierstra (adapted by HP, AL) October 12, 2017 1 MSO Lecture 9 Wouter Swierstra (adapted by HP, AL) October 12, 2017 2 BEFORE WE START... Some notes on Kahoot! nicknames: Normaal: Anneke, Bastiaan, Pieter, Sjaak, Ruud,... Funny: Jeroen Fokker, Sergey

More information

Contents. What's New. Version released. Newsletter #31 (May 24, 2008) What's New New version released, version 4.3.3

Contents. What's New. Version released. Newsletter #31 (May 24, 2008) What's New New version released, version 4.3.3 Campground Master Newsletter #31 (May 24, 2008) 1 Newsletter #31 (May 24, 2008) Contents What's New New version released, version 4.3.3 Q & A Retrieving credit card information Guarantee Info missing the

More information

Basic Fiction Formatting for Smashwords in OpenOffice L. Leona Davis. Copyright 2012 L. Leona Davis All Rights Reserved

Basic Fiction Formatting for Smashwords in OpenOffice L. Leona Davis. Copyright 2012 L. Leona Davis All Rights Reserved Basic Fiction Formatting for Smashwords in OpenOffice L. Leona Davis Copyright 2012 L. Leona Davis All Rights Reserved Cover Photo by Dmitry Maslov Cover Design by L. Leona Davis Smashwords Edition June

More information

Assignment 6. Methods Homework Spring 2017 P Question 1 all

Assignment 6. Methods Homework Spring 2017 P Question 1 all Assignment 6. Methods Homework Spring 2017 P. 225-234 Question 1 all True since the parameters are different b. True since the parameters are different c. False since the return type is different but the

More information

Java Programming Constructs Java Programming 2 Lesson 1

Java Programming Constructs Java Programming 2 Lesson 1 Java Programming Constructs Java Programming 2 Lesson 1 Course Objectives Welcome to OST's Java 2 course! In this course, you'll learn more in-depth concepts and syntax of the Java Programming language.

More information

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between MITOCW Lecture 10A [MUSIC PLAYING] PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between all these high-level languages like Lisp and the query

More information

C# Language. CSE 409 Advanced Internet Technology

C# Language. CSE 409 Advanced Internet Technology C# Language Today You will learn Building a basic class Value Types and Reference Types Understanding Namespaces and Assemblies Advanced Class Programming CSE 409 Advanced Internet Technology Building

More information

The State Pattern. State Design Pattern: Motivation

The State Pattern. State Design Pattern: Motivation The State Pattern The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. Toni Sellarès Universitat de Girona State Design

More information

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University Functional Programming in Java CSE 219, Stony Brook University What is functional programming? There is no single precise definition of functional programming (FP) We should think of it as a programming

More information

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to A PROGRAM IS A SEQUENCE of instructions that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form

More information

Programming Language Implementation

Programming Language Implementation A Practical Introduction to Programming Language Implementation 2014: Week 10 Garbage Collection College of Information Science and Engineering Ritsumeikan University 1 review of last week s topics dynamic

More information

The Dynamic Typing Interlude

The Dynamic Typing Interlude CHAPTER 6 The Dynamic Typing Interlude In the prior chapter, we began exploring Python s core object types in depth with a look at Python numbers. We ll resume our object type tour in the next chapter,

More information

Clean & Speed Up Windows with AWO

Clean & Speed Up Windows with AWO Clean & Speed Up Windows with AWO C 400 / 1 Manage Windows with this Powerful Collection of System Tools Every version of Windows comes with at least a few programs for managing different aspects of your

More information

CS1120 Programming Mastery Test

CS1120 Programming Mastery Test Problem: The Final Frontier The year is 2092 and space travel has become commonplace. Many star systems have been discovered with multiple inhabited planets in orbit. Space cruises (like ocean cruises)

More information

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

CSE 403 Lecture 21. Refactoring and Code Maintenance. Reading: Code Complete, Ch. 24, by S. McConnell Refactoring, by Fowler/Beck/Brant/Opdyke CSE 403 Lecture 21 Refactoring and Code Maintenance Reading: Code Complete, Ch. 24, by S. McConnell Refactoring, by Fowler/Beck/Brant/Opdyke slides created by Marty Stepp http://www.cs.washington.edu/403/

More information

TEST-DRIVEN DEVELOPMENT

TEST-DRIVEN DEVELOPMENT tdd 2003/6/10 21:42 page 5 #25 Chapter 1 TEST-DRIVEN DEVELOPMENT To vouch this, is no proof, Without more wider and more overt test -Othello,Act 1 Scene 3 William Shakespeare From programmers to users,

More information

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

Problem 1. Remove consecutive duplicates (6 points, 11 mintues) Problem 1. Remove consecutive duplicates (6 points, 11 mintues) CS3 Fall 04 Midterm 2 Consider a function remove-conseq-dups that takes a sentence and returns a sentence in which any occurrences of a word

More information

ctio Computer Hygiene /R S E R ich

ctio Computer Hygiene /R S E R ich Computer Hygiene Protect Yourself You don't want to be part of the problem If there is a serious attack, you want your systems to be clean You rely on your systems on the air these days Packet NBEMS Logging

More information

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

B. What strategy (order of refactorings) would you use to improve the code?

B. What strategy (order of refactorings) would you use to improve the code? { return title.gettext(); public boolean needssplit() { return costlabel.gettext().equals(" >3 "); public boolean needsestimate() { return costlabel.gettext().equals("? "); Challenges PG-1. Smells. A.

More information

Test Driven Development (TDD)

Test Driven Development (TDD) Test Driven Development (TDD) Test Driven Development Introduction Good programmers write code, great programmers write tests Never, in the field of programming, have so many owed so much to so few - Martin

More information

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

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle Code Critique Identifying smells Refactoring Liskov Substitution Principle What goal are we designing to? What is the typical fix for code smells? What is a limitation of those fixes? How do we address

More information

9 th CA 2E/CA Plex Worldwide Developer Conference 1

9 th CA 2E/CA Plex Worldwide Developer Conference 1 1 Introduction/Welcome Message Organizations that are making major changes to or replatforming an application need to dedicate considerable resources ot the QA effort. In this session we will show best

More information

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 2: Review of Object Orientation 2.1 What is Object Orientation? Procedural paradigm: Software is organized

More information

Program Structure. Steven M. Bellovin April 3,

Program Structure. Steven M. Bellovin April 3, Program Structure We ve seen that program bugs are a major contributor to security problems We can t build bug-free software Can we build bug-resistant software? Let s look at a few examples, good and

More information

Allocation & Efficiency Generic Containers Notes on Assignment 5

Allocation & Efficiency Generic Containers Notes on Assignment 5 Allocation & Efficiency Generic Containers Notes on Assignment 5 CS 311 Data Structures and Algorithms Lecture Slides Friday, October 30, 2009 Glenn G. Chappell Department of Computer Science University

More information

Reliable programming

Reliable programming Reliable programming How to write programs that work Think about reliability during design and implementation Test systematically When things break, fix them correctly Make sure everything stays fixed

More information

Program Structure I. Steven M. Bellovin November 14,

Program Structure I. Steven M. Bellovin November 14, Program Structure I Steven M. Bellovin November 14, 2010 1 Program Structure We ve seen that program bugs are a major contributor to security problems We can t build bug-free software Can we build bug-resistant

More information

CS103 Spring 2018 Mathematical Vocabulary

CS103 Spring 2018 Mathematical Vocabulary CS103 Spring 2018 Mathematical Vocabulary You keep using that word. I do not think it means what you think it means. - Inigo Montoya, from The Princess Bride Consider the humble while loop in most programming

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Subprograms: Arguments

Subprograms: Arguments Subprograms: Arguments ICS312 Machine-Level and Systems Programming Henri Casanova (henric@hawaii.edu) Activation Records The stack is useful to store and rieve urn addresses, transparently managed via

More information

COSC 2P95 Lab 5 Object Orientation

COSC 2P95 Lab 5 Object Orientation COSC 2P95 Lab 5 Object Orientation For this lab, we'll be looking at Object Orientation in C++. This is just a cursory introduction; it's assumed that you both understand the lecture examples, and will

More information

11 MOST COMMON MARKETING BLUNDERS AND HOW TO OVERCOME THEM

11 MOST COMMON  MARKETING BLUNDERS AND HOW TO OVERCOME THEM GOOGLE WEBMASTER TOOLS GETS A NEW NAME GOOGLE SEARCH CONSOLE GOOGLE WEBMASTER TOOLS REPLACES SEARCH QUERIES WITH SEARCH ANALYTICS REPORT GOOGLE TESTING "VALUE ALERT" NOTICE IN GOOGLE SHOPPING ADS PROMOTED

More information

Close Your File Template

Close Your File Template In every sale there is always a scenario where I can t get someone to respond. No matter what I do. I can t get an answer from them. When people stop responding I use the Permission To. This is one of

More information

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 X Cynthia Lee Topics: Last week, with Marty Stepp: Making your own class Arrays in C++ This week: Memory and Pointers Revisit some topics from last week Deeper look at

More information