12-Jul-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.

Slides:



Advertisements
Similar presentations
A practical guide John E. Boal TestDrivenDeveloper.com.
Advertisements

Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
Group that looked at Miami syllabus. Current Syllabus (with regards to testing) 2: To be able to identify and eliminate errors in programs – 2.1: Describe.
Software Evolution and Refactoring July Introduction Any changes on software by developers or users These changes are costly How we can design.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) Cell: (937)
The Java Programming Language
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Computer Engineering 203 R Smith Agile Development 1/ Agile Methods What are Agile Methods? – Extreme Programming is the best known example – SCRUM.
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
Slide 1. Slide 2 Administrivia Nate's office hours are Wed, 2-4, in 329 Soda! TA Clint will be handing out a paper survey in class sometime this week.
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.
Refactoring to Patterns Ravindra Chilaka Extreme Programming Seminar XP 2004 b.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
15-Aug-15 Design Patterns and Refactoring. 2 History A Pattern Language: Towns, Buildings, Construction, Christopher Alexander, 1977 The Timeless Way.
21-Aug-15 Design Patterns and Refactoring. 2 History A Pattern Language: Towns, Buildings, Construction, Christopher Alexander, 1977 The Timeless Way.
Refactoring Lecture 5 CIS 6101 Software Processes and Metrics.
Test Driven Development An approach to writing better code Jimmy Zimmerman Intel Corporation.
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.
1 Design and Integration: Part 1 Nuggets about Design vs Project Management.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 3 Introduction to Collections – Stacks Modified
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Introduction to Refactoring. Refactoring Refactoring is: –restructuring (rearranging) code in a series of small, semantics- preserving transformations.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
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.
Chapter 7: Bad Code Smells Omar Meqdadi SE 3860 Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
17-Oct-15 Design Patterns and Refactoring CSC 2053.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
Best Practices. Contents Bad Practices Good Practices.
Refactoring1 Improving the structure of existing code.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 Design and Integration: Part 2. 2 Plus Delta Feedback Reading and lecture repeat Ambiguous questions on quizzes Attendance quizzes Boring white lecture.
Refactoring & Testability. Testing in OOP programming No life in flexible methodologies and for refactoring- infected developers without SOME kind of.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse Student: Alexander Libov Supervisor: Dr. Ran Ettinger, IBM.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
Extreme Programming. Extreme Programming (XP) Formulated in 1999 by Kent Beck, Ward Cunningham and Ron Jeffries Agile software development methodology.
Refactoring1 Improving the structure of existing code.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
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.
10-Jun-16.  Refactoring is: ◦ restructuring (rearranging) code... ◦...in a series of small, semantics-preserving transformations (i.e. the code keeps.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Working with Java.
Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring III 27-Nov-18.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Coding Concepts (Basics)
Refactoring Types Blake Duncan.
An Example of Interacting Classes
Refactoring.
Focus of the Course Object-Oriented Software Development
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

12-Jul-15 Refactoring

2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code keeps working)......in order to make the code easier to maintain and modify Refactoring is not just any old restructuring You need to keep the code working You need small steps that preserve semantics You need to have unit tests to prove the code works There are numerous well-known refactoring techniques You should be at least somewhat familiar with these before inventing your own

3 When to refactor You should refactor: Any time that you see a better way to do things “Better” means making the code easier to understand and to modify in the future You can do so without breaking the code Unit tests are essential for this You should not refactor: Stable code (code that won’t ever need to change) Someone else’s code Unless you’ve inherited it (and now it’s yours)

4 Design vs. coding “Design” is the process of determining, in detail, what the finished product will be and how it will be put together “Coding” is following the plan In traditional engineering (building bridges), design is perhaps 15% of the total effort In software engineering, design is 85-90% of the total effort By comparison, coding is cheap

5 The refactoring environment Traditional software engineering is modeled after traditional engineering practices (= design first, then code) Assumptions: The desired end product can be determined in advance Workers of a given type (plumbers, electricians, etc.) are interchangeable “Agile” software engineering is based on different assumptions: Requirements (and therefore design) change as users become acquainted with the software Programmers are professionals with varying skills and knowledge Programmers are in the best position for making design decisions Refactoring is fundamental to agile programming Refactoring is sometimes necessary in a traditional process, when the design is found to be flawed

6 A personal view In my opinion, Design, because it is a lot more creative than simple coding, is also a lot more fun Admittedly, “more fun” is not necessarily “better”...but it does help you retain good programmers Most small to medium-sized projects could benefit from an agile programming approach We don’t yet know about large projects Most programming methodologies attempt to turn everyone into a mediocre programmer Sadly, this is probably an improvement in general These methodologies work less well when you have some very good programmers

7 Back to refactoring When should you refactor? Any time you find that you can improve the design of existing code You detect a “bad smell” (an indication that something is wrong) in the code When can you refactor? You should be in a supportive environment (agile programming team, or doing your own work) You should have an adequate set of unit tests

8 Example 1: switch statements switch statements are very rare in properly designed object-oriented code Therefore, a switch statement is a simple and easily detected “bad smell” Of course, not all uses of switch are bad A switch statement should not be used to distinguish between various kinds of object There are several well-defined refactorings for this case The simplest is the creation of subclasses

9 Example 1, continued class Animal { final int MAMMAL = 0, BIRD = 1, REPTILE = 2; int myKind; // set in constructor... String getSkin() { switch (myKind) { case MAMMAL: return "hair"; case BIRD: return "feathers"; case REPTILE: return "scales"; default: return "integument"; } } }

10 Example 1, improved class Animal { String getSkin() { return "integument"; } } class Mammal extends Animal { String getSkin() { return "hair"; } } class Bird extends Animal { String getSkin() { return "feathers"; } } class Reptile extends Animal { String getSkin() { return "scales"; } }

11 How is this an improvement? Adding a new animal type, such as Amphibian, does not require revising and recompiling existing code Mammals, birds, and reptiles are likely to differ in other ways, and we’ve already separated them out (so we won’t need more switch statements) We’ve gotten rid of the flags we needed to tell one kind of animal from another Basically, we’re now using Objects the way they were meant to be used

12 JUnit tests As we refactor, we need to run JUnit tests to ensure that we haven’t introduced errors public void testGetSkin() { assertEquals("hair", myMammal.getSkin()); assertEquals("feathers", myBird.getSkin()); assertEquals("scales", myReptile.getSkin()); assertEquals("integument", myAnimal.getSkin()); } This should work equally well with either implementation The setUp() method of the test fixture may need to be modified JUnit is discussed in a separate lecture

13 Bad Smell Examples We should refactor any time we detect a “bad smell” in the code Examples of bad smells include: Duplicate Code Long Methods Large Classes Long Parameter Lists Multi location code changes Feature Envy Data Clumps Primitive Obsession

14 The End