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

Size: px
Start display at page:

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

Transcription

1 Refactoring Faculty of Economic Sciences, Communication and IT

2 Why Refactor Refactoring is one factor in keeping your system in shape Without continuous refactoring, the system will rot It takes on technical dept Quality Ideal utopia With continuous refactoring Technical dept Time No refactoring Rotting system

3 What is Technical Dept? Not-quite-right code, Like unwashed dishes not washed up after dinner Nobody can describe exactly what it is But you recognize it when you see it Ask In which part of the system would you rather not work? Shipping first time code is like going into debt You hurry to reach your deadline Cutting quality Skipping refactoring I ll do it later, there is no time right now

4 Time Pressure and Velocity Consider a project with a tough deadline and high development speed Scope No refactoring Break even Next target Project target With continuous refactoring Sustainable pace After successful first delivery Project manager is promoted and moves on Developer is promoted Then, quickly move on to another job! Time

5 Technical Dept and Velocity Why is technical dept a problem? It slows down development The danger occurs when the debt is not immediately repaid Every minute spent on not-quite-right code counts as interest on that debt A little debt speeds development As long as it is paid back promptly with a rewrite If the dept is not payd back, code gets worse and worse Velocity drops because of bad code that is difficult to change You fall behind more and more and need to rush to meet your deadline You produce more and more crap in less and less time

6 Refactoring Examples of technical dept / smells Duplications Less than best naming Long methods / large classes taking care of several tasks Long parameter lists, symptom of mixing tasks Message chains, like t.get().get().get().get() Comments (because code would be impossible to understand without them) Mixing what (the objectives) with how (the algorithmic details) Refactoring is a means to repay the technical dept

7 The Essence of Refactoring Always refactor in small steps on working (green) code Because each change is small, any errors are very easy to find You don't spend long debugging, even if you are careless One benefit from TDD is to support refactoring By continuously running all the old specification examples ( tests ) Like a security net

8 A problem?

9 So, what is Refactoring? Refactoring is a disciplined technique for restructuring an existing body of code ALTERING ITS INTERNAL STRUCTURE WITHOUT CHANGING ITS EXTERNAL BEHAVIOR Its heart is a series of small behavior preserving transformations Each transformation (called a 'refactoring') does little A sequence of transformations can produce a significant restructuring Since each refactoring is small, it's less likely to go wrong The system is also kept fully working after each small refactoring reducing the chances that a system can get seriously broken during the restructuring. Source: Fowler, Martin, Refactoring Home Page, with permission

10 Refactoring Kata The sample program is a program to print out a statement of a customer s charges at a video store. There are several classes that represent various video elements. Here s a class diagram to show them. DomainObject #name: string Movie Tape Rental Customer +pricecode: int daysrented: int -* +statement() Source: Fowler, Martin, Refactoring, a first example,

11 1 Refactoring: Inappropriate intimacy Subclass constructors set instance variable _name in base class DomainObject Change assignment to base class variable in constructor -> Delegate to base constructor public Customer(string name) {_name = name} -> public Customer(string name) : base(name) { } Make the base class variable private

12 Quality Assessment Customer holds all the behavior for producing a statement in its Statement() method. The long Statement method does far too much, and is certainly not objectoriented Loops rentals Selects right kind of rental Computes and accumulated rental costs Computes frequent renter points based on rentals Formats and fills in statement report Many of these things should really be done by the other classes

13 New user requirement: Similar statement in HTML Neither Statement() nor any of its parts is reusable Requires a complete rewrite Copying large parts of the Statement() method to HtmlStatement() and modify selected parts Not too complicated But what happens when details change? Charging rules Frequent renter points New movie codes etc When changes are likely to come, copy and paste is a threat to quality

14 Is if it ain t broke, don t fix it still valid? Statement() is making life painful for HtmlStatement() Enters refactoring When you find you have to add a feature to a program, and the program s code is not structured in a convenient way to add the feature; then 1. first refactor the program to make it easy to add the feature, 2. then add the feature.

15 2 Extracting the Amount Calculation The Statement() method is too long and does too much Extracting a method is taking the chunk of code and making a method out of it. An obvious piece here is the switch statement Mark the switch and choose Refactor -> Extract Method It wants each and thisamount as parameters each is initialized outside and is not modified inside thisamount is initialized outside and is modified inside

