What is Refactoring? CSE301 University of Sunderland Harry R. Erwin, PhD.

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

A practical guide John E. Boal TestDrivenDeveloper.com.
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
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Refactoring and Code Smells
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 3, Part 4 Big Refactorings Steve Chenoweth Office Phone: (812) Cell: (937)
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.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
Why Be Concurrent? CET306 Harry R. Erwin University of Sunderland.
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. 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.
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.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
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.
The effectiveness of refactoring based on a compatibility testing taxonomy and a dependency graph Steve Counsell and Robert Hierons, Brunel University,
Introduction to Refactoring Jim Cooper Falafel Software.
Informatics 122 Software Design II
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring and such ● (Side note) Specialization ● Key terms ● Abstraction, state, persistence and association and their relationship to software development.
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.
E L EARNING E NVIRONMENT FOR S OFTWARE E NGINEERING E DUCATION (R EFACTORING A GENT ) A.Stoyanova-Doycheva University of Plovdiv г. 10th Workshop.
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.
How and When to do Refactoring CSE301 University of Sunderland Harry R. Erwin, PhD.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
University of Sunderland CSE301 Advanced Object-Oriented Software DevelopmentUnit 1 Test-Driven Development CSE301 University of Sunderland Dr. Giles Oatley.
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.
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.
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.
CSCE 240 – Intro to Software Engineering Lecture 3.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring and Code Smells
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

What is Refactoring? CSE301 University of Sunderland Harry R. Erwin, PhD

Purpose The purpose of this lecture is to introduce a way of cleaning up working code. This is called refactoring and is part of current industry practice. You are expected to experiment with it inyour PBL projects. This lecture is organised to structure your investigation of refactoring. You will need to do some reading in the sources.

Sources Fowler, 2000, Refactoring: Improving the Design of Existing Code, Addison-Wesley. Available electronically at the library. / / http:// / / / http:// 841/ ex.phphttp:// ex.php Extensive discussions on the comp.object newsgroup.

Topics Test-Driven Development JUnit Refactoring –Principles –Bad Smells in Code –A Catalogue of Refactorings

Test-Driven Development Refactoring depends on having good unit tests. This means you need to be doing test- driven development. Test-driven development is a method of software development where tests specify interfaces of implementation and all code must have passed the tests. (Wikipedia)

How Bob Martin Describes TDD (personal communication) Erwin: TDD as I understand it: 1. Write a test for a bit of functionality. 2. Show that it fails. 3. Write the code to make the test pass. Martin: A good summary, but there's more. 1. We do not write production code until there is a failing test. 2. We write the simplest possible production code to get the test to pass. 3. We do not write more tests when we have a failing test. 4. We do not add to a failing test.

Martin Comments Further If you watched someone doing TDD you would see them oscillating between test code and production code once every minute or so. During each oscillation the programmer would add a few lines to his test code, thus making it fail (or not compile) and then add just a few lines to his production code in order to make the test pass (or compile). Each oscillation is so simple that it's not worth taking. Each oscillation is so simple that the risk of error is close to zero. If you walk into a room of people working this way, and chose anyone at random, a minute ago all his code would have been working.

Refactoring Deals with code rot. Your program works but it has become ugly as it has been modified. “Refactoring is a technique to restructure code in a disciplined way. 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.” (Fowler) Will be very useful for your projects.

Refactoring Principles Change only the implementations Use it to improve the design of software Use it to make software more understandable Use it to help find bugs Use it to help you program faster Use it to prepare your current program for modification.

When? “Three strikes and you refactor.” –The third time you have to cope with something ugly, fix it. particularly… –When you add functionality –When you need to fix a bug –When you do a code review

Problems with Refactoring The Manager (expectation management) Overuse (spinning your wheels) Databases (database schemas are rigid!) Public Interfaces (avoid changing things the user depends on) A Design that Commits You (In ham and eggs, the hen is involved; the pig is committed. Don’t go there.)

How Software May “Smell”: The system is rigid—it’s hard to change because everything has to change at once. The system is fragile—changes cause the system to break in the strangest of places. The system is immobile—that is, not reusable. The system is viscous—doing things the right way is hard. The system is needlessly complex. The system contains mindless repetition. The system is opaque—hard to understand. Have you ever seen software with these problems?

What “Bad Smells” Smell Like Duplicated Code—cut and pasted everywhere Long Method—hard to understand Long Parameter List— multi-line calls Parallel Inheritance—two hierarchies ‘joined at the hip’ Divergent Change— violates the Single Responsibility Principle. Shotgun Surgery—can’t change just one thing Feature Envy—a class needs lots of methods from another class. Data Clumps—data always used together Large Class—everything including kitchen sink Primitive Obsession— procedural coding style Switch Statements—and duplicated cases

More “Bad Smells” (in Code) Lazy Class—does no useful work Speculative Generality—hooks that are never used Temporary Field—variables only used sometimes Message Chains—reference to a reference to a ref… Middle Man—everything is delegated to a second class Inappropriate Intimacy—’friends’ Alternative Classes/Different Interfaces Incomplete Library Class—lacks useful methods Data Class—hold data, nothing else Refused Bequest—inherited methods ignored Comments—as deodorants

