Refactoring Refactoring.

Slides:



Advertisements
Similar presentations
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Advertisements

Alternate Software Development Methodologies
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
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.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Chapter 9 – Software Evolution and Maintenance
CASE Tools And Their Effect On Software Quality Peter Geddis – pxg07u.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Refactoring Lecture 5 CIS 6101 Software Processes and Metrics.
Test Driven Development An approach to writing better code Jimmy Zimmerman Intel Corporation.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Advanced Programing practices
Sadegh Aliakbary Sharif University of Technology Spring 2012.
T-unit: Tcl Unit Test Package Automated Unit Test Package For Tcl Procedures Final Presentation Joseph Boyle Loyola Marymount University.
Refactoring Improving the structure of existing code Refactoring1.
Refactoring1 Refactoring DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY February 6, 2009.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring1 Improving the structure of existing code.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Coding.
Advanced Programming in Java
Eclipse 24-Apr-17.
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.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Com S 362: Object-Oriented Analysis and Design Refactoring.
Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David.
1 ECE 750 Topic 8 Meta-programming languages, systems, and applications Evolving Object-Oriented Designs with Refactorings – Lance Tokuda and Don Batory.
Refactoring Mehdi Einali Advanced Programming in Java 1.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring1 Improving the structure of existing code.
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.
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.
Java IDE Dwight Deugo Nesa Matic
CPSC 372 John D. McGregor Module 8 Session 1 Testing.
Principles and examples
Principles of Programming & Software Engineering
Data Abstraction: The Walls
Module Road Map Refactoring Why Refactoring? Examples
John D. McGregor Session 9 Testing Vocabulary
Chapter 18 Maintaining Information Systems
Principles of Programming and Software Engineering
A Very Common Series of Techniques
Lecture 2 of Computer Science II
Advanced Programming in Java
John D. McGregor Session 9 Testing Vocabulary
Refactoring SENG 301 © Refactoring: Improving the Design of Existing Code, Martin Fowler, Kent Beck Source:
Un</br>able’s MySecretSecrets
Dilbert Scott Adams Manage It! Your Guide to Modern, Pragmatic Project Management. Johanna Rothman.
Business Should Adopt Agile Testing. Test Driven Development is an essential software development way that is made by an automated test program which.
Refactoring and Code Smells
John D. McGregor Session 9 Testing Vocabulary
Objects First with Java
Chapter 2 Software Processes
Overview of Eclipse Lectures
Design and Programming
Improving the structure of existing code
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Chapter 3 – Agile Software Development
Refactoring and Code Smells
Advanced Programing practices
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring Refactoring

Refactoring :: What is it? Definition: Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Refactoring does not fix bugs, but it may help find bugs by scrutinizing code. It may also reduce further bugs occurrence by cleaning-up code. Refactoring does not add new functionality to the system, but it will ease the further adding of new functionality. Refactoring

Refactoring :: when? Refactoring ought to be done continuously as “bad smells” are encountered during programming. More importantly, when using iterative development, a major refactoring stage should precede the beginning of the development of a new build. This will remove slight design problems and ease the addition of further functionality. In this case, refactoring counterbalances the productivity-driven software development practices implied by agile incremental software development. Refactoring

Refactoring :: why? Refactoring is usually done to: improve code readability & comprehensibility simplify code structure, improve design quality improve maintainability improve extensibility Improve some of the nonfunctional attributes of the software Does not modify its functional requirements Refactoring

Refactoring does not take place in a vacuum, but typically the refactoring process takes place in a context of adding features to software: "... refactoring and adding new functionality are two different but complementary tasks" -- Scott Ambler Refactoring

There are two general categories of benefits to the activity of refactoring. Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp. This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments. Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed. Refactoring

Before refactoring a section of code, a solid set of automatic unit tests is needed. The tests should demonstrate in a few seconds that the behavior of the module is correct. The process is then an iterative cycle of making a small program transformation, testing it to ensure correctness, and making another small transformation. If at any point a test fails, you undo your last small change and try again in a different way. Through many small steps the program moves from where it was to where you want it to be. Proponents of extreme programming and other agile methodologies describe this activity as an integral part of the software development cycle Refactoring code ruthlessly prevents rot, keeping the code easy to maintain and extend. This extensibility is the reason to refactor and the measure of its success. But note that it is only "safe" to refactor the code this extensively if we have extensive unit test suites of the kind we get if we work Test-First. Without being able to run those tests after each little step in a refactoring, we run the risk of introducing bugs. If you are doing true Test-Driven Development (TDD), in which the design evolves continuously, then you have no choice about regular refactoring, since that's how you evolve the design. Refactoring

Refactoring :: how is it done? Each refactoring is implemented as a small behavior-preserving transformation. Behavior-preservation is achieved through pre- and post-transformation testing. Refactoring process: test-refactor-test Refactoring

Refactoring :: drawbacks Cost Overhead: Refactoring is an add-on activity and therefore will incur extra cost in form of time, effort, and resource allocation, especially if elaborated design and code documentation is maintained. However, when done sparingly and only on key issues, its benefits are greater than its overhead. Automated documentation tools will also diminish the overhead. Requires Expertise: Refactoring requires some expertise and experience and considerable effort in going through the process, especially if proper testing is involved. However, this overhead can be minimized by using automated testing such as with a unit testing framework. Refactoring

