Imagine you ve written a piece of code but then accidentally deleted and lost it.

Similar documents
COURSE 11 DESIGN PATTERNS

Source Code Rejuvenation is not Refactoring

Adaptive Refactoring Using a Core-Based Clustering Approach

How We Refactor, and How We Know it. Emerson Murphy-Hill, Chris Parnin, Andrew P. Black ICSE 2009

Living and Working with Aging Software. Ralph Johnson. University of Illinois at Urbana-Champaign

Refactoring, 2nd Ed. A love story. Michael Hunger

Refactoring. Chen Tang March 3, 2004

A Study of Bad Smells in Code

Specification and Automated Detection of Code Smells using OCL

Beyond the Refactoring Browser: Advanced Tool Support for Software Refactoring

What Kinds of Refactorings are Co-occurred? An Analysis of Eclipse Usage Datasets

How We Refactor, and How We Know It

SAFEREFACTOR Tool for Checking Refactoring Safety

Objectives: On completion of this project the student should be able to:

Automated Behavioral Testing of Refactoring Engines

McCa!"s Triangle of Quality

Refactoring Framework for Instance Code Smell Detection

Refactoring (guest lecture)

Classification and Summarization of Software Refactoring Researches: A Literature Review Approach

Implementing evolution: Refactoring

Understading Refactorings

Software Reengineering P1: Intro & Organization. Martin Pinzger Delft University of Technology

Eclipse CDT refactoring overview and internals

Java. Murphy-Hill[1] Eclipse 90% 90% Fowler. Eclipse *2 Visual Studio *3 [2] Eclipse. Eclipse 26. Java. Eclipse

Software Design COSC 4353/6353 D R. R A J S I N G H

Reengineering II. Transforming the System

Refactoring Myths FOCUS: REFACTORING

Refactoring with Eclipse

Making Program Refactoring Safer

CSC 408F/CSC2105F Lecture Notes

Zjednodušení zdrojového kódu pomocí grafové struktury

DAT159 Refactoring (Introduction)

International Journal of Computer Science Trends and Technology (IJCST) Volume 5 Issue 2, Mar Apr 2017

COMP 354 TDD and Refactoring

SERG. Sort-based Refactoring of Crosscutting Concerns to Aspects

AUTO GENERATION OF CODE AND TABLE TOOL

Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring

Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004

A Catalog and Classification of Fortran Refactorings

Fascinating Observation Monitor-based Clamant Code Smell Detection Using Python

Abstract: Refactorings are used to improve the internal structure of software without changing its external behavior.

Analysis of the Test Driven Development by Example

Test-Driven Development Metodology Proposal for Web Service Choreographies

Overview of Eclipse Lectures. Module Road Map

SAFIRA: A Tool for Evaluating Behavior Preservation

Detecting Structural Refactoring Conflicts Using Critical Pair Analysis

Refactoring. Paul Jackson. School of Informatics University of Edinburgh

Making Program Refactoring Safer

Static rules of variable scoping in Erlang

Er. Himanshi Vashisht, Sanjay Bharadwaj, Sushma Sharma

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture

Implementing evolution: Refactoring

Sort-based Refactoring of Crosscutting Concerns to Aspects

CSE 403 Lecture 21. Refactoring and Code Maintenance. Reading: Code Complete, Ch. 24, by S. McConnell Refactoring, by Fowler/Beck/Brant/Opdyke

Editing Code. SWE 795, Spring 2017 Software Engineering Environments

Template-based Reconstruction of Complex Refactorings

Prototype Environment for Refactoring Clean Programs

International Journal of Scientific & Engineering Research, Volume 8, Issue 2, February ISSN

Outline. Software Rots

AOC: An Aspect Oriented Approach for Comment to Reduce the Redundant Behavior between Source Code and Compilation Code

DCC / ICEx / UFMG. Software Code Clone. Eduardo Figueiredo.

Restructuring. What is restructuring? Tudor Gîrba Reengineering life cycle. What? forward engineering. reverse engineering