16 Finding the boundaries of the method block The initialization to zero is at the top of the each loop

17 Adjusting the block boundaries Include the initialization of the temporary variable in the method extraction First move it closer to the block to extract Use the comment as the method name (will allow for removal of comment later) static confirms that the method is idependent of the Customer object

18 The result The comment is now redundant May be removed static has been removed in the new, private method declaration Verify that all tests still pass VS refactoring helps to avoid silly mistakes

19 3 Improving Variable Names AmountOf is now an independent calculation in its own right Makes it easier to consider variable names in isolation Should be reflected in the parameter name Make it more generic Mark it and press F2 (or right click and choose Refactor -> Rename) Type arental as the new name Respects the Principle of Least Astonishment Also rename thisamount to result

20 Result of renaming variables Good code should communicate what it is doing clearly, and variable names are key to clear code. Never be afraid to change the names to things to improve clarity Remember any fool can write code that a computer can understand, good programmers write code that humans can understand.

21 4 Moving the Amount calculation We saw that AmountOf does not use information from Customer But it does use information from Rental Rental is the Information Expert The method belongs in the Rental class, not the Customer class Move it there

22 Moving a method Compile after each step Copy the code over to Rental, adjust it to fit in its new home and compile In this case, remove the parameter arental Make the method public Locate the calls to the old method and modify the call (Find All References) AmountOf(someRental) -> somerental.amountof() Only one place in this case, since we just created the method Run the tests after each substitution Delete the old method Rename the new method to something more appropriate For instance AmountOf() -> Charge()

23 5 Replacing temp thisamount with a query The variable thisamount is assigned a value that never changes From each.charge() It may be Replaced by a call to each.charge() at each occurrence and dismissed Temps are disturbing

24 6 Extracting the Frequent Renter Points Calculation The calculation of frequentrenterpoints is done incrementally Each increment is calculated independently of the previous ones

25 The refactoring Extract Method doesn t do the trick Extract Method notices that the new value is based on the old one We extract the method in smaller steps 1. Introduce a temporary variable to compute the new value, Run the tests 2. Extract method using that temp variable, Run the tests 3. Replace the new temp with the call to the new method, Run the tests 4. Rename method and temps, Run the tests 5. Move and rename the new method to the Rental class, Run the tests

26 Extract Method in smaller steps 1. Introduce a temporary variable to compute the new value 2. Extract only the parts that do not involve frequentrenterpoints

27 Extract Method in smaller steps 3. Replace the temp by the call to the new method

28 Extract Method in smaller steps 4. Clean up method and temp names

29 Extract Method in smaller steps 5. Move and rename the new method from the Custom class to the Rental class and remove the now redundant comment

30 7 Removing temps Temporary variables are useful within their own routine Encourages long, complex routines Prevents functionality from being used in other contexts We have two temporaries, totalamount and frequentrenterpoints

31 Working towards more than one goal at a time Both variables are accumulated in the loop The loop breaks the Single Responsibility Principle Doing at least three tasks at the same time Computing the total charge Computing the frequent renter points Formating the statement Computing the charge and frequent renter points will both be needed in both Statement and HtmlStatement But HtmlStatement will need a different formating The totals will be more accessible if broken out as separate queries Encouraging a cleaner design without long, complex methods

32 Replace totalamount with a Charge() method Copy the whole of Statement() Rename to Charge() Change return type to double and return totalamount Remove anything not related to calculating the totalamount Rename totalamount to result, refactor to lambda expression requires using System.Linq; Replace totalamount in the footer line of Statement() by Charge()

33 What was achieved by Replace Temp with Queries? The total summed up in totalamount is not used any more The variable can be removed and the tests run The total charge is made accessible as a public operation to whom it might concern The net number of lines of code has slightly increased Due to the overhead of writing the extra method and the loop overhead Lambda expressions compensate for that Performance might suffer from the collection of rentals being looped through one extra time (and yet another for the frequent renter points to come) Valid concerns if the loop takes time Most do not, and if they do, isolating the operations empower optimization

34 Replace frequentrenterpoints with a query Following the same procedure Calling the new procedure int FrequentRenterPoints()

35 8 Adding HtmlStatement HtmlStatement may now easily be written And a corresponding test added The pattern is still copied from Statement, may be refactored further