Refactoring :: examples Encapsulate Downcast: A method returns an object that needs to be downcasted by its callers. Refactor by moving the downcast to within the method. Object lastReading() { return readings.lastElement(); } Reading lastReading() { return (Reading) readings.lastElement(); Refactoring

Refactoring :: examples Consolidate Conditional Expression: You have a sequence of conditional tests with the same result. Refactor by combining them into a single conditional expression and extract it. double disabilityAmount() { if (_seniority < 2) return 0; if (_monthsDisabled > 12) return 0; if (_isPartTime) return 0; // compute the disability amount if (isNotEligibleForDisability()) return 0; Refactoring

Refactoring :: examples Consolidate Duplicate Conditional Fragments: The same fragment of code is in all branches of a conditional expression. Refactor by moving it outside of the expression. if (isSpecialDeal()) { total = price * 0.95; send(); } else { total = price * 0.98; } if (isSpecialDeal()) total = price * 0.95; else total = price * 0.98; send(); Refactoring

Refactoring :: examples Rename Method: The name of a method does not reveal its purpose. Refactor it by changing the name of the method. int getInvCdtLmt(){ … } int getInvoiceableCreditLimit(){ Refactoring

Refactoring :: examples Pull Up Field: Two subclasses have the same field. Refactor it by moving the field to the superclass. Refactoring

Refactoring :: examples Push Down Method: Behavior on a superclass is relevant only for some of its subclasses. Refactor it by moving it to those subclasses. Has option to be abstract and stays in the super class level Refactoring

Refactoring :: practice Some refactorings are controversial. Some refactorings are arguably not improving code quality. Some refactorings can in fact be counter-productive when applied blindly, especially in iterative development, where design is evolving. Have your team adopt a set of refactorings to be applied, and make sure that refactorings are applied in a productive manner. Apply in combination with the application of design patterns. Use refactoring tools to automate changes, e.g. Eclipse Refactoring

Refactoring is the process of applying behavior-preserving transformations to a program with the objective of improving the program's design. A specific refactoring is identified by a name (e.g., Extract Method), a set of preconditions, and a set of specific transformations that need to be performed. Tool support for refactoring is highly desirable because checking the preconditions for a given refactoring often requires nontrivial program analysis, and applying the transformations may affect many locations throughout a program. In recent years, the emergence of light-weight programming methodologies such as Extreme Programming has generated a great amount of interest in refactoring, and refactoring support has become a required feature in modern-day IDEs. Refactoring

Automated Code Refactoring Many software editors and IDEs have automated refactoring support. Here is a list of a few of these editors, or so-called refactoring browsers. IntelliJ IDEA (for Java) Eclipse's Java Development Toolkit (JDT) NetBeans (for Java) and RefactoringNG, a Netbeans module for refactoring where you can write transformations rules of the program's abstract syntax tree. Embarcadero Delphi Visual Studio (for .NET) JustCode (addon for Visual Studio) ReSharper (addon for Visual Studio) Refactoring

Automated Code Refactoring Coderush (addon for Visual Studio) Visual Assist (addon for Visual Studio with refactoring support for VB, VB.NET. C# and C++) DMS Software Reengineering Toolkit (Implements large-scale refactoring for C, C++, C#, COBOL, Java, PHP and other languages) Photran a Fortran plugin for the Eclipse IDE SharpSort addin for Visual Studio 2008 Sigasi HDT (for VHDL) XCode Smalltalk Refactoring Browser (for Smalltalk) Simplifide (for Verilog, VHDL and SystemVerilog) Tidier (for Erlang) Refactoring

To refactor code in eclipse or IDEA, you select the code you want to refactor, pull down the specific refactoring you need from a menu, and the IDE does the rest of the hard work. You are prompted appropriately by dialog boxes for new names for things that need naming, and for similar input. You can then immediately rerun your tests to make sure that the change didn't break anything. If anything was broken, you can easily undo the refactoring and investigate. To perform refactoring, you must select the elements first. You can select elements I several views including Outline view and Package explorer view Refactoring

Types of Refactoring in Eclipse

Example The Rename refactoring does just that, it renames a Java element. Although you can rename Java files and Java elements by hand, this will not update any references to those files and elements. You should have to search through the files in your project and replace the appropriate references by hand. This is always the chance that you might miss a reference or replace a wrong reference. The Rename refactoring will intelligently update all of the appropriate references in a project for you. Refactoring

Example Applying some refactoring that modify the structure of classes such as Push Down and Pull Up may not modify the other classes in a project. In this case you will have to make sure that all references to the modified elements are updated. This is why a good test suites is needed, although you may have to update references in the test classes as well. An example of this is using the Push Down refactoring on a method. If another class has an instance of the class that originally had the method and makes a class to the method, after the Push Down refactoring there will be an error in the calling class because the method is no longer found in the original class. Refactoring

Resources Catalog of Refactorings. http://www.refactoring.com/catalog/index.html Refactoring Home Page. http://www.refactoring.com/ Martin Fowler. Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999. ISBN 0-201-48567-2. Kerievsky, Joshua. Refactoring To Patterns. Addison- Wesley, 2004. ISBN 0-321-21335-1. Wake, William C.. Refactoring Workbook. Addison-Wesley, 2003. ISBN 0-321-10929-5. Refactoring