9 Refactoring Refactoring changes the software structure but does not change the functionality of the program –important activity during evolution Refactoring.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Introduction to C Programming
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Procedural programming in Java
Software Engineering Class design. Class design – ADT Abstract Data Types:  Why use ADT? Hidden implementation details Changes do not affect whole program.
10 Verification All changes in the code have to be verified –refactoring –actualization Essential difficulties –programmers very often produce imperfect.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
7 Impact analysis (IA) Determines the strategy and impact of change Classes identified in concept location are the initial impact set Class dependencies.
5 Introduction to software change Software change (SC) is the process of adding new functionality to existing code Foundation of software evolution, servicing.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
18 Example of SIP Solo Iteration Process(SIP) Software begins by a very simple initial development Functionality is added one step at a time by software.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
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.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
8 Actualization Programmers implement the new functionality –according to change request The process of actualization varies –depends on the size of the.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Procedural programming in Java Methods, parameters and return values.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Object Oriented Software Development
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
© 2012 Václav Rajlich Software Engineering: The Current Practice Ch Conclusion of software change The last phase of software change The activities.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
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.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
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.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Module 9. Dealing with Generalization Course: Refactoring.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
8 Actualization Programmers implement the new functionality –according to change request The process of actualization varies –depends on the size of the.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Eine By: Avinash Reddy 09/29/2016.
Chapter 5: Enhancing Classes
Objects as a programming concept
Module Road Map Refactoring Why Refactoring? Examples
CS 215 Final Review Ismail abumuhfouz Fall 2014.
18 Example of SIP Solo Iteration Process(SIP)
A Very Common Series of Techniques
Review: Two Programming Paradigms
About the Presentations
Object Oriented Analysis and Design
Classes and Data Abstraction
Classes and Objects.
CIS 199 Final Review.
Submitted By : Veenu Saini Lecturer (IT)
Refactoring.
Presentation transcript:

9 Refactoring Refactoring changes the software structure but does not change the functionality of the program –important activity during evolution Refactoring consists of behaviour preserving transformations –Fowler: Refactoring (book, web site)‏ Restructuring, reengineering © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 11

Two roles of refactoring Prefactoring –before attempting the actualization refactoring will make actualization easier Postfactoring‏ –prepares software for future evolution –cleans up the code Initiation Concept Location Impact Analysis Prefactoring Actualization Postfactoring Conclusion VERIFICATIONVERIFICATION © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 12

Examples of refactoring Rename an entity Encapsulate part of the code as a function –opposite: expand a function in a place of call Move function into/out of class Merge and divide classes –factor out a base class, component class © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 13

Extract function During the software evolution some functions may grow to be too large Or we may need to separate two concepts the function currently deals with Extracting part of the function into another function will make it –easier to understand –reusable © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 14

void foo(char c, int& count) { int i,len; char str[MAX]; cin>>str; len=strlen(str); count=0; for(i=0;i<len;i++) if(str[i]==c) count++; } void foo(char c, int count&) { int len; char str[MAX]; cin>>str; len=strlen(str); newfun(count,len,str,c); } void newfun(int& count,int len, char* str,char c) { int i; count=0; for(i=0;i<=len;i++) if(str[i]==c) count++; } Extract function example © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 15

Extract function process Select a block of code for extraction Is the block syntactically complete? Create new function Extract the selected block as a function body Replace the code block with the function call © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 16

Variables during extract function Local variable –value assigned inside, used only inside Parameter passed by value –value assigned outside Parameter passed by reference –value assigned and used outside, changed inside Global variable © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 17

Extract a base class In code development, derived classes always come before the base classes –developers may miss some base classes –refactoring will correct these omissions Extract a base class prepares software for incorporation of new functionality through polymorphism –applicable when old and new functionality have a large overlap © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 18

Example class Matrix{ protected: int elements [10000], columns, rows; public: Matrix(); inverse(); matrix multiply (Matrix&); int get (int,int); void put(int,int); }; // dense matrix © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 19

Extract class AbstractMatrix Change request: Add sparse matrix to the code. –sparse matrix uses the same algorithm for “multiply” and “inverse”. –only access to the elements are different (functions “get” and “put”)‏ Extract abstract class AbstractMatrix –DenseMatrix and SparseMatrix will be derived from it –this will make the change (much) easier –it will allow to incorporate SparseMatrix through polymorphism © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 110

Step 1 of refactoring Rename class –class Matrix  class DenseMatrix There will be several classes dealing with matrix –name needs to be more specific © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 111

The next steps of refactoring and incorporation 2. Extracting base class 3. Incorporating SparseMatrix DenseMatrix AbstractMatrix SparseMatrix DenseMatrix AbstractMatrix © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 112

Steps of refactoring Create a new class AbstractMatrix Make DenseMatrix derived from AbstractMatrix Replace all references to the elements by get and set Move variables columns and rows to AbstractMatrix Move functions inverse and multiply to AbstractMatrix Add virtual functions get and set into AbstractMatrix © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 113

Results After refactoring, it is easy to incorporate SparseMatrix Refactoring preserves the behaviour © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 114

Component class extraction Motivation: Incorporation by replacement –primitive implementation of the class is replaced by a full functionality Concept sometimes does not have class of its own –must be extracted from another class –prefactoring for incorporation by replacement © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 115

Example: Price in PoS © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 116

Refactoring © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 117

Changes in class Item public double calcSubTotal(int numberToSell) { if (numberToSell < 1) return 0.0; else // return numberToSell * price; return numberToSell*price.getPrice(); } © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 118

Extract CashierRecord in PoS © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 119

Incorporation after prefactoring © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 120

Function insertion in C++ A stand-alone function should be a member of a class Function insertion will accomplish that Opposite: Function expulsion –expels member function from a class © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 121

Example of function insertion class A { public: int i; protected: char c; }; int foo(A& a) { a.i = 4;. } int main() { A la;... foo(la)‏... } © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 122

Example of function insertion class A { public: int i; int foo(); protected: char c; }; int A::foo() { this->i = 4;. } int main() { A la;... la.foo()‏... } © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 123

Actions of function insertion The function’s header is inserted into the class specification. Change access to the members of the target class The function header must be qualified by the class identifier. The parameter that is now replaced by membership must be removed. All function calls must be qualified with a class instance. All forward declarations of the function are replaced by the new function declaration in the target class specification. © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 124

Move function from composite to component class A { // A is composite B* b; // B is component }; Add a new parameter of the composite type to the function Access all component members through the new parameter Move the misplaced function from the composite into the component © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 125

class A { public: B* b; int a_data; void a_fun(); void foo(); }; void A::a_fun() { this->foo(); } void A::foo(){ //access A this->... //access B b->... } class B { }; class A { public: B* b; int a_data; void a_fun(); void foo(B*); }; void A::a_fun() { this->foo(b); } void A::foo(B* lb){ //access A this->... //access B lb->... } class B { }; class A { public: B* b; int a_data; void a_fun(); }; void A::a_fun(){ b->foo(this); } void B::foo(A* d){ //accsess A d->... //access B this->... } class B { public: void foo(A*); }; Move function - example © 2012 Václav Rajlich Software Engineering: The Current Practice Ch. 126