Lab Exercise Refactoring using Eclipse

Searching for Refactoring Opportunities to apply the Strategy Pattern

HOW AND WHEN TO FLATTEN JAVA CLASSES?

Course December Adrian Iftene

C++- und Ruby- und... Refactoring für Eclipse

AntiPatterns. EEC 421/521: Software Engineering. AntiPatterns: Structure. AntiPatterns: Motivation

Tool Support for Complex Refactoring to Design Patterns

Tutorial 02: Writing Source Code

Challenges in Model Refactoring

Identifying overly strong conditions in refactoring implementations

Finding Extract Method Refactoring Opportunities by Analyzing Development History

Towards the integration of security patterns in UML Component-based Applications

Test Driven Development TDD

Martin P. Robillard and Gail C. Murphy. University of British Columbia. November, 1999

Model refactoring within a Sequencer TOMAŽ KOS 1, TOMAŽ KOSAR 2, JURE KNEZ 1, MARJAN MERNIK 2

Coding and Scheduling for Efficient Loss-Resilient Data Broadcasting

Visualizing Software Metrics for increased Refactoring

A Source Code History Navigator

Investigating the Effectiveness of Greedy Algorithm on Open Source Software Systems for Determining Refactoring Sequence

FaultBuster: An Automatic Code Smell Refactoring Toolset

Keywords Clone detection, metrics computation, hybrid approach, complexity, byte code

Refactoring Tools: Fitness for Purpose

Microservice Splitting the Monolith. Software Engineering II Sharif University of Technology MohammadAmin Fazli

An experience report on using code smells detection tools

Web Applications: A Simple Pluggable Architecture for Business Rich Clients

CPEG 852 Advanced Topics in Computing Systems The Dataflow Model of Computation

Refactoring and Detection of Bad Smells of Coding Using Larger Scale and Critical Incident Technique

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Improving Usability of Refactoring Tools

On Refactoring for Open Source Java Program

Kerievsky_book.fm Page 355 Thursday, July 8, :12 PM. Index

Refactoring via Database Representation

Software Engineering I (02161)

Inheritance Metrics: What do they Measure?

IJREAS Volume 2, Issue 2 (February 2012) ISSN: SOFTWARE CLONING IN EXTREME PROGRAMMING ENVIRONMENT ABSTRACT

SOFTWARE ENGINEERING SOFTWARE EVOLUTION. Saulius Ragaišis.

Software Refactoring Approaches: A Survey

IDENTIFICATION OF PROMOTED ECLIPSE UNSTABLE INTERFACES USING CLONE DETECTION TECHNIQUE

Transcription:

Why Refactor? Imagine you ve written a piece of code but then accidentally deleted and lost it. Questions: How much time would it take you to reconstruct from scratch what you had the same amount, or more, or less? Would the code have a better design the second time you write it? Imagine you are doing software maintenance. Observations: It s harder to maintain (someone else s) code than it is to write new code. Most developers hope that they won t have to deal with code maintenance. Evolving/maintaining code is what most developers do most of the time. Good engineering: keep code simple and easy to understand. Problem: code is hard to evolve and maintain Solution: refactor to keep it simple Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Martn Fowler November 2011 CSC7302: Testing & Metrics Refactoring.1

What is Refactoring? [Refactoring is] the 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 structure Martin Fowler Changes made to a system that: Do not change observable behavior Remove duplication or needless complexity Enhance software quality Make the code easier and simpler to understand Make the code more flexible Make the code easier to change, etc NOTE: the importance of Tests Metrics November 2011 CSC7302: Testing & Metrics Refactoring.2

Why Refactor?. Revisited Long-term investment in the quality of the code and its structure No refactoring may save costs / time in the short term but incurs a huge penalty in the long run Why fix if it ain t broken? Every module has three functions: 1. To execute according to its purpose 2. To afford change 3. To communicate to its readers If it does not do one or more of these, it is broken. November 2011 CSC7302: Testing & Metrics Refactoring.3

