Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.

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

Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Refactoring and Code Smells
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.
Refactoring By: Brian Smith. What is Refactoring? Definition: a change to the internal structure of software to make it easier to understand and cheaper.
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.
Advanced Programing practices
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
Refactoring - A disciplined approach to rework for better design.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
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.
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,
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
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.
Advanced Programming in Java
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.
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.
NJIT 1 Test Driven Development and Refactoring Larman, Chapter 21.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David.
Refactoring Mehdi Einali Advanced Programming in Java 1.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Diagnosing Design Problems in Object Oriented Systems Adrian Trifu, Radu Marinescu Proceedings of the 12th IEEE Working Conference on Reverse Engineering.
Refactoring1 Improving the structure of existing code.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
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.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Principles and examples
Module Road Map Refactoring Why Refactoring? Examples
Advanced Programming in Java
Refactoring II 21-Sep-18.
Refactoring and Code Smells
بازآرایی برنامه Code Refactoring
Overview of Eclipse Lectures
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Refactoring II 5-Feb-19.
Refactoring.
Refactoring and Code Smells
Advanced Programing practices
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5

Motto „Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” Martin Fowler

Agenda 1.Refactoring: ideas, motivation, examples 2.Verification of refactorings 3.Bad smells in code

More functionality!

Oh, I've found a bug...

We've got no time. Let's integrate...

Motivation for refactoring  High cost of maintenance  Yourdon: 80% of TCD  Software decays during development  Low understanding of code  Design does not fit the requirements/ functionality  YAGNI = Y ou A ren't G oing to N eed I t

Refactoring  a change made to the internal structure of software void doSth()  to make it easier to understand and cheaper to modify  without changing its observable behaviour Refactoring is: Source: W. Opdyke

Simplest example Extract Method void compAndPrint() { // compute X // compute Y... // print X // print Y... // store X // store Y } void compute () { // compute X // compute Y print(X, Y); // store X // store Y } void print(X,Y) { // print X // print Y }  One method – one function  Allows localizing the potential bugs

Simplest example (reverted) Inline Method void compAndPrint() {... X++; Y++;... } void compute () { X = next(X); Y = next(Y); } int next(N) { return N++; }  Removal of very short methods

Cost of refactoring The cost depends on:  the language used  support from CASE tools  the type of refactorings  number and quality of test cases Refactoring is costly because it does not add new functions to the system. Important factors:  cost of documentation update  cost of test cases update

When to do and not do refactoring?  Three strikes and refactor  When adding new functions  When fixing a bug  While inspecting code  At close deadlines  Prematurely published interfaces  Unstable, rubbish code Unfinished refactoring is like going into debt. You can live with it, but it is costly. Ward Cunningham

Verification of refactorings  Automated verification  Implemented in many IDEs  Verification requires testing  Tests need to be manually created SIMPLEHARD

Simple refactorings... void doSth() If before....  Verification of pre-conditions  Static analysis of code Then after....

... and hard ones  Static code analysis, preconditions  Role of unit tests void doSth() If before.... Then after....

Bad smells If it stinks, change it Kent Beck grandma discussing child-rearing philosophy

Duplicated Code Same or similar code appears all over the system  in same class: extract out the common bits into their own method ( extract method )  in sibling classes: extract method with a shared functionality and then pull up the method to a common superclass  in unrelated classes: extract class

Long Method Same or similar code appears all over the system  Too many options, causing the method to do too many things  Not enough support from other methods, causing the method to do tasks at a lower level than it should  Overly complicated exception handling  the rule of one screen/twenty lines  extract code

Large class Same or similar code appears all over the system  No clear definition of what the class should do, resulting in it doing rather a lot of different things  Out-of-control inner classes  Numerous static and instance methods  Excessive numbers of convenience methods  Cut-and-pasted code  extract new class/subclass/interface  pull up methods/fields

Long Parameter List Method needs to much external information  replace parameter with method (receiver explicitly asks sender for data via sender getter method)  replace parameters with a member fields in a dedicated object

Divergent Change Same or related code appears all over the system Many different changes are necessary  Separate out the varying code into varying classes (extract class) that either subclass or are contained by the non-varying class  Use Visitor or Self Delegation patterns

Feature Envy Method in one class uses lots of pieces from another class  move method to the other class  use Visitor or Self Delegation patterns

Data Clumps Data that's always hanging with each other (e.g. name street zip)  Extract out a class ( extract class ) for the data  Related to long parameter list.  Introduce a Parameter Object  Preserve whole object

Summary  Refactoring decreases cost of maintenance  Refactoring preserves software behaviour  Testing and analysis as methods of verification  Code smells indicate a need for refactoring

Q&A