Refactoring1 Improving the structure of existing code.

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
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.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
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.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
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.
Eclipse – making OOP Easy
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.
Software Engineering Modern Approaches Eric Braude and Michael Bernstein 1.
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.
17-Oct-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Small changes to code to improve it. Refactoring Defined A change made to the internal structure of software to make it easier to understand and cheaper.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
Refactoring1 Refactoring DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY February 6, 2009.
SWE 316: Software Design and Architecture Objectives Lecture # 20 Improving the existing design: Refactoring SWE 316: Software Design and Architecture.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Best Practices. Contents Bad Practices Good Practices.
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
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
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.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Module 3. Smells Between Classes Course: Refactoring.
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)
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
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.
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.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
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
Refactoring II 21-Sep-18.
Refactoring and Code Smells
بازآرایی برنامه 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 II 5-Feb-19.
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring1 Improving the structure of existing code

Refactoring2 Refactoring, the idea Make software more supportable –Understandable By other programmers and other human readers –Maintainable Correcting errors Adapting to new requirements Introducing new qualities. –Scalable Scale up in response to growth in demand for its functionality –More users –More CPU’s

Refactoring3 Refactoring targets “Bad smells in code” Duplicated code Long method Large class Long parameter list Divergent change –If you have to change 3 methods when you have a new database. Shotgun surgery –A simple change spreads over several classes. Feature envy –Method uses a lot of methods from other classes Data clumps –Same data clumps in many classes Example: first name, last name, address Explaining comments –Comments often cover (try to explain) code that is otherwise difficult to read.

Refactoring4 Categories of refactorings overview Composing methods Moving features between objects Organizing data Simplifying conditional expression Making method calls simpler Dealing with generalization

Refactoring5 Some refactoring methods (refactorings) Composing methods –Extract method –Introduce explaining variable –Remove assignments to parameters Moving features between objects –Move method –Move field –Extract class –Inline class Organizing data –Replace magic number with symbolic constant –Encapsulate field –Encapsulate collection Simplifying conditional expressions –Remove control flag –Replace conditional with polymorphism –Introduce assertion Making method calls simpler –Rename method –Remove setting method –Hide method –Replace constructor with factory method –Replace error code with exception –Replace exception with test Dealing with generalization –Pull up field –Pull up method –Pull up constructor body –Extract subclass –Extract superclass –Extract interface –Replace inheritance with delegation

Refactoring6 Composing methods Common problems (“smells in code”) –Long method –Duplicated code –Comments to explain hard-to-understand code Refactorings –Extract method Turn a code fragment into a method whose name explains the purpose of the method –Comment no longer necessary –Introduce explaining variable Put the result of an expression in a temporary variable with a name that explains the purpose. –Remove assignments to parameters Use a temporary variable

Refactoring7 Moving features between objects Common problems –Data class A class with only get and set methods, and nothing else –Feature envy A method is more interested in other classes methods than in it own class’ methods. –Large class Refactorings –Move method Move a method from one class to another –Move field Move a field from one class to another –Extract class Create a new class and move field + methods to the new class. (Rest of) old class references the newly extracted class. –Inline class Move fields + methods to another class.

Refactoring8 Organizing data Common “smells” –Explaining comments –Public fields Refactorings –Replace magic number with symbolic constant Area = * r * r; // bad Static final int PI = ; Area = PI * r * r; // better –Encapsulate field Public fields are bad Use private field + get / set methods –Encapsulate collection Methods must generally return read-only views of collections

Refactoring9 Simplifying conditional expressions Common “smells” –Lots of conditions in loops. Refactorings –Remove control flag Don’t use a lot of conditions in a loop. Use break or return (or continue ) –Replace conditional with polymorphism If you have different types of objects from the same class consider making subclasses. –Introduce assertion If you have any assumptions about the state of the program, you should make an assertion in the program –assert object != null

Refactoring10 Making method calls simpler Refactorings –Rename method If the method name does not reveal the methods purpose, rename the method. –Remove setting method If the field is supposed to be read-only, remove the set method. –Hide method If the method is not used by other classes, make the method private –Replace constructor with factory method IMPORTANT! If you want to do more than simple creation, use a factory method. Example: java.text.DateFormatjava.text.DateFormat –Replace error code with exception -1 or null might not be appropriate error codes –Replace exception with test Give the caller a chance to test the condition of an object before calling the real method. Example: java.util.Iteratorjava.util.Iterator

Refactoring11 Dealing with generalization Refactorings –Pull up field Two subclasses have the same field –Pull up method Two subclasses have the same method –Pull up constructor body Two subclasses have constructors with similar bodies –Extract subclass A class has features that are use only in some instances. Create a subclass for that subset of features. –Extract superclass Two classes with similar features. Create a superclass with the common features. –Extract interface Several clients use the same subset of features of a class. Extract the subset into an interface –Replace inheritance with delegationIMPORTANT! Subclass uses only a part of a superclass –Example (don’t do this at home): java.util.Propertiesjava.util.Properties Use delegation

Refactoring12 Refactoring is not debugging Your program must be working and tested before you start refactoring. If your refactoring reveals a previously unknown bug –Stop refactoring –Write an new test case that shows the bug –Run the new test case –Do your debugging –Run the test case: Now shows that the bug has gone –Go back to refactoring

Refactoring13 Unit testing Before you do any refactoring you must have a reliable unit test. Run the test before the refactoring. Run the test after the refactoring –To check that nothing bad happened due to the refactoring.

Refactoring14 Small steps Refactoring must be done in small steps. –Don’t assume that you can do a lot in one big step. Example: Extract class –Create the new (but empty class) –Make a link from the old to the new class –Use move field (another refactoring) to move fields to the new class Run test after each separate move –Use move method (another refactoring) to move methods to the new class. Run test after each separate move –Decide whether to expose the new class Should if be public or not?

Refactoring15 NetBeans assistance NetBeans can help do simple refactorings –Rename a class, method, or field Across all files in a project –Move class from one package to another –Encapsulate field Make the field private Generate get and set methods –Change parameters to a method Across al files in a project –Extract method –And many other refactorings

Refactoring16 References Fowler et al. Refactoring: Improving the Design of Existing Code, Addison Wesley Chapter 1 Refactoring, a First Example, page –This is THE book on refactoring Martin Fowler Catalog of refactoringshttp://refactoring.com William C. Wake Extreme Programming Explored, Addison Wesley 2002 –Chapter 2. Refactoring, page Maciaszek & Liong Practical Software Engineering, Addison Wesley Chapter 15 Architectural Refactoring, page –Modern books on software engineering has chapters on refactoring Joshua Kerievsky Refactoring to Patterns, Addison Wesley Professional 2005 The code Arie van Deursen et al. Refactoring Test Code, xp2001.pdf xp2001.pdf –Any code can benefit from refactoring, even test code.