SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.

Slides:



Advertisements
Similar presentations
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Advertisements

Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Inheritance Inheritance Reserved word protected Reserved word super
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Module: Definition ● A logical collection of related program entities ● Not necessarily a physical concept, e.g., file, function, class, package ● Often.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Object-Orientated Design Unit 3: Objects and Classes Jin Sa.
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.
Language Evaluation Criteria
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
C++ Code Analysis: an Open Architecture for the Verification of Coding Rules Paolo Tonella ITC-irst, Centro per la Ricerca Scientifica e Tecnologica
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.
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
CSC 313 – Advanced Programming Topics. Design Pattern Intent  Each design pattern is a tool  Like all tools, have reason for being.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Refactoring Improving the structure of existing code Refactoring1.
SE: CHAPTER 7 Writing The Program
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Feasibility Study Cross-language Clone Analysis Team 2.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
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.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
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.
Writing Maintainable code Dr. Susan McKeever DT228/3 GUI Programming.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Refactoring Advanced Software Engineering Dr Nuha El-Khalili.
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.
Copyright © Craig Larman All Rights Reserved COMP-350 Object-Oriented Analysis and Design GRASP: Designing Objects with Responsibilities Reference:
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.
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.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
CSCE 240 – Intro to Software Engineering Lecture 3.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Principles and examples
UNIT-IV Designing Classes – Access Layer ‐ Object Storage ‐ Object Interoperability.
PB173 - Tématický vývoj aplikací v C/C++ (jaro 2016)
Steve Chenoweth Office Phone: (812) Cell: (937)
Module Road Map Refactoring Why Refactoring? Examples
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
A Very Common Series of Techniques
Refactoring and Code Smells
Objects First with Java
Overview of Eclipse Lectures
Design and Programming
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Programming Logic and Design Fourth Edition, Comprehensive
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring

SEG Topic T - Refactoring2 Copyright Note Based on the book: “Refactoring. Improving the Design of Existing Code” Martin Fowler 2000, Addison-Wesley ISBN

SEG Topic T - Refactoring3 What is Refactoring? “Refactoring is the process of changing a software system in such a way that it does not alter the external behaviour of the code yet improves its internal structure.”

SEG Topic T - Refactoring4 What is Refactoring? (cont.) Each refactoring is a series of steps Each step is simple and small Minimises introduction of new bugs Unit tests should be used to verify changes have no effect on behaviour Behavioural changes to a program are not refactorings

SEG Topic T - Refactoring5 Why Refactor? It improves design so that maintenance is easier Makes source code easy to read and understand —I.e. makes the code more usable by programmers Reduced chaos in long-lived code Faster programming in the long term Thinking about specific ways to refactor helps us formalize and name best practices

SEG Topic T - Refactoring6 Opportunities for Refactoring During initial design and programming When adding functionality Make adding new feature easier Keep refactoring separate, though When fixing a bug During a code review E.g. in pair programming

SEG Topic T - Refactoring7 Specific Refactorings to be Discussed in the Following Slides Many refactorings make use of other simpler refactorings Some are opposites (inverses) Use the one that makes your code easier to understand Some can be applied straightforwardly whereas others require deeper analysis Using a tool to help in refactoring is useful Many require finding code to change elsewhere, e.g. code that has to change to be consistent with the refactoring

SEG Topic T - Refactoring8 Refactoring: Extract Method Probably the most used refactoring You should have done this many times yourself Identify code that should be put into a separate method that you will then call Code to reuse —Immediately, or potentially Code that is making a method too long, Code that is difficult to understand —The new method’s name helps explain the function You may also do a Move Method refactoring too move the method to a different class

SEG Topic T - Refactoring9 Dealing with Variables when doing the Extract Method Refactoring Local variables only used in the extracted code can be copied to the new method If one original variable is modified, then Make the new method a function If several original variables are modified, then do one of the following Pass references (usually not a good idea since adds complexity) Use output or var parameters available in some languages Structure the variable to modify as an object, then update the fields of the object Use other refactorings first so multiple variables do not need to be modified

SEG Topic T - Refactoring10 Inverse Refactoring: Inline method Much less common than Extract Method, but still useful sometimes For efficiency If the inlined method is only called once, and overall complexity would be less after the inlining

SEG Topic T - Refactoring11 Refactoring: Extract Class Performed when a you perform object-oriented analysis and the resulting UML class diagram suggests two classes where you have only one E.g. Extract a ‘common superclass’ E.g. Extract a class that will be associated using the ‘Player Role’ or ‘Abstraction Occurrence’ patterns The need results from a violation of the principle of separation of concerns —Often the result of feature creep —Can result from too many Extract Method refactorings Split out related subset of data and methods into a new class May need to rename the old class if its duties have changed

SEG Topic T - Refactoring12 Extract Class - 2 Normally you need a reference to an object of the new class in the old one Uses Move Field and Move Method refactorings to move elements of the new class across (test after each move)

SEG Topic T - Refactoring13 Inverse Refactoring: Inline Class The opposite of Extract Class Applied when a class is doing too little Normally only when there is a 1-1 association Move fields and methods into a class that uses them

SEG Topic T - Refactoring14 Refactoring: Inline Temp Replace a local variable with a simple expression Can make other refactorings easier Can improve efficiency slightly Only do it if the variable is not being used in many places Inverse: Turn a temp back into an expression Local variable may be doing no harm, can be making the code easier to understand, or may be there to avoid breaking the Law of Demeter In which case, leave it there and don’t do this refactoring

SEG Topic T - Refactoring15 Refactoring: Rename Method Reasons for doing this The old name was not clear The task of a method has changed Seems trivial, but is very important Bad naming defeats the whole purpose of encapsulation Code is confusing; in the worst case it can be completely misinterpreted

SEG Topic T - Refactoring16 Refactoring: Replace Magic Number with Symbolic Constant Encapsulates a standard piece of programming advice Applies to strings etc, as well as numbers

SEG Topic T - Refactoring17 Refactoring: Replace Error Code with Exception Code can be written assuming it works It can therefore be much easier to read Error handling code separated out

SEG Topic T - Refactoring18 Refactoring: Encapsulate Field Make a field (instance variable) private Add a getter, and a setter if necessary Or, in C# and Delphi make the variable a ‘property’

SEG Topic T - Refactoring19 Refactoring: Remove Control Flag Remove a local variable that serves as a control flag to break out of loops Use Break, Continue and Exit instead

SEG Topic T - Refactoring20 Refactoring: Split Complex Condition if ((a || b) && (c || d)) { … } Becomes if (a || b) { if (c || d) { … } The inverse of this can sometimes also be useful

SEG Topic T - Refactoring21 Refactoring: Name The Condition if ((a || b) && (c || d)) { … } Becomes meaningfulVariable = a || b; anotherVariable = c || d; if (meaningfulVariable && anotherVariable) { … }

SEG Topic T - Refactoring22 General Considerations about Refactoring 1 Do not apply refactorings without thought for the consequences Multi-threaded applications may experience unexpected effects Changing interfaces may affect a published API Some code is beyond all help

SEG Topic T - Refactoring23 General Considerations about Refactoring 2 Perform good upfront design but you can design for ease of refactoring Don’t make optimisation decisions before finding bottlenecks Refactor first, optimise after