Refactoring.

Slides:



Advertisements
Similar presentations
12-Dec-14 Refactoring IV. Previously discussed bad smells Duplicated code — and other forms of redundancy Long method — use short methods that delegate.
Advertisements

Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Gregor Gisler-Merz BrownBag Session Refactoring.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) Cell: (937)
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
George Blank University Lecturer. REFACTORING Improving the Design of Existing Code Supplement to Ian Sommerville, Software Engineering, Chapter 20 Prepared.
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
25-Jun-15 Refactoring III. General philosophy A refactoring is just a way of rearranging code Refactorings are used to solve problems If there’s no problem,
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
“Blink-factoring”: Software engineering meets pop psychology Presented by Jamie Hohman EECS 816: Object-Oriented Software Development April 22, 2008.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Leiden University. The university to discover. Leiden University. The university to discover. Leiden University. The university to discover. Software Engineering.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
What is Refactoring? CSE301 University of Sunderland Harry R. Erwin, PhD.
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
Refactoring. Refactoring is a step in the software design process.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 3 Bad Smells in Code - 1 Steve Chenoweth Office Phone: (812) Cell: (937)
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
Refactoring - A disciplined approach to rework for better design.
Refactoring Improving the structure of existing code Refactoring1.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
Refactoring1 Improving the structure of existing code.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Introduction to Refactoring Jim Cooper Falafel Software.
Informatics 122 Software Design II
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring: Code Smells. Admin Notes REGISTER FOR BLACKBOARD Watch blackboard site for updates on class as hurricane season approaches.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
NJIT 1 Test Driven Development and Refactoring Larman, Chapter 21.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
Module 3. Smells Between Classes Course: Refactoring.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
Refactoring. 2 Process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Module 9. Dealing with Generalization Course: Refactoring.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Module Road Map Refactoring Why Refactoring? Examples
Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring II 21-Sep-18.
Refactoring and Code Smells
Software Construction and Evolution - CSSE 375 Composing Methods
بازآرایی برنامه Code Refactoring
Overview of Eclipse Lectures
Refactoring III 27-Nov-18.
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Refactoring Types Blake Duncan.
Refactoring.
Refactoring and Code Smells
Refactoring and Code Smells
Refactoring and Code Smells
Presentation transcript:

Refactoring

Refactoring Noun: “A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behaviour” Verb: “To restructure software by applying a series of refactorings without changing its observable behaviour 1-Sep-19 COSC6431

When to refactor All the time! Indications that it’s time to refactor are known as code smells We’ll examine a number of them… 1-Sep-19 COSC6431

Duplicated code Same expression in two methods of the same class Use Extract Method refactoring Same expression in two methods of sibling classes Use Extract Method and Pull Up Method If code is similar but not same, consider Form Template Method Duplicated code in unrelated classes May need to Extract Class or otherwise eliminate one of the versions 1-Sep-19 COSC6431

Long Method The longer a method is, the more difficult it is to understand Be aggressive about decomposing methods Use good naming 90% of the time, just Extract Method What to extract? Look for comments explaining a piece of code 1-Sep-19 COSC6431

Large Class A class that tries to do too much often has too many instance variables Prime breeding ground for duplicated code Extract Class Extract SubClass for some of the Extract Interface variables 1-Sep-19 COSC6431

Long parameter list Hard to understand, requires frequent changes In OO systems, much fewer parameters are required Shorten parameter lists with Replace Parameter with Method Preserve Whole Object Introduce Parameter Object 1-Sep-19 COSC6431

Divergent Change A class is commonly changed in different ways for different reasons “I will have to change these three methods every time I get a new database; I have to change these four methods every time there is a new financial instrument” Extract Class to alleviate this problem 1-Sep-19 COSC6431

Shotgun Surgery Every time you make a kind of change, you have to make a lot of little changes Easy to miss an important change Move Method and Move Field to put all changes into a single class You might even use Inline Class 1-Sep-19 COSC6431

Feature Envy A method seems more interested in a class other than the one it is in Invokes many getter methods from another class Move Method to where it wants to be Strategy and Visitor design patterns result in code that has feature envy Acceptable since this way we fight divergent change Often there are tradeoffs in fighting code smells 1-Sep-19 COSC6431

Data Clumps Bunches of data that hang around together ought to be made into their own object (Extract Class) Delete one of the data values. Do the others make sense? You can then slim parameter lists down with Introduce Parameter Object or Preserve Whole Object 1-Sep-19 COSC6431

Switch statements Switch statements are often duplicated If you add a new clause, you need to find all related switch statements Polymorphism can solve this problem If switching on type code Extract Method Move Method Replace Type Code with Subclasses Replace Conditional with Polymorphism 1-Sep-19 COSC6431

Parallel Inheritance Hierarchies Special case of shotgun surgery Every time you make a subclass of one class, you also have to make a subclass of another Eliminate duplication by having instances of one hierarchy refer to instances of the other 1-Sep-19 COSC6431

Lazy class If a class is not doing enough to justify maintaining it, it should be removed Refactoring often results in lazy classes that can be removed with Collapse Hierarchy Inline Class 1-Sep-19 COSC6431

Speculative Generality Machinery added for future use that never gets implemented Makes system much harder to understand Often identified because test cases are the only users of a method of a class Remove unnecessary machinery with Inline Class / Collapse Hierarchy Remove Parameter / Rename Method 1-Sep-19 COSC6431

Temporary Field Fields that are not used (or used only in certain circumstances) Very difficult to determine their usefulness Maybe they are only used as global variables to avoid passing them around as parameters Extract Class for temporary fields 1-Sep-19 COSC6431

Refused Bequest Subclasses do not want or need methods or data of their parents Push Down Method and Push Down Field to move unwanted methods to siblings If the subclass does not want to support the interface of the superclass, Replace Inheritance with Delegation 1-Sep-19 COSC6431

Comments Comments are of course a sweet smell, but they should not be used as deodorant When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous Can also use Extract Method Rename Method Introduce Assertion 1-Sep-19 COSC6431

More code smells Primitive obsession Message Chains Middle man Inappropriate intimacy Alternative classes with different interfaces Incomplete library class Data class 1-Sep-19 COSC6431

Refactoring catalog Many different refactorings possible Martin Fowler lists about 80 of them in his book on Refactoring Other refactorings have been identified as well They all come with well-defined mechanisms for their application 1-Sep-19 COSC6431

Mechanics of Extract Method Create a new method, and name it after the intention of the method (what it does, not how it does it) Copy the extracted code from the source method to the target method Scan the extracted code for references to any variables that are local in scope to the source method See whether any temporary variables are used only within the extracted code. If so, declare them in the new method 1-Sep-19 COSC6431

Mechanics of Extract Method See if the extracted code modifies any local-scope variables. If only one, it can be the return value of the new method. If more, extraction cannot happen as is Pass into the target method as parameters local-scope variables that are read from the extracted code Replace the extracted code in the source method with a call to the target method Compile and test 1-Sep-19 COSC6431

The first step: Testing In order to refactor, you need a solid suite of tests Tests must be automatic and self-checking Run tests often, after every small change The Junit framework can help with the automation part (for Java code) www.junit.org 1-Sep-19 COSC6431