36 9 Moving Rental Calculations to Movie The switch statement in Rental.Charge is disturbing Switches based on an attribute of another object. Bad idea Move the Charge to Movie Copy Charge from Rental to Movie Remove Tape.Movie. from the switch statement Add parameter int DaysRented Rename DaysRented to daysrented Remove Movie. from the case clauses Compile Replace the body of Rental.Charge with return _tape.movie.charge(_daysrented); Run all the tests Move FrequentRenterPoints to Movie in a similar way

37 10 Hiding Movie price codes Introduce creation methods To avoid pattern new Movie("Home Alone", Movies.REGULAR); And similar for NewRegular and NewChildrens Movies can also change their classification amovie.setpricecode(movie.regular) does not work any more Make Be methods in Movie as an explicit interface

38 11 Inheritance A class hierarchy for the three types of movies might be like this Using polymorphism Movie +Charge() ChildrensMovie +Charge() REgularMovie +Charge() NewReleaseMovie +Charge() Only, this does not allow for a movie to change its classification during its lifetime An object cannot change its class during its lifetime

39 State pattern A solution using the state pattern All the new leave classes are singeltons Movie +Charge() -1 Price +Charge() return pricecode.charge(); ChildrensPrice RegularPrice NewReleasePrice +Charge() +Charge() +Charge() Move date and methods in small pieces to avoid errors

40 Create the classes Let the Price base class own and manage the singleton instances The singleton classes are subclasses of Price

41 Move data over to the new classes Move the price code to the new classes Or modify the methods that use the codes so they work the same as before First, self-encapsulate the type code Let all access, even from within the Movie class use setter and getter properties Replace By And make a new constructor Movie(string name) : base(name) { }

42 Change getting and setting methods in Movie First run all the tests Set a new _price attribute in BeRegular etc. Leaving the old code for the time being, so that all the tests pass Supply Price and subclasses with a PriceCode getter property In Price: In ChildrensPrice: Movie.CHILDRENS e.al. need to be made public again for a short while

43 Clean up Movie._priceCode Modify Movie.PriceCode to return _price.pricecode Check the remaining references for _pricecode in Movie Constructor Movie(string name, int pricecode) is not used any more Delete it The remaining references only set _pricecode It is not read any more Delete all occurrences of it Run all the tests

44 Move methods from Movie to Price Move Charge(), Replacing it by return _price.charge(daysrented); in Movie Run the tests Successively replace the case statement with inheritance One leg at a time Make Price.Charge virtual (=overridable) Make an override in the subclasses, one at a time Copy the logic from the corresponding case leg

45 Reminder: The target structure DomainObject #name: string Price Movie Tape Rental Customer +Charge() +pricecode: int +Charge() +daysrented: int * +Statement() NewReleasePrice +Charge() RegularPrice +Charge() ChildrensPrice +Charge()

46 The Charge methods RegularPrice: NewReleasePrice: ChildrensPrice: Make Price.Charge() abstract

47 Move FrequentRenterPoints Move FrequentRenterPoint() to Price Forward call from Movie to _price.frequentrenterpoints(daysrented) Make the new method in Pricevirtual Change to cover the default case inprice Override in NewReleasePrice only

48 Remove the price codes Verify that Price.PriceCode is only called by Movie.PriceCode Which in turn is never called Verify that the price code constants in Movie are only used by PriceCode in the corresponding Price subclass Remove all price code constants and PriceCode methods Inserting the state pattern was quite an effort The benefit is that it will be far easier to add new price codes or modify price structures

49 When to refactor? Refactor when you add function Refactor first Add new functionality then (in a green mode) Refactor when you fix a bug Obviously the code wasn't good enough to see the bug in the first place Refactor when doing code reviews It might be clear to you, but not to others Pair programming is code review

50 When are we done refactoring? Is like asking When are we done doing the dishes? Or When are we done weeding the garden There is always more weed coming

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

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. In essence we improve the design of the code after it has been written. That's an odd turn of phrase.

Refactoring. In essence we improve the design of the code after it has been written. That's an odd turn of phrase. 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

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

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

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

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

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

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

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 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

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

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

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

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

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

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

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

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

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

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

SOFTWARE ENGINEERING SOFTWARE EVOLUTION. Saulius Ragaišis.

