about me – frank xu

36
About Me – Frank Xu Education North Dakota State University Ph.D. in Software Engineering Towson University MS in Computer Science Southeast Missouri State University BS in Computer Science, Minor in Math Working Experience GE Transportation, 2008- present, Consultant Gannon University, 2008- present Director of Keystone Software Development Institute, Assistant Professor of SE University VA –Wise, 2007- 2008 Assistant Professor of Software Engineering Swanson Health Products, MIS department, 2005 ~ 2007 Sr. Programmer Analyst Volt Information Science Inc., 2004 ~ 2005 Software Engineer

Upload: yitta

Post on 25-Feb-2016

45 views

Category:

Documents


2 download

DESCRIPTION

About Me – Frank Xu. Education North Dakota State University Ph.D. in Software Engineering Towson University MS in Computer Science Southeast Missouri State University BS in Computer Science, Minor in Math Working Experience GE Transportation, 2008- present, Consultant - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: About Me – Frank Xu

About Me – Frank Xu• Education

▫ North Dakota State University Ph.D. in Software Engineering

▫ Towson University MS in Computer Science

▫ Southeast Missouri State University BS in Computer Science, Minor in Math

• Working Experience▫ GE Transportation, 2008- present,

Consultant▫ Gannon University, 2008- present

Director of Keystone Software Development Institute, Assistant Professor of SE▫ University VA –Wise, 2007- 2008

Assistant Professor of Software Engineering▫ Swanson Health Products, MIS department, 2005 ~ 2007

Sr. Programmer Analyst ▫ Volt Information Science Inc., 2004 ~ 2005

Software Engineer

Page 2: About Me – Frank Xu

Refactoring- A disciplined approach to rework for better design.

Page 3: About Me – Frank Xu

Objectives•What is refactoring?•Why should I refactor? •When should I refactor?•How to refactor?

Page 4: About Me – Frank Xu

Definition•Refactoring is a disciplined technique for

restructuring an existing body of code, ▫altering its internal structure ▫without changing its external behavior.

• It makes the software easier to understand and cheaper to modify.

Page 5: About Me – Frank Xu

A simple example•Example: compute gravitational potential energy

▫PEgrav = mass * g * height

double potentialEnergy(double mass, double height) { return mass * height * 9.81; }

static final double GRAVITATIONAL_CONSTANT = 9.81;

double potentialEnergy(double mass, double height) { return mass * height * GRAVITATIONAL_CONSTANT; }

Magic number

assertEquals(9.81, new RefactoringDemo().potentialEnergy(1,1),0.0);

Page 6: About Me – Frank Xu

Properties of refactoring

•One step at a time

•Preserve correctness

•Frequent testing

Page 7: About Me – Frank Xu

Why refactor?• Improves the design of software.

▫Without refactoring, the design of the program will decay

•Makes software easier to understand.▫A good design is easy to understand

•Helps you find bugs.▫The clarification process helps find bugs

•Helps you program faster.▫Poor design slow you down

Page 8: About Me – Frank Xu

When to Refactor? •As you develop

▫Example: change a variable name to something more meaningful.

•Before adding functions.▫Sometimes the existing design does not allow you to easily

add the feature.•When you need to fix a bug

▫The bug exists because the code was not clear enough for you to see the bug in the first place.

•When you do a code review▫Code reviews help spread knowledge through the

development team.▫Works best with small review groups

Page 9: About Me – Frank Xu

Steps to refactoring

•Identifying bad smells of code▫a “bad smell” = a warning sign in the code▫e.g., time consuming code

•Designing solid tests ▫for the section of code under analysis.

•Refactoring the code ▫based on the type of the smell

•Applying tests.

Page 10: About Me – Frank Xu

Bad Smells in Code•Duplicated Code•Long Method•Large Class•Long Parameter List•Divergent Change•Shotgun Surgery•Feature Envy•Data Clumps•Primitive Obsession•Switch Statements

• Parallel Interface Hierarchies• Lazy Class• Speculative Generality• Temporary Field• Message Chains • Middle Man• Inappropriate Intimacy• Incomplete Library Class• Data Class• Refused Bequest

Page 11: About Me – Frank Xu

