Refactoring - A disciplined approach to rework for better design.

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.
About Me – Frank Xu Education ▫ North Dakota State University  Ph.D. in Software Engineering ▫ Towson University  MS in Computer Science ▫ Southeast.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Lectures 17 and 18 A Refactoring Micro-Example FOR0383 Software Quality Assurance 5/16/20151Dr Andy Brooks Refactoring is really easy using this tool.
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
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.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
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.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 3 Bad Smells in Code - 1 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.
Refactoring Improving the structure of existing code Refactoring1.
Chapter 7: Bad Code Smells Omar Meqdadi SE 3860 Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
Today’s Agenda  More refactoring patterns Software Testing and Maintenance 1.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
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)
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Mehdi Einali Advanced Programming in Java 1.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Module 9. Dealing with Generalization Course: Refactoring.
Catalog of Refactoring (1) Composing Methods. Code Smells Long methods Dubious temporary variables Dubious methods.
Catalog of Refactoring (6) Making Method Calls Simpler.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Catalog of Refactoring
Catalog of Refactoring
Steve Chenoweth Office Phone: (812) Cell: (937)
Extract Subclass, Extract Superclass and Extract Hierarchy
Refactoring and Code Smells
Refactoring with inline temp, method, and class
Software Construction and Evolution - CSSE 375 Composing Methods
بازآرایی برنامه Code Refactoring
Refactoring III 27-Nov-18.
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Refactoring.
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring - A disciplined approach to rework for better design.

Objectives What is refactoring? History Why should I refactor? When should I refactor? How to refactor?

Definition Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. It makes the software easier to understand and cheaper to modify.

Continued.. Example: compute gravitational potential energy ▫ PE grav = mass * g * height double potentialEnergy(double mass, double height) { return mass * height * 9.81; } double potentialEnergy(double mass, double height) { return mass * height * 9.81; } static final double GRAVITATIONAL_CONSTANT = 9.81; double potentialEnergy(double mass, double height) { return mass * height * GRAVITATIONAL_CONSTANT; } static final double GRAVITATIONAL_CONSTANT = 9.81; double potentialEnergy(double mass, double height) { return mass * height * GRAVITATIONAL_CONSTANT; } Magic number

Properties of Refactoring One step at a time Preserve correctness Frequent testing

Where did refactoring come from ? For a long time it was a piece of programmer lore, done with varying degrees of discipline by experienced developers, but not passed on in a coherent way. Kent Beck and Ward Cunningham were two of the first people to recognize the importance of refactoring ▫ worked with Smalltalk from the 80's onward. Ralph Johnson's work with refactoring and frameworks has also been an important contribution. Martin Fowler’s book Refactoring: Improving the Design of Existing Code is the classic reference.

Why refactor? Improves the design of software. ▫ Without refactoring, the design of the program will decay Makes software easier to understand. ▫ A good design is easy to understand Helps you find bugs. ▫ The clarification process helps find bugs Helps you program faster. ▫ Poor design slow you down

When to Refactor? As you develop ▫ Example: change a variable name to something more meaningful. Before adding functions. ▫ Sometimes the existing design does not allow you to easily add the feature. When you need to fix a bug ▫ The bug exists because the code was not clear enough for you to see the bug in the first place. When you do a code review ▫ Code reviews help spread knowledge through the development team. ▫ Works best with small review groups

Steps to refactoring Identifying bad smells of code ▫a “bad smell” = a warning sign in the code ▫e.g., time consuming code Designing solid tests ▫for the section of code under analysis. Refactoring the code ▫ based on the type of the smell Applying tests.

Bad Smells in Code Duplicated Code Long Method Large Class Long Parameter List Divergent Change Shotgun Surgery Feature Envy Data Clumps Primitive Obsession Switch Statements Parallel Interface Hierarchies Lazy Class Speculative Generality Temporary Field Message Chains Middle Man Inappropriate Intimacy Incomplete Library Class Data Class Refused Bequest

Bad smells in code Smells within classes ▫ Duplicated code ▫ Long method ▫ Large class ▫ … Smells between classes ▫ Primitive obsession ▫ Inappropriate intimacy ▫ Middle man ▫ …

Duplicated Code – (1) “The #1 bad smell” ▫ If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them. ▫ What if duplicates changes Refactoring solutions ▫ Pull up a field ▫ Form a template method ▫ Substitute algorithm

Duplicated Code – (2) Pull up a field ▫ Two subclasses have the same field. ▫ Move the field to the superclass. SalesmanEngineer Employee name

Duplicated Code – (3) Form a template method ▫ You have two methods in subclasses that perform similar steps in the same order, yet the steps are different. ▫ Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up.

Duplicated Code – (4) Form a template method PersonalCustomer CorporateCustomer Customer getBillableAmt() double base=unit*rate double tax=base*Site.TAX_RATE; return base+tax double base=unit*rate*0.5 double tax=base*Site.TAX_RATE*0.2; return base+tax getBaseAmt() getTaxAmt() getBaseAmt() getTaxAmt() return getBaseAmt()+getTaxAmt() getBaseAmt() getTaxAmt()

Duplicated Code – (5) Substitute algorithm ▫ Replace the body of the method with the new algorithm. duplications

Long Method – (1) The longer the method the harder it is to see what it’s doing. ▫ Poorly thought out abstractions and boundaries Refactoring solutions ▫ Extract method ▫ Replace temp with query ▫ Introduce parameter object ▫ Preserve whole object