SOFTWARE ENGINEERING SOFTWARE EVOLUTION. Saulius Ragaišis. SOFTWARE ENGINEERING SOFTWARE EVOLUTION Saulius Ragaišis saulius.ragaisis@mif.vu.lt CSC2008 SE Software Evolution Learning Objectives: Identify the principal issues associated with software evolution and

More information

Course December Adrian Iftene

Course December Adrian Iftene Course 10 12 December 2016 Adrian Iftene adiftene@info.uaic.ro Recapitulation QoS Functional Testing Non-Functional Testing Rotting Design Refactoring 2 QoS = ability to provide different priority to different

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

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

JUnit 3.8.1, 64. keep it simple stupid (KISS), 48 Index A accessor methods, 11, 152 add parameter technique, 189 190 add() method, 286 287, 291 algorithm, substituting, 104 105 AND logical operator, 172 architectural design patterns, 277 278 architecture,

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

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

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

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

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

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

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

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

Understading Refactorings

Understading Refactorings Understading Refactorings Ricardo Terra terra@dcc.ufmg.br Marco Túlio Valente mtov@dcc.ufmg.br UFMG, 2010 UFMG, 2010 Understanding Refactorings 1 / 36 Agenda 1 Overview 2 Refactoring 3 Final Considerations

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

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module Basic Class Design Goal of OOP: Reduce complexity of software development by keeping details, and especially changes to details, from spreading throughout the entire program. Actually, the same goal as

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

CSC 408F/CSC2105F Lecture Notes

CSC 408F/CSC2105F Lecture Notes CSC 408F/CSC2105F Lecture Notes These lecture notes are provided for the personal use of students taking CSC 408H/CSC 2105H in the Fall term 2004/2005 at the University of Toronto. Copying for purposes

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

Patterns in Software Engineering

Patterns in Software Engineering Patterns in Software Engineering Lecturer: Raman Ramsin Lecture 10 Refactoring Patterns Part 1 1 Refactoring: Definition Refactoring: A change made to the internal structure of software to make it easier

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

1: Introduction to Object (1)

1: Introduction to Object (1) 1: Introduction to Object (1) 김동원 2003.01.20 Overview (1) The progress of abstraction Smalltalk Class & Object Interface The hidden implementation Reusing the implementation Inheritance: Reusing the interface

More information

Software Development. Modular Design and Algorithm Analysis

Software Development. Modular Design and Algorithm Analysis Software Development Modular Design and Algorithm Analysis Data Encapsulation Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using

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

Software Engineering /48

Software Engineering /48 Software Engineering 1 /48 Topics 1. The Compilation Process and You 2. Polymorphism and Composition 3. Small Functions 4. Comments 2 /48 The Compilation Process and You 3 / 48 1. Intro - How do you turn

More information

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

Refactoring. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 11/29/11. University of Colorado, 2011 Refactoring Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 11/29/11 University of Colorado, 2011 Credit where Credit is Due Some of the material for this lecture is taken

More information

Implementing evolution: Refactoring

Implementing evolution: Refactoring 2IS55 Software Evolution Sources Implementing evolution: Refactoring Alexander Serebrenik / SET / W&I 17-5-2010 PAGE 1 Last week Problem: changing code is difficult Assignment 6 Deadline: Today Assignment

More information

Refactoring. Paul Jackson. School of Informatics University of Edinburgh

Refactoring. Paul Jackson. School of Informatics University of Edinburgh Refactoring Paul Jackson School of Informatics University of Edinburgh Refactoring definition Refactoring (noun) is a change made to the internal structure of software to make it easier to understand,

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

Performance Evaluation

Performance Evaluation Performance Evaluation Chapter 4 A Complicated Queuing System (Acknowledgement: These slides have been prepared by Prof. Dr. Holger Karl) 1 Goal of this chapter! Understand implementation issues and solutions

More information

Rewrite or Refactor. When to declare technical bankruptcy. Laura Thomson OSCON - July 22,

Rewrite or Refactor. When to declare technical bankruptcy. Laura Thomson OSCON - July 22, Rewrite or Refactor When to declare technical bankruptcy Laura Thomson (laura@mozilla.com) OSCON - July 22, 2010 1 Technical debt Shipping first time code is like going into debt. A little debt speeds

More information

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

