meljun cortes java refactoring

Upload: meljun-cortes-mbampa

Post on 02-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    1/30

    Refactoring

    Improving the Design of Existing Code

    MELJUN CORTES MBA MPA BSCS

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    2/30

    Topic Coverage

    Background of refactoring

    What is Refactoring?

    Where it came from Principles in Refactoring

    The Two Hats Metaphor

    Why and When

    Refactoring and Design

    Refactoring and Performance

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    3/30

    Topic Coverage (cont.)

    Bad Smells

    What is code smell?

    Types of code smells Types of Refactoring

    Types of refactoring.

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    4/30

    Background of

    Refactoring

    What is Refacto r ing?

    Where it came from

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    5/30

    What is Refactoring?

    Disciplined way to code

    Over time code will be modified

    Changing code in small steps

    Verify no change in external behavior by

    Testing

    Formal code analysis by tool

    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.

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    6/30

    Where Refactoring Came

    From?

    Ward Cunningham and Kent Beck

    Smalltalk style

    Ralph Johnson at University of Illinois at

    Urbana-Champaign Bill Opdykes Thesis

    ftp://st.cs.uiuc.edu/pub/papers/refactoring/opdyke-thesis.ps.Z

    John Brant and Don Roberts: TheRefactoring Browser

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    7/30

    Principles in

    Refactoring

    The Two Hats Metapho r

    Why and When

    Refactor ing and Design

    Refacto r ing and Perform ance

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    8/30

    The Two Hats Metaphor

    Adding Function

    Add new capabilities

    to the system

    Adds new tests

    Get the test working

    Refactoring

    Does not add any

    new features

    Does not add tests

    (but may change some)

    Restructure the code

    to remove redundancy

    Swap frequently between the hats,

    bu t only w ear on e at a time

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    9/30

    Why Refactor?

    To improve the software design

    Combats bit rot

    Makes the program easier to change

    To make the software easier to understand Write for people, not the compiler

    Understand unfamiliar code

    To help find bugs

    Refactor while debugging to clarify the code

    Refactoring helps you program faster.

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    10/30

    When to Refactor?

    To add new functionality

    Refactor existing code until youunderstand it

    Refactor the design to make it easy toadd

    To find bugs

    Refactor to understand the code

    For code reviews Immediate effect of code review

    Allows for higher level suggestions

    Dont set aside time for refactoring,

    inc lude i t in you r norm al act iv i t ies

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    11/30

    Refactoring and Design

    Special role as a complement to

    design.

    Refactoring can be an alternative to

    upfront design

    Refactoring can lead to simpler

    design without sacrificing flexibility

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    12/30

    Refactoring and

    Performance

    Refactoring make software go more

    slowly Refactoring makes the software more

    amenable to performance tuning

    Well refactored program gives you time to

    spend on performance tuning Well refactored program you have finer

    granularity for you performance analysis

    The best way to optimize performance is to first write

    a well factored program, then optimize it.

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    13/30

    Types of Refactoring

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    14/30

    Extract Method

    You have a code fragment that can be grouped togetherTurn the fragment into a method whose name explains the

    Purpose of the method.

    void printOwing(double amount){printBanner();

    //printDetailsSystem.out.println(name:+ _name);System.out.println(amount:+ amount);

    }

    void printOwing(double amount){

    printBanner();printDetails(amount);

    }

    void printDetails(double amount){

    System.out.println(name:+ _name);

    System.out.println(amount:+ amount);}

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    15/30

    Inline Method

    A methods body is just as clear as its name

    Put the methods body in 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;}

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    16/30

    Inline Temp

    You have a temp that assigned to once with a simple expression,

    and the temp is getting in the way of other refactorings.

    Replace all references to that temp with the expression

    double basePrice anOrder.basePrice();return (basePrice > 1000)

    return (anOrder.basePrice() > 1000)

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    17/30

    Replace Temp with Query

    You are using a temporary variable to hold the

    result of an expression

    Extract the expression into a method. Replace all references to the temp

    with the expression. The new method can then be

    used in other methods.double basePrice = _quantity * _itemPrice;if ( basePrice > 1000)

    return basePrice * 0.95;else

    return basePrice * 0.98;

    if (basePrice() > 1000)return basePrice() * 0.95;

    elsereturn basePrice() * 0.98;

    ...double basePrice(){

    return _quantity * _itemPrice;}

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    18/30

    Introduce Explaining

    Variable

    You have a complicated expression

    Put the result of the expression, or parts of the expression, in a

    temporary variable with a name that explains the purpose.

    if ( (platform.toUpperCase().indexOf(MAC) > -1) &&(browser.toUpperCase().indexOf(IE) > -1 &&

    wasInitialized() && resize > 0 ) {

    // do something ...}

    final boolean isMacOs = platform.toUpperCase().indexOf(MAC) > -1;final boolean isIEBrowser = platform.toUpperCase().indexOf(IE) > -1;

    final boolean wasResized = resize > 0;

    if (isMacOs && isIEBrowser && wasInitialized() && wasResized) {

    //do something}

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    19/30

    Move Method

    A method is, or will be, using or used by more

    features of another class that the class on which it

    is defined

    Create a new method with a similar body in the class it uses most.

    Either turn the old method into a simple delegation, or

    remove it altogether.

    Class 1

    aMethod()

    Class 2

    Class 1

    Class 2

    aMethod()

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    20/30

    Move Field

    A field is, or will be, used by another class more than the class

    on which it is defined.

    Create a new field in the target class, and change all its users.

    Class 1

    aField

    Class 2

    Class 1

    Class 2

    aField

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    21/30

    Extract Class

    You have one class doing work that should be done by two.

    Create a new class and move the relevant fields and methods

    From the old class into the new class

    Person

    nameofficeAreaCode

    officeNumber

    getTelephoneNumber()

    Person

    name

    getTelephoneNumber()

    Telephone Number

    areaCode

    number

    getTelephoneNumber()

    officeTelephone

    1

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    22/30

    Inline Class

    A class isnt doing very much

    Move all its features into another class and delete it.

    Person

    nameofficeAreaCode

    officeNumber

    getTelephoneNumber()

    Person

    name

    getTelephoneNumber()

    Telephone Number

    areaCode

    number

    getTelephoneNumber()

    officeTelephone

    1

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    23/30

    Pull Up Field

    Two subclasses have the same field

    Move the field to the superclass.

    Employee

    Salesman

    name

    Engineer

    name

    Employee

    name

    EngineerSalesman

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    24/30

    Pull Up Method

    You have methods with identical results on subclass

    Move them to the superclass

    Employee

    Salesman

    getName()

    Engineer

    getName()

    Salesman

    Employee

    getName()

    Engineer

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    25/30

    Pull Up Constructor Body

    You have constructor on subclasses with mostly

    Identical bodies.

    Create a superclass constructor; call this from the

    subclass methods

    class Manager extends Employee ...public Manager (String name, String id, int grade) {

    _name = name;_id = id;_grade = grade;

    }

    public Manager (String name, String id, int grade) {super(name, id);

    _grade = grade;}

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    26/30

    Push Down Method

    Behavior on a superclass is relevant only for

    some of its subclasses

    Move it to those subclasses

    Employee

    Salesman

    getQuota()

    EngineerSalesman

    Employee

    getQuota()

    Engineer

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    27/30

    Push Down Field

    Two subclasses have the same field

    Move the field to the superclass.

    Employee

    Salesman

    quota

    Engineer

    Employee

    quota

    EngineerSalesman

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    28/30

    Rename Method

    The name of a method does not reveal its purpose.

    Change the name of the method

    Customer

    getinvcdtlmt()

    Customer

    getInvoiceCreditLimit()

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    29/30

    Add Parameter

    A method needs more information from its caller.

    Add a parameter for an object that can pass on

    this information.

    Customer

    getContact()

    Customer

    getContact(:Date)

  • 8/10/2019 MELJUN CORTES JAVA Refactoring

    30/30

    Remove Parameter

    A parameter is no longer used by the method body.

    Remove it

    Customer

    getContact()

    Customer

    getContact(:Date)