When to Refactor? 1. NOT: 2 weeks every 6 months 2. Do it as you develop - Opportunistic Refactoring 3. Boy Scout principle: leave it better than you found it. 4. If you recognize a warning sign (a bad smell) I. When you add a function a) Before, to start clean and/or b) After, to clean-up II. When you fix a bug III. When you code review IV. You can use The Rule of Three (Fowler XP) a) The first time, just do it! b)! Need it somewhere else? Cut and paste it! c)! The third time, refactor! November 2011 CSC7302: Testing & Metrics Refactoring.4

When Not to Refactor? 1. When the tests are failing 2. When you should just rewrite the code 3. When you have impending deadlines How to Refactor? November 2011 CSC7302: Testing & Metrics Refactoring.5

Bad Smells in Code: (Refactoring, chp3, Martin Fowler) November 2011 CSC7302: Testing & Metrics Refactoring.6

Bad Smells in Code: (Refactoring, chp3, Martin Fowler) November 2011 CSC7302: Testing & Metrics Refactoring.7

Refactoring Drawbacks When taken too far Incessant tinkering with code Trying to make it perfect Attempting refactoring when the tests don t work or without tests can lead to dangerous situations! Refactoring published interfaces propagates to external users relying on these interfaces November 2011 CSC7302: Testing & Metrics Refactoring.8

Why Developers Fear Refactoring? 1. I don t understand the code enough to do it 2. Short-term focus (Adding a new working feature is cooler!) 3. Not paid for overhead tasks such as refactoring? Solutions: 1. Test, test and test again, with good test code. 2. Learn to appreciate elegant code (and find good metrics) 3. Teach the benefits of better code (to your colleagues) November 2011 CSC7302: Testing & Metrics Refactoring.9

Recommended Reading Susan L. Gerhart. 1975. Correctness-preserving program transformations. In Proceedings of the 2nd ACM SIGACT-SIGPLAN symposium on Principles of programming languages(popl '75). ACM, New York, NY, USA, 54-66. DOI=10.1145/512976.512983 David B. Loveman. 1977. Program Improvement by Source-to-Source Transformation. J. ACM24, 1 (January 1977), 121-145. DOI=10.1145/321992.322000 R.J.R. Back, On correct refinement of programs, Journal of Computer and System Sciences, Volume 23, Issue 1, August 1981, Pages 49-68, ISSN 0022-0000, 10.1016/0022-0000(81)90005-2. William F. Opdyke. 1992. Refactoring Object-Oriented Frameworks. Ph.D. Dissertation. Universityof Illinois aturbana-champaign, Champaign, IL, USA. UMI Order No. GAX93-05645. Fowler, Beck, Bravnt, Opdyke, Roberts, Refactoring: Improving the Design of Existing Code. Addison- Wesley Longman Publishing Co., Inc., Boston, MA, USA., 1999, ISBN: 978-0201485677. (Chapter3: Bad Smells In Code, p75-87, Fowler, Beck) Lance Tokudaand Don Batory. 1999. Evolving Object-Oriented Designs with Refactorings. In Proceedings of the 14th IEEE international conference on Automated software engineering (ASE '99). IEEE Computer Society, Washington, DC, USA. (From PhD Thesis, Tokuda: University of Texas at Austin) November 2011 CSC7302: Testing & Metrics Refactoring.10