Refactoring, 2nd Ed. A love story. Michael Hunger Refactoring, 2nd Ed. A love story Michael Hunger Michael Hunger Open Sourcerer Neo4j @mesirii It crashed at 940! I know what you're here for! Covers By: dev.to/rly Which Refactoring do you like most? Extract

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

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors QUIZ How could we disable the automatic creation of copyconstructors pre-c++11? What syntax feature did C++11 introduce to make the disabling clearer and more permanent? Give a code example. QUIZ How

More information

Refactoring with Eclipse

Refactoring with Eclipse Refactoring with Eclipse Seng 371 Lab 8 By Bassam Sayed Based on IBM article Explore refactoring functions in Eclipse JDT by Prashant Deva Code Refactoring Code refactoring is a disciplined way to restructure

More information

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation Object-Oriented Software Engineering Chapter 2: Review of Object Orientation 2.1 What is Object Orientation? Procedural paradigm: Software is organized around the notion of procedures Procedural abstraction

More information

Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles

Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles Ingegneria del Software Corso di Laurea in Informatica per il Management Software quality and Object Oriented Principles Davide Rossi Dipartimento di Informatica Università di Bologna Design goal The goal

More information

Practical Objects: Test Driven Software Development using JUnit

Practical Objects: Test Driven Software Development using JUnit 1999 McBreen.Consulting Practical Objects Test Driven Software Development using JUnit Pete McBreen, McBreen.Consulting petemcbreen@acm.org Test Driven Software Development??? The Unified Process is Use

More information

CS112 Lecture: Extending Classes and Defining Methods

CS112 Lecture: Extending Classes and Defining Methods Objectives: CS112 Lecture: Extending Classes and Defining Methods Last revised 1/9/04 1. To introduce the idea of extending existing classes to add new methods 2. To introduce overriding of inherited methods

More information

Tutorial 02: Writing Source Code

Tutorial 02: Writing Source Code Tutorial 02: Writing Source Code Contents: 1. Generating a constructor. 2. Generating getters and setters. 3. Renaming a method. 4. Extracting a superclass. 5. Using other refactor menu items. 6. Using

More information

CSC207H: Software Design SOLID. CSC207 Winter 2018

CSC207H: Software Design SOLID. CSC207 Winter 2018 SOLID CSC207 Winter 2018 1 SOLID Principles of Object-Oriented Design How do we make decisions about what is better and what is worse design? Principles to aim for instead of rules. e.g. there is no maximum

More information

Import Statements, Instance Members, and the Default Constructor

Import Statements, Instance Members, and the Default Constructor Import Statements, Instance Members, and the Default Constructor Introduction In this article from my free Java 8 course, I will be discussing import statements, instance members, and the default constructor.

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

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

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line 1.2 Adding Integers Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line Finding Sums Mentally The Commutative Property Finding Sums using And Patterns and Rules of Adding Signed

More information

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors QUIZ How could we disable the automatic creation of copyconstructors pre-c++11? What syntax feature did C++11 introduce to make the disabling clearer and more permanent? Give a code example. Ch. 14: Inheritance

More information

Type Checking in COOL (II) Lecture 10

Type Checking in COOL (II) Lecture 10 Type Checking in COOL (II) Lecture 10 1 Lecture Outline Type systems and their expressiveness Type checking with SELF_TYPE in COOL Error recovery in semantic analysis 2 Expressiveness of Static Type Systems

More information

ERICH PRIMEHAMMER REFACTORING

ERICH PRIMEHAMMER REFACTORING ERICH KADERKA @ PRIMEHAMMER REFACTORING WHAT IS REFACTORING? Martin Fowler: Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

CS 147: Computer Systems Performance Analysis

CS 147: Computer Systems Performance Analysis CS 147: Computer Systems Performance Analysis Test Loads CS 147: Computer Systems Performance Analysis Test Loads 1 / 33 Overview Overview Overview 2 / 33 Test Load Design Test Load Design Test Load Design

More information

Back to ObjectLand. Contents at: Chapter 5. Questions of Interest. encapsulation. polymorphism. inheritance overriding inheritance super

Back to ObjectLand. Contents at: Chapter 5. Questions of Interest. encapsulation. polymorphism. inheritance overriding inheritance super korienekch05.qxd 11/12/01 4:06 PM Page 105 5 Back to ObjectLand Contents at: Chapter 5 #( encapsulation polymorphism inheritance overriding inheritance super learning the class hierarchy finding classes