The Basic Rule of Refactoring “Refactor the low hanging fruit” Low Hanging Fruit (def): “The thing that gets you most value for the least investment.” In other words, don’t spend much time on it. There are always ways to improve any design incrementally. We will explore a few of them.

The Goal of Refactoring To improve code without changing what it does. This in some ways is similar to how an optimizing compiler restructures code. Eclipse automates many of these actions. Think about why they work!

A Catalog of Refactorings 1.Composing Methods 2.Moving Features Between Objects 3.Organizing Data 4.Simplifying Conditionals 5.Making Method Calls Simpler 6.Generalization 7.Big Refactorings

1. Composing Methods Extract Method Inline Method Inline Temp Replace Temp with Query Introduce Explaining Variable Split Temporary Variable Remove Assignments to Parameters Replace Method with Method Object Substitute Algorithm

Comments Extract Method is the most important, since it takes a clump of code and makes it into a small method. Eclipse handles this very well. It has problems with local variables, so some of the specialized methods handle those. I use this a lot. Inline Method is the reverse method. Use it if Extract Method creates ugly code. Replace Method with Method Object moves local variables of a method into the class fields of a ‘functor’ class, so Extract Method can be used more easily.

2. Moving Features Between Objects Move Method Move Field Extract Class Inline Class Hide Delegate Remove Middle Man Introduce Foreign Method Introduce Local Extension

Comments Remember CRC cards? You never get responsibilities right the first time. This is how you change your mind. If you need to move functions between objects, these refactorings handle it. Move Method, Move Field, Extract Class, and Inline Class are the heavyweights here. The remainder are special purpose methods to support the Big Four.

3. Organizing Data Self Encapsulate Field Replace Data Value with Object Change Value to Ref Change Ref to Value Replace Array with Object Duplicate Observed Data Switch Uni/Bidirectional Association Replace Magic Number Encapsulate Field Encapsulate Collection Replace Record Replace Type Code with Class/Subclass/ State/Strategy Replace Subclass with Fields

Comments These generally clean up problems with how classes define and access fields. I use encapsulate field a lot. A common need in student projects. Value objects are immutable objects that are equal if their fields are equal—you have to override equals() and hashCode() for them. The most advanced refactoring here is probably Duplicate Observed Data. That’s how you fix a design that mixes model or business logic with GUI or SQL code.

4. Simplifying Conditionals Decompose it Consolidate it Consolidate Duplicate Fragments Remove Control Flag Replace Nested Conditional with Guard Clauses Replace with Polymorphism Introduce Null Object Introduce Assertion

Comments Involve creation of polymorphic classes. Remember this when you learn about polymorphism next term. These mostly simplify conditional logic (if/then/else). Decompose Conditional is the most important. The parts of an if/then/else are replaced with method calls. These also clean up the use of flags to control sequencing or returns: –if(o.foo()) o.bar() else o.baz(); Switch statements can also be replaced with polymorphic classes. A null object is a real ‘do-nothing’ object that substitutes for a null value in a reference. It obviates the need to do an instanceOf test to check for null.

5. Making Method Calls Simpler Rename Method Add Parameter Remove Parameter Separate Query from Modifier Parameterize Method Replace Parameter with Explicit Methods Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Remove Setting Method Hide Method Replace Constructor with Factory Method Encapsulate Downcast Replace Error Code with Exception Replace Exception with Test

Comments These refactorings make interfaces easier to understand. Rename Method is convenient to document what a method does. Peter Dunne likes it a lot. Most of the remaining refactorings are used to get rid of parameters, but be cautious in concurrent programming. (Ask Chris Knowles why.) Factory methods hide the concrete implementation of an interface. Remember this when you learn about the Dependency Inversion Principle next term. This is the answer.

6. Generalization Pull Up Field Pull Up Method Pull Up Constructor Body Push Down Method Push Down Field Extract Subclass Extract Superclass Extract Interface Collapse Hierarchy Form Template Method Replace Inheritance with Delegation Replace Delegation with Inheritance

Comments I find it hard to get inheritance right from the beginning. These refactorings help clean up messy inheritance hierarchies (like those in your projects). They also allow you to evolve inheritance hierarchies ‘on the fly’, another common need. Sometimes delegation works better than inheritance or vice versa. Try both. (I will talk about ‘delegate, delegate, delegate’ again.)

7. Big Refactorings Tease Apart Inheritance –Create two hierarchies from a complex one and use delegation to invoke one from the other. Convert Procedural Design to Objects –Turn data into objects; break up the behaviour; finally move the behaviour into the objects. Separate Domain from Presentation –Move domain logic into separate domain classes. Extract Hierarchy –Subclass a very complex class with many special cases. (A common need in student projects)

Comments Use “Tease Apart Inheritance” to simulate multiple inheritance. Use “Convert Procedural Design to Objects” to convert a procedural program to OO. Use “Separate Domain from Presentation” to fix a messy GUI. Use “Extract Hierarchy” to clean up complexity, particularly involving many special cases.

Conclusions Kent Beck says: This is “only the beginning.” Why? Questions we’ve left unaddressed include when to use refactoring and when to let well enough alone. That will be another lecture. Beck’s advice: –Pick an achievable goal –Stop when you’re unsure –Backtrack if necessary –Work with a partner I have some exercises involving refactoring.