Recommended Reading Frank Simon, Frank Steinbruckner, and Claus Lewerentz. 2001. MetricsBasedRefactoring. In Proceedings of the Fifth European Conference on Software Maintenance and Reengineering (CSMR '01). IEEE Computer Society, Washington, DC, USA. Arie van DeursenLeonMoonenAlex van den Bergh Gerard Kok, Refactoring Test Code, Technical Report, Software Improvement Group, The Netherlands, 2001 E. Van Emden and L. Moonen. 2002. Java Quality Assurance by Detecting Code Smells. In Proceedings of the Ninth Working Conference on Reverse Engineering (WCRE'02)(WCRE '02). IEEE Computer Society, Washington, DC, USA Tom Mensand Tom Tourwé; 2004. A Survey of Software Refactoring. IEEE Trans. Softw. Eng. 30, 2 (February 2004), 126-139. DOI=10.1109/TSE.2004.1265817 Konstantinos Stroggylosand DiomidisSpinellis. 2007. Refactoring--DoesIt ImproveSoftware Quality?. In Proceedings of the 5th International Workshop on Software Quality(WoSQ'07). IEEE Computer Society, Washington, DC, USA, 10-. DOI=10.1109/WOSQ.2007.11 Robert M. Fuhrer, Markus Keller, Adam Kiezun: Advanced Refactoring in the Eclipse JDT: Past, Present, and Future. WRT 2007: 30-31 Emerson Murphy-Hill, Chris Parnin, and Andrew P. Black. 2009. How we refactor, and how we know it. In Proceedings of the 31st International Conference on Software Engineering (ICSE '09). IEEE Computer Society, Washington, DC, USA, 287-297. DOI=10.1109/ICSE.2009.5070529 November 2011 CSC7302: Testing & Metrics Refactoring.11

Java Refactoring Built-in to Eclipse Source - Leif Frenzel: The Language Toolkit: An API for Automated Refactorings in Eclipsebased IDEs, http://www.eclipse.org/articles/article-ltk/ltk.html Eclipse provides a general-purpose API for implementing refactorings that can be applied to any Eclipse workspace elements, from text resources to whole projects. Several plugins for Eclipse draw upon the API to implement refactorings for specific languages; here we will focus on refactorings implemented for Java. The Eclipse refactoring API, part of the Language Toolkit (LTK), is implemented within the org.eclipse.ltk.core.refactoring and org.eclipse.ltk.ui.refactoring plug-ins since R3.0 The API for refactoring provides a process-level abstraction upon which specific refactorings may be built November 2011 CSC7302: Testing & Metrics Refactoring.12

Java Refactoring Built-in to Eclipse Dependencies between refactoring API elements November 2011 CSC7302: Testing & Metrics Refactoring.13

Java Refactoring Built-in to Eclipse A refactor implenter must extend the abstract class org.eclipse.ltk.core.refactoring.refactoring There is a life cycle associated to such refactorings: A refactoring ultimately produces a single implementation of the abstract class Change that describes the workspace changes necessary to accomplish the refactoring. Implementers of refactorings must re-use or implement their own classes derived from Change to specify the behavior of a change. November 2011 CSC7302: Testing & Metrics Refactoring.14

Java Refactoring Built-in to Eclipse The Java part of Eclipse, JDT, is able to perform several types of automatic refactorings on Java projects, classes, and their members. There are several ways to quickly select a refactoring for an element in a Java project. If you look in the Refactor dropdown menu in Eclipse, you will notice four sections: The first section has Undo and Redo in it The other three sections contain the three different types of refactorings available in Eclipse 1. contains refactorings that change the physical structure of the code and classes such as Rename and Move. 2. contains refactorings that change the code structure on a class level such as Pull Up and Push Down. 3. contains refactorings that change the code within a class such as Extract Method and Encapsulate Field November 2011 CSC7302: Testing & Metrics Refactoring.15

Java Refactoring Built-in to Eclipse Type 1 Physical Structure Rename Move Change Method Signature Convert Anonymous Class to Nested Convert Nested Type to Top Level (Eclipse 2 only) Move Member Type to New File (Eclipse 3 only) Type 2 Class Level Structure Push Down Pull Up Extract Interface Generalize Type (Eclipse 3 only) User Supertype Where Possible Type 3 Structure inside a Class Inline Extract Method Extract Local Variable Extract Constant Introduce Parameter (Eclipse 3 only) Introduce Factory (Eclipse 3 only) Encapsulate Field TO DO: Experiment withall the refactoringsthatare already in your version of Eclipse. You should become familiarwithall of them. November 2011 CSC7302: Testing & Metrics Refactoring.16

Refactoring Catalog (http://refactoring.com/catalog/index.html) November 2011 CSC7302: Testing & Metrics Refactoring.17