More information

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L 2 0 1 5 Introduction At this point, you are ready to beginning programming at a lower level How do you actually write your

More information

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

QUIZ How do we implement run-time constants and. compile-time constants inside classes? QUIZ How do we implement run-time constants and compile-time constants inside classes? Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of

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

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

Small changes to code to improve it

Small changes to code to improve it Small changes to code to improve it 1 Refactoring Defined A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior

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

CSC207 Week 3. Larry Zhang

CSC207 Week 3. Larry Zhang CSC207 Week 3 Larry Zhang 1 Announcements Readings will be posted before the lecture Lab 1 marks available in your repo 1 point for creating the correct project. 1 point for creating the correct classes.

More information

CS11 Introduction to C++ Fall Lecture 7

CS11 Introduction to C++ Fall Lecture 7 CS11 Introduction to C++ Fall 2012-2013 Lecture 7 Computer Strategy Game n Want to write a turn-based strategy game for the computer n Need different kinds of units for the game Different capabilities,

More information

Module 10 Inheritance, Virtual Functions, and Polymorphism

Module 10 Inheritance, Virtual Functions, and Polymorphism Module 10 Inheritance, Virtual Functions, and Polymorphism Table of Contents CRITICAL SKILL 10.1: Inheritance Fundamentals... 2 CRITICAL SKILL 10.2: Base Class Access Control... 7 CRITICAL SKILL 10.3:

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Basic Keywords Practice Session

Basic Keywords Practice Session Basic Keywords Practice Session Introduction In this article from my free Java 8 course, we will apply what we learned in my Java 8 Course Introduction to our first real Java program. If you haven t yet,

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

Implementing evolution: Refactoring

Implementing evolution: Refactoring 2IS55 Software Evolution Implementing evolution: Refactoring Alexander Serebrenik Sources / SET / W&I 5-6-2012 PAGE 1 Last week How to implement evolution Last week: evolution strategies and decision making

More information

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and dataoriented programming) - Ada 95 (also supports procedural and dataoriented

More information

6.001 Notes: Section 7.1

6.001 Notes: Section 7.1 6.001 Notes: Section 7.1 Slide 7.1.1 In the past few lectures, we have seen a series of tools for helping us create procedures to compute a variety of computational processes. Before we move on to more

More information

Lecture 13: more class, C++ memory management

Lecture 13: more class, C++ memory management CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 13:

More information

Best Practices in Programming

Best Practices in Programming Best Practices in Programming from B. Kernighan & R. Pike, The Practice of Programming Giovanni Agosta Piattaforme Software per la Rete Modulo 2 Outline 1 2 Macros and Comments 3 Algorithms and Data Structures

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 31 Static Members Welcome to Module 16 of Programming in C++.

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 Creational Design Patterns What are creational design patterns? Types Examples Structure Effects Creational Patterns Design patterns that deal with object

More information

Agile Architecture. The Why, the What and the How

Agile Architecture. The Why, the What and the How Agile Architecture The Why, the What and the How Copyright Net Objectives, Inc. All Rights Reserved 2 Product Portfolio Management Product Management Lean for Executives SAFe for Executives Scaled Agile

More information

Safety SPL/2010 SPL/20 1

Safety SPL/2010 SPL/20 1 Safety 1 system designing for concurrent execution environments system: collection of objects and their interactions system properties: Safety - nothing bad ever happens Liveness - anything ever happens

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

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

Programming Style and Optimisations - An Overview

Programming Style and Optimisations - An Overview Programming Style and Optimisations - An Overview Summary In this lesson we introduce some of the style and optimization features you may find useful to understand as a C++ Programmer. Note however this

More information

Logistics. Final Exam on Friday at 3pm in CHEM 102

Logistics. Final Exam on Friday at 3pm in CHEM 102 Java Review Logistics Final Exam on Friday at 3pm in CHEM 102 What is a class? A class is primarily a description of objects, or instances, of that class A class contains one or more constructors to create

More information

Upcoming Features in C# Mads Torgersen, MSFT

Upcoming Features in C# Mads Torgersen, MSFT Upcoming Features in C# Mads Torgersen, MSFT This document describes language features currently planned for C# 6, the next version of C#. All of these are implemented and available in VS 2015 Preview.

More information