Toronto ECE 450 - Lecture 5 - Topics on Refactoring

Unformatted text preview:

Spring 2005 ECE450H1S Software Engineering IILecture 5Topics on RefactoringSome materials are based on Martin Fowler’s bookCopyright © Yijun Yu, 2005Spring 2005 ECE450H1S Software Engineering IILast lecture and tutorial …Design patterns• We showed the structures of all the classicdesign patterns• We explained some of them and theirapplications in OpenOME, Protégé and Eclipse• For your exercise– Explain the MVC and plug-in patterns using theclassic design patterns– Find more cases in OpenOME that can apply thedesign patterns• The application of design patterns, can be called“refactoring”Spring 2005 ECE450H1S Software Engineering IIToday …Topics on Refactorings1. What is refactoring? Why?2. How to classify refactorings3. How to apply refactorings4. Compare with tuning and design patterns5. Refactor source code into requirements6. SummaryReferencesMartin Fowler. Refactoring – improve the design of existing code.http://www.refactoring.comTom Mens et al. “A survey of software refactoring”. TSE 30(2),2004.Spring 2005 ECE450H1S Software Engineering II1. What is refactoring?• It is a new English word, can be usedin part of speech for a noun(countable or uncountable), a verb …• Its origin = FactoringSpring 2005 ECE450H1S Software Engineering IIFactoring• In mathematics, factorization orfactoring is the decomposition of anobject into an expression of smallerobjects, or factors, which multipliedtogether give the original• For example, the number 15 factors intoprimes as 35; and the polynomial x2- 4factors as (x - 2)(x + 2)Spring 2005 ECE450H1S Software Engineering IIRefactoring• Refactoring is the process of rewriting writtenmaterial to improve its readability or structure,with the explicit purpose of keeping its meaningor behavior.• The term is by analogy with the factorization of numbers andpolynomials. For example, x2- 1 can be factored as (x + 1)(x- 1), revealing an internal structure that was previously notvisible (such as the two zeroes at +1 and -1). Similarly, insoftware refactoring, the change in visible structure can oftenreveal the "hidden" internal structure of the original code.• Extracting common descriptions20 + 20 + 20 = (1 + 1 + 1) x 20 = 3 x 20Spring 2005 ECE450H1S Software Engineering IISoftware Refactoring• Software refactoring = “Restructuringexisting code by altering its internalstructure without changing its externalbehavior”– adapted from Martin Fowler’s book• To avoid duplicationsA. Hunt, and D. Thomas. Pragmatic Programmer,Addison Wesley,1999.Martin Fowler, Avoid Repetition, IEEE Software, Jan/Feb2001 pp.97—99.Spring 2005 ECE450H1S Software Engineering IIMore on definitionsAre the following activities refactorings?• Adding new functionalities• Fixing correctness bugs• Tuning performance• Patching security holesSpring 2005 ECE450H1S Software Engineering IIWhen to apply refactorings“Any fool can write code that a computer canunderstand. Good programmers write code that humancan understand”Bad code smells:• Duplicate code (clones): feature envy• Complex control, Long methoduse Hammock graph: single entry/single exit– Comments signal semantic distance– Conditional and loops• Complex data, Long parameter list• OO specific: large class, switch statements, parallelinheritance, middle man, message change, temporaryfields, data class, etc.Spring 2005 ECE450H1S Software Engineering IIThe refactoring rhythms• Development =(Adding features, Refactoring)*• Refactoring = (Testing, Small Steps) *• Small Steps = one of the refactoring typesSpring 2005 ECE450H1S Software Engineering II2. Type of refactorings“Putting things together when changes aretogether”• Extract Methods• Move Methods• Rename Methods• Replace Temp with Query• Replace conditionals with polymorphism• Replace Type code with State/Strategy• Self Encapsulate Field• ……Spring 2005 ECE450H1S Software Engineering II3. Applications• We use three examples to explain some basicrefactorings– Extract method:• signalled by comments• single-entry, single-exit• increase the level of indirection• reduce the length of a method• increase the chance of reuse– Move method:• Place method together with the object, Putting thingstogether when changes are together– Replace conditions with polymorphism• Switches are “hard code”, polymorphism is better forextensibility in OOSpring 2005 ECE450H1S Software Engineering IIExample 1 – Extract methodvoid f() {...// Compute scorescore = a * b + c;score -= discount;}void f() {...computeScore();}void computeScore() {score = a * b + c;score -= discount;}Spring 2005 ECE450H1S Software Engineering IIExample 2 – Move methodclass Jar {...}class RoboPacker {private bool isFragile(Jar foo) {switch(foo.material) {case GLASS: return true;case WOOD: return true;case TIN: return false;}}}class Jar {bool isFragile() {switch(material) {case GLASS: return true;case WOOD: return true;case TIN: return false;} } }class RoboPacker {private bool isFragile(Jar foo) {return foo.isFragile();}}Spring 2005 ECE450H1S Software Engineering IIExample 3 – Replace conditionalswith polymorphismclass Jar {bool isFragile() {switch(material) {case GLASS:// complex glass calculationcase WOOD:// complex wood calculationcase TIN:// complex tin calculation} } }class Jar {bool isFragile() {return material.isFragile();} }interface Material { ... }class GlassMaterial:Material { ... }class WoodMaterial:Material { ... }class TinMaterial:Material { ... }Spring 2005 ECE450H1S Software Engineering II4. Refactoring versus Tuning• Refactoring aims at improve understandabilityand maintainability• Tuning aims at improve performance• They are both non-functional (no new features),but they are different– Refactoring can be harmful to performance– Tuning can be harmful to maintainability• You need to know where are the bottlenecks• Y. Yu et al. “Software refactorings guided bysoftgoals”, REFACE workshop in conjunctionwith WCRE’03.Spring 2005 ECE450H1S Software Engineering IIThe header restructuring projectSpring 2005 ECE450H1S Software Engineering IIYour exercise• Monitor the evolution of your softwareproduct by measured its metrics– Statically:complexity metrics: LOC, Halstead, McCabe– Dynamically:Performance metrics: time (clockticks,#instructions), space (cache misses, L1instruction, L1 data, L2 cache, etc., memoryfootprint)• Decide on which is the urgent non-functional


View Full Document
Download Lecture 5 - Topics on Refactoring
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5 - Topics on Refactoring and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5 - Topics on Refactoring 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?