Long Method – (2) Extract method ▫ break up into smaller private methods within the class Example private void m1(){ Statement 1;.. Statement 9; } private void m2(){ Statement 10;.. Statement 19; } private void m3(){ Statement 20;.. Statement 30; } public void methodA(){ } private void m1(){ Statement 1;.. Statement 9; } private void m2(){ Statement 10;.. Statement 19; } private void m3(){ Statement 20;.. Statement 30; } public void methodA(){ } public void methodA(){ Statement 1; Statement 2;.. Statement 30; } public void methodA(){ Statement 1; Statement 2;.. Statement 30; } m1(); m2(); m3();

Long Method – (3) Replace temp with query ▫ You are using a temporary variable to hold the result of an expression. ▫ Extract the expression into a method. if (basePrice() > 1000) { return basePrice() * 0.95; } else { return basePrice() * 0.98; } if (basePrice() > 1000) { return basePrice() * 0.95; } else { return basePrice() * 0.98; } Temp variableexpression double basePrice=basePrice(); if (basePrice > 1000) { return basePrice * 0.95; } else { return basePrice * 0.98; } double basePrice=basePrice(); if (basePrice > 1000) { return basePrice * 0.95; } else { return basePrice * 0.98; }

Long Method – (4) Introduce parameter object ▫ A group of parameters that naturally go together. ▫ Replace them with an object. amountInvoicedIn ( ) amountReceivedIn ( ) amountOverdueIn ( ) Customer start: Date end: Date DateRange Start: Date, end: Date DateRange

Long Method – (5) Preserve whole object ▫ You are getting several values from an object and passing these values as parameters in a method call. ▫ Send the whole object instead.

Large Class – (1) A class that is trying to do too much ▫ Can usually be identified by looking at how many instance variables it has. ▫ When a class has too many instance variables, duplicated code cannot be far behind. Refactoring solution ▫ Extract class ▫ Extract subclass

PhoneNumber Large Class – (2) Extract class ▫ Have one class doing work that should be done by two. ▫ Need create a new class and move the relevant fields and methods from the old class into the new class. Customer name areaCode number String: getPhoneNumber() PhoneNumber: getPhoneNumber()

LaborItem Large Class – (3) Extract subclass ▫ A class has features that are used only in some instances. ▫ Create a subclass for that subset of features. JobItem getTotalPrice() getUnitPrice() getEmployee() Do all the JobItem objects need have getEmployee function? getUnitPrice()

Primitive Obsession – (1) Over use primitive to represent data ▫ All properties of a class are primitive types  int, String, boolean, double, etc. ▫ Primitive difficult to represent data  money (which combines quantity and currency)  a date range object Refactorings ▫ Replace data value with object ▫ Replace type code with class

Primitive Obsession – (2) Replace data value with objects ▫ You have a data item that needs additional data or behavior. ▫ Turn the data item into an object. Order customer: String Customer id: String last Name:: String middleName: String firstName: String phone: PhoneNumber

Primitive Obsession – (3) Replace type code with class ▫ A class has a numeric type code that does not affect its behavior. ▫ Replace the number with a new class. Person O: int A: int B:int AB:int BloodType O: BloodType A: BloodType B: BloodType AB: BloodType bloodType:int public class Person{ …… int bloodType =BloodType.O; ……. } public class BloodType{ public static final int O 1; public static final int A 2; public static final int B 3; public static final int AB 4; }

Inappropriate Intimacy – (1) Two classes are overly entertwined ▫ Sharing of secrets between classes ▫ Leads to data coupling Refactorings ▫ Hide delegate ▫ Replace inheritance with delegation

What is that? Inappropriate Intimacy – (2) Hide delegate ▫ A client is calling a delegate class of an object. ▫ Create methods on the server to hide the delegate. Client Server Delegate taskA() public void method(){ delegate.taskA(); }

Inappropriate Intimacy – (3) ClientClass Employee Department getDepartment() getManager() public class Department{ private Employee manager; …… public Department (Employee manager){ this.manager=manager; } public string getManager(){ return manager; } ….. } manager=john. Object need to know less about other parts of the system public getManager(){ return department.getManager(); } getManager() getDepartment().getManager();

Inappropriate Intimacy – (3) Replace inheritance with delegation ▫ A subclass uses only part of a superclasses interface or does not want to inherit data. ▫ Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing.

Middle Man Inline Method A method's body is just as clear as its name Refactoring solution ▫ Put the method's body into the body of its callers and remove the method. int getRating() { return (moreThanFiveLateDeliveries()) ? 2 : 1; } boolean moreThanFiveLateDeliveries() { return _numberOfLateDeliveries > 5; } int getRating() { return (moreThanFiveLateDeliveries()) ? 2 : 1; } boolean moreThanFiveLateDeliveries() { return _numberOfLateDeliveries > 5; } int getRating() { return (_numberOfLateDeliveries > 5) ? 2 : 1; } int getRating() { return (_numberOfLateDeliveries > 5) ? 2 : 1; }

Why might you still not refactor your programs? You might not understand how to refactor. If the benefits are long-term, why exert the effort now? In the long term, you might not be with the project to reap the benefits! Refactoring code is an overhead activity; you're paid to write new features. Refactoring might break the existing program.

Summary Refactoring is a disciplined approach to rework for better design. Refactor when code smells. Take advantage of IDE (Eclipse/IntelliJ/ Java Studio). Check online resources for updated refactoring. Know refactoring before your interview.

References Refactoringshttp://wiki.java.net/bin/view/People/SmellsTo Refactorings

Questions?