Refactoring with inline temp, method, and class

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
About Me – Frank Xu Education ▫ North Dakota State University  Ph.D. in Software Engineering ▫ Towson University  MS in Computer Science ▫ Southeast.
Software Testing and Maintenance 1 Today’s Agenda  Course Evaluation  HW 4 Return  HW 5 Correction  Quiz 4 Next Class  Software Refactoring.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 2 Composing Methods Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 3 Moving Features Between Objects Steve Chenoweth Office Phone: (812) Cell: (937)
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
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
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
Refactoring – III Measured Smells. Smells Covered 1. Comments 2. Long method 3. Large Class 462.
Refactoring and its role in Test-Driven Development Miguel J. T. Pessoa Monteiro Escola Superior de Tecnologia de Castelo Branco.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
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.
Refactoring - A disciplined approach to rework for better design.
Refactoring Improving the structure of existing code Refactoring1.
Chapter 7: Bad Code Smells Omar Meqdadi SE 3860 Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Small changes to code to improve it. Refactoring Defined A change made to the internal structure of software to make it easier to understand and cheaper.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
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.
Hesam C. Esfahani Lecture 8 Code Refactoring CSC301-Winter 2011.
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
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. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
Today’s Agenda  More refactoring patterns Software Testing and Maintenance 1.
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.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Small changes to code to improve it. Refactoring Defined A change made to the internal structure of software to make it easier to understand and cheaper.
Refactoring1 Improving the structure of existing code.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
Refactoring Constants and Variables Lesson Three: Constants and Variables.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
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.
Code Refactoring Milan Vukoje Soprex SkfOffice2 SkfOffice3 Big5 Quality oriented We are hiring…
Catalog of Refactoring (1) Composing Methods. Code Smells Long methods Dubious temporary variables Dubious methods.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Catalog of Refactoring (6) Making Method Calls Simpler.
Refactoring Presentation Yash Pant. Overview 3 Refactoring Types: 1. Replace Error Code with Exception 2. Split Temporary Variable 3. Remove Middle Man.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Catalog of Refactoring
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Refactoring Methods: Kevin Murphy.
Introduction to Methods in java
Introduction to Refactoring
Refactorings Luke Marsh.
Starting Out with Java: From Control Structures through Objects
Software Construction and Evolution - CSSE 375 Composing Methods
Small changes to code to improve it
UML & Programming Martin Fowler.
Improving the structure of existing code
Generic programming in Java
Small changes to code to improve it
Code Animation Examples
Test Driven Lasse Koskela Chapter 3: Refactoring in Small Steps
Presentation transcript:

Refactoring with inline temp, method, and class Sung-Jun Lee

Inline temp Example: You have a temp that is assigned to once with a simple expression. double basePrice = anOrder.basePrice(); return (basePrice > 1000) Motivation: used in Replace temp with query

Inline temp(cont.) Solution: Replace all references to temps that are assigned once with a simple expression with the expression itself. return (anOrder.basePrice() > 1000) Benefits: Fixes problems that need to be fixed to do other types of refactoring

Inline Method Example: Make method name as clear as the body int getRating() {     return (moreThanFiveLateDeliveries()) ? 2 : 1;} boolean moreThanFiveLateDeliveries(){     return _numberOfLateDeliveries > 5;} Motivation: To get rid of pointless indirection and often good to do before other refactoring types such as replacing method with method object

Inline Method(cont.) Solution: Put the method’s body into the body of its callers and remove the method. int getRating() {     return (_numberOfLateDeliveries > 5)? 2 : 1; } Benefits: Makes the code clear and easy to read

Inline Class Example: A class is not doing much class Person...   public String getName() {       return _name;} public String getTelephoneNumber(){       return _officeTelephone.getTelephoneNumber(); private String _name; private TelephoneNumber _officeTelephone = new TelephoneNumber();

Inline class(cont.) class TelephoneNumber...   public String getTelephoneNumber() {       return ("(" + _areaCode + ") " + _number);   }   String getAreaCode() {return _areaCode;}   void setAreaCode(String arg) {_areaCode = arg;}   String getNumber() {return _number;}   void setNumber(String arg) {_number = arg;}   private String _number;   private String _areaCode;

Inline Class(cont.) Motivation: Reverse of extract class. Result of other refactoring methods Solutions:Move the features of class to another class then delete the old class class Person...   String getAreaCode() {return _officeTelephone.getAreaCode();}   void setAreaCode(String arg) {_officeTelephone.setAreaCode(arg);}   String getNumber() {return _officeTelephone.getNumber();}   void setNumber(String arg) {_officeTelephone.setNumber(arg);} Person martin = new Person(); martin.getOfficeTelephone().setAreaCode ("781"); becomes Person martin = new Person(); martin.setAreaCode ("781");

Inline Class(cont.) Benefits: Makes the program easier to read.