Bad smells in code•Smells within classes

▫Duplicated code▫Long method▫Large class▫…

•Smells between classes▫Primitive obsession ▫ Inappropriate intimacy▫Middle man▫…

Page 12: About Me – Frank Xu

Duplicated Code – (1)•“The #1 bad smell”

▫Copy & paste▫What if duplicates changes

•Refactoring solutions ▫Pull up a field▫Form a template method▫Substitute algorithm

Page 13: About Me – Frank Xu

Duplicated Code – (2)• Bad Smell

▫ Two subclasses have the same field.• Refactoring

▫ Pull up a field: Move the field to the superclass.

Salesman Engineer

Employee

name name

Page 14: About Me – Frank Xu

Duplicated Code – (3)•Bad Smell

▫You have two methods in subclasses that perform similar steps in the same order, yet the steps are different.

•Refactoring: Form a template method▫Get the steps into methods with the same

signature, so that the original methods become the same. Then you can pull them up.

Page 15: About Me – Frank Xu

Duplicated Code – (4)

PersonalCustomer

CorporateCustomer

Customer

getBillableAmt() getBillableAmt()

double base=unit*ratedouble tax=base*Site.TAX_RATE;return base+tax

double base=unit*rate*0.5double tax=base*Site.TAX_RATE*0.2;return base+tax

getBaseAmt()getTaxAmt()

getBaseAmt()getTaxAmt()

return getBaseAmt()+getTaxAmt()

getBaseAmt()getTaxAmt()

Page 16: About Me – Frank Xu

Duplicated Code – (5)• Bad smell

▫ Similar logic• Refactoring: Substitute algorithm

▫ Replace the body of the method with the new algorithm.

duplications

Page 17: About Me – Frank Xu

Long Method – (1)•The longer the method the harder it is to see what

it’s doing. ▫Poorly thought out abstractions and boundaries

•Refactoring solutions▫Extract method▫Replace temp with query▫ Introduce parameter object▫Preserve whole object

Page 18: About Me – Frank Xu

Long Method – (2)•Extract method

▫ break up into smaller private methods within the class

• Example

private void m1(){Statement 1;..Statement 9;}

private void m2(){Statement 10;..Statement 19;}

private void m3(){Statement 20;..Statement 30;}

public void methodA(){

}

public void methodA(){

Statement 1;Statement 2;..Statement 30;} m1();

m2();m3();

Page 19: About Me – Frank Xu

Long Method – (3)• Bad smell

▫using a temporary variable to hold the result of an expression.

▫Refactoring: Replace temp with query ▫Extract the expression into a

method.

if (basePrice() > 1000) { return basePrice() * 0.95;} else { return basePrice() * 0.98; }//any side effects?

Temp variable expression

double basePrice=basePrice();if (basePrice > 1000) { return basePrice * 0.95;} else { return basePrice * 0.98; }

Page 20: About Me – Frank Xu

Long Method – (4)•Smell: a method with a long list of parameters

▫A group of parameters that naturally go together.•Refactoring: Introduce parameter object

▫Replace them with the object.

amountInvoicedIn ( )amountReceivedIn ( )amountOverdueIn ( )

Customer start: Dateend: Date

DateRangeDateRange

Start: Date, end: DateStart: Date, end: DateStart: Date, end: Date

DateRangeDateRange

Page 21: About Me – Frank Xu

Long Method – (5)•Smell

▫You are getting several values from an object and passing these values as parameters in a method call.

•Refactoring: Preserve whole object▫Send the whole object instead.

Page 22: About Me – Frank Xu

Large Class – (1)•A class that is trying to do too much

▫Can usually be identified by looking at how many instance variables it has.

▫When a class has too many instance variables, duplicated code cannot be far behind.

•Refactoring solution▫Extract class▫Extract subclass

Page 23: About Me – Frank Xu

PhoneNumber

Large Class – (2)•Bad smell

▫Have one class doing work that should be done by two.•Refactoring: Extract class

▫Need create a new class and move the relevant fields and methods from the old class into the new class.

Customer

nameareaCodenumber

String: getPhoneNumber()

PhoneNumber: getPhoneNumber()

Page 24: About Me – Frank Xu

LaborItem

Large Class – (3)• Smell

▫ A class has features that are used only in some instances.▫Extract subclass

▫ Create a subclass for that subset of features.

JobItem

getTotalPrice()getUnitPrice()getEmployee()

Do all the JobItem objects need to have getEmployee

function?

getUnitPrice()

isLabor

Page 25: About Me – Frank Xu

Primitive Obsession – (1)•Over use primitive to represent data

▫All properties of a class are primitive types int, String, boolean, double, etc.

▫Primitive difficult to represent data money (which combines quantity and currency) a date range object

• Refactorings▫Replace data value with object ▫Replace type code with class

Page 26: About Me – Frank Xu

Primitive Obsession – (2)•Smell

▫You have a data item that needs additional data or behavior.

•Refactoring▫Turn the data item into an object.

Order

customer: String

Customer

id: Stringlast Name:: StringmiddleName: StringfirstName: Stringphone: PhoneNumber

Page 27: About Me – Frank Xu

Primitive Obsession – (3)•Smell

▫A class has a numeric type code that does not affect its behavior.

•Refactoring: Replace type code with class ▫Replace the number with a new class.

Person

O: intA: intB:intAB:int

BloodType

O: BloodTypeA: BloodTypeB: BloodTypeAB: BloodType

bloodType:int

public class Person{ …… int bloodType =BloodType.O; …….}

public class BloodType{ public static final int O 1; public static final int A 2; public static final int B 3; public static final int AB 4;}

public enum BloodType { O,A,B,AB}

Page 28: About Me – Frank Xu

Inappropriate Intimacy – (1)•Two classes are overly entertwined

▫Sharing of secrets between classes▫Leads to data coupling

•Refactorings▫Hide delegate▫Replace inheritance with delegation

Page 29: About Me – Frank Xu

What is problem? Change to Delegate will propagate to the

cleint

Inappropriate Intimacy – (2)•Motivation

▫A client is calling a delegate class of an object.•Refactoring: Hide delegate

▫Create methods on the server to hide the delegate.

Client Server

Delegate

taskA() taskA()

public void method(){ delegate.taskA();}

Client Delegate

taskA()

Page 30: About Me – Frank Xu

Inappropriate Intimacy – (3)

ClientClass

Employee

Department

getDepartment() getManager()

public class Department{ private Employee manager; …… public Department (Employee manager){ this.manager=manager; } public string getManager(){ return manager; } …..}

manager=john. Object need to know less about other parts of the

system

public getManager(){ return department.getManager();}

getManager()

getDepartment().

getManager();

Page 31: About Me – Frank Xu

Inappropriate Intimacy – (3)• Motivation

▫ A subclass uses only part of a superclasses interface or does not want to inherit data.

• Refactoring: Replace inheritance with delegation▫ Create a field for the superclass, adjust methods to delegate to the

superclass, and remove the subclassing.

Page 32: About Me – Frank Xu

Middle Man• Smell

• A method's body is just as clear as its name • Refactoring

▫ Put the method's body into the body of its callers and remove the method

int getRating() { return (moreThanFiveLateDeliveries()) ? 2 :

1; }

boolean moreThanFiveLateDeliveries() { return _numberOfLateDeliveries > 5;

}

int getRating() { return (_numberOfLateDeliveries > 5) ? 2 : 1;

}

Page 33: About Me – Frank Xu

Why might you still not refactor your programs?•You might not understand how to refactor.

• If the benefits are long-term, why exert the effort now? In the long term, you might not be with the project to reap the benefits!

•Refactoring code is an overhead activity; you're paid to write new features.

•Refactoring might break the existing program.

Page 34: About Me – Frank Xu

Summary•Refactoring is a disciplined approach to rework

for better design.•Refactor when code smells.•Take advantage of IDE (Eclipse/IntelliJ/ Java

Studio).•Check online resources for updated refactoring.•Know refactoring before your interview.

Page 35: About Me – Frank Xu

References•http://wiki.java.net/bin/view/People/Smells

ToRefactorings•https://netfiles.uiuc.edu/dig/RefactoringIn

fo/•http://www.refactoring.com/

Page 36: About Me – Frank Xu

Questions?