Refactoring in Extreme Programming - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Refactoring in Extreme Programming

Description:

One way is to make it so simple that there are obviously no eficiencies. ... You Aint Gonna Need It (YAGNI) Refactoring. 9/3/09. Refactoring in Extreme Programming ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 37
Provided by: burakc
Category:

less

Transcript and Presenter's Notes

Title: Refactoring in Extreme Programming


1
Refactoring in Extreme Programming
  • Burak Celebi
  • burak.celebi_at_gmail.com

2
Outline
  • Basics of Extreme Programming
  • Why Refactoring is important for XP?
  • Refactoring
  • Conclusion

3
Warming up...
  • There are two ways of constructing a software
    design.
  • One way is to make it so simple that there are
    obviously no eficiencies.
  • And the other way is to make it so complicated
    that there are no obvious deficiencies.
  • C.A.R. Hoare

4
What is Extreme Programming?
  • XP is a set of principles and practices that
    guide software development. It is an agile
    process in that it makes every effort to
    eliminate unnecessary work, instead focusing on
    tasks that deliver value to the customer.1
  • 1 Extreme Programming Cookbook

5
Four Principles in XP
  • Simplicity
  • Communication
  • Feedback
  • Courage

6
Simplicity
  • Perfection (in design) is achieved not when
    there is nothing more to add, but rather when
    there is nothing more to take away. Antoine de
    Saint-Exupéry
  • Do The Simplest Thing That Could Possibly Work
  • You Aint Gonna Need It (YAGNI)
  • Refactoring

7
Communication
  • Customers
  • No Long Design Phase. No heavy documents! Just do
    it..
  • CRC Cards
  • Pair Programming - pairprogramming.com
  • Team Room
  • http//www.scissor.com/resources/teamroom/Sdadsad

8
Feedback
  • Test-First approach.
  • Unit Testing
  • Integration Test
  • Acceptance Tests

9
Courge
  • Always design and code for today and not for
    tomorrow.
  • For managers, Pair Programming can be hard to
    accept.
  • Just the code remains from a project.

10
Refactoring
  • Any fool can write code that
  • a computer can understand.
  • Good programmers write code that
  • humans can understand.
  • Martin Fowler

11
Outline of Refactoring Section
  • What is Refactoring?
  • Why You Should Refactor?
  • When Sould You Refactor?
  • When Shouldnt You Refactor?
  • Refactoring Examples
  • Refactoring Tools

12
What is Refactoring?
  • Refacoring 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.
  • It is a disciplined way to clean up code that
    minimizes the chances of introducing bugs.

13
What Refactoring isnt
  • Refactoring is not an opportunity to add features
  • Refactoring is not performance optimisation which
    often leads to code that is harder to understand.
  • 1 Refactoring Improving The Design of Existing
    Code, Martin Fowler

14
Why Sould You Refactor?
  • Refactoring Improves the Design of Software
  • Refactoring Makes Software Easier to Undersand,
    therefore maintain, extend, verify.
  • Refactoring Helps You Find Bugs
  • Refactoring Helps You Program Faster

15
When Sould You Refactor?
  • Refactor When You Add Function
  • Refactor When You Need to Fix a Bug
  • Refactor As You Do a Code Review

16
When Shouldnt You Refactor?
  • If the current code does not work.
  • If you are close to a deadline.

17
What about Managers?
  • Who cares?

18
Refactoring and Performance
  • To make the software easier to understand, you
    often make changes that will cause the program to
    run more slowly.
  • However, it makes the software easier to tune
    during optimization
  • Result Rapid and clean development

19
Bed Smells in the Code
  • Duplicated Code
  • Long Methods
  • Large Classes
  • Long Parameter Lists
  • Speculative Generality
  • Too Many Comments

20
Selected Topics from Fowlers Catalog
  • Extract Method
  • Introduce Explaining Variable
  • Preserve Whole Object
  • Remove Double Negative
  • Replace Conditional with Polymorphism
  • Replace Error Code with Exception
  • Replace Iteration with Recursion
  • Replace Error Code with Exception
  • Replace Magic Number with Symbolic Constant
  • Replace Method with Method Object
  • Replace Error Code with Exception
  • Replace Parameter with Method
  • Introduce Parameter Object

21
Extract Method
  • You have a code fragment that can be grouped
    together.
  • Turn the fragment into a method whose name
    explains the purpose of the method.
  • void printOwing()
  • printBanner()
  • //print details
  • System.out.println ("name " _name)
    System.out.println ("amount "
    getOutstanding())
  • void printOwing()
  • printBanner()
  • printDetails(getOutstanding())
  • void printDetails (double outstanding)
  • System.out.println ("name " _name)
    System.out.println ("amount " outstanding)

22
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") gt -1)
    (browser.toUpperCase().indexOf("IE") gt -1)
    wasInitialized()
  • resize gt 0 )
  • // do something
  • final boolean isMacOs platform.toUpperCase().ind
    exOf("MAC") gt -1
  • final boolean isIEBrowser browser.toUpperCase().
    indexOf("IE") gt -1
  • final boolean wasResized resize gt 0
  • if (isMacOs isIEBrowser wasInitialized()
    wasResized)
  • // do something

23
Preserve Whole Object
  • You are getting several values from an object and
    passing these values as parameters in a method
    call.
  • Send the whole object instead.
  • int low daysTempRange().getLow()
  • int high daysTempRange().getHigh()
  • withinPlan plan.withinRange(low, high)
  • withinPlan lan.withinRange(daysTempRange())

24
Remove Double Negative
  • You have a double negative conditional.
  • Make it a single positive conditional
  • if ( !item.isNotFound() )
  • if ( item.isFound() )

25
Replace Conditional with Polymorphism
  • You have a conditional that chooses different
    behavior depending on the type of an object.
  • Move each leg of the conditional to an overriding
    method in a subclass. Make the original method
    abstract.
  • double getSpeed()
  • switch (_type)
  • case EUROPEAN
  • return getBaseSpeed()
  • case AFRICAN
  • return getBaseSpeed() - getLoadFactor()
    _numberOfCoconuts
  • case NORWEIGIAN_BLUE
  • return (_isNailed) ? 0 getBaseSpeed(_voltage
    )
  • throw new RuntimeException ("Should be
    unreachable")

26
Replace Error Code with Exception
  • A method returns a special code to indicate an
    error.
  • Throw an exception instead.
  • int withdraw(int amount)
  • if (amount gt _balance)
  • return -1
  • else
  • _balance - amount
  • return 0
  • void withdraw(int amount) throws
    BalanceException
  • if (amount gt _balance) throw new
    BalanceException()
  • _balance - amount

27
Replace Iteration with Recursion
You have a loop, and it is not obvious what each
iteration is doing Replace Iteration with
Recursion
  • unsigned greatest_common_divisor (unsigned a,
    unsigned b)
  • while (a ! b)
  • if (a gt b)
  • a - b
  • else if (b gt a)
  • b - a
  • unsigned greatest_common_divisor (unsigned a,
    unsigned b)
  • if (a gt b)
  • return greatest_common_divisor ( a-b, b )
  • else if (b gt a)
  • return greatest_common_divisor ( a, b-a )
  • else // (a b)
  • return a

28
Replace Error Code with Exception
  • A method returns a special code to indicate an
    error.
  • Throw an exception instead.
  • int withdraw(int amount)
  • if (amount gt _balance)
  • return -1
  • else
  • _balance - amount
  • return 0
  • void withdraw(int amount) throws
    BalanceException
  • if (amount gt _balance) throw new
    BalanceException()
  • _balance - amount

29
Replace Magic Number with Symbolic Constant
  • You have a literal number with a particular
    meaning.
  • Create a constant, name it after the meaning, and
  • replace the number with it.
  • double potentialEnergy(double mass, double
    height)
  • return mass height 9.81
  • double potentialEnergy(double mass, double
    height)
  • return mass GRAVITATIONAL_CONSTANT height
  • static final double GRAVITATIONAL_CONSTANT
    9.81

30
Replace Method withMethod Object
You have a long method that uses local variables
in such a way that you cannot apply Extract
Method Turn the method into its own object so
that all the local variables become fields on
that object. You can then decompose the method
into other methods on the same object.
  • class Order...
  • double aMethod(int a, Obj o) double
    primaryBasePrice
  • double secondaryBasePrice double
    tertiaryBasePrice
  • // long computations
  • private int otherMethod(int a)
  • ...

class Order.. price() return new
PriceCalculator(this).compute() Class
PriceCalculator... double primaryBasePrice double
secondaryBasePrice double tertiaryBasePrice
compute() ... private int otherMethod() ...
// it uses global variables
31
Replace Error Code with Exception
  • A method returns a special code to indicate an
    error.
  • Throw an exception instead.
  • int withdraw(int amount)
  • if (amount gt _balance)
  • return -1
  • else
  • _balance - amount
  • return 0
  • void withdraw(int amount) throws
    BalanceException
  • if (amount gt _balance) throw new
    BalanceException()
  • _balance - amount

32
Replace Parameter with Method
  • An object invokes a method, then passes the
    result as a parameter for a method. The receiver
    can also invoke this method.
  • Remove the parameter and let the receiver invoke
    the method.
  • int basePrice _quantity _itemPrice
  • discountLevel getDiscountLevel()
  • double finalPrice discountedPrice (basePrice,
    discountLevel)
  • int basePrice _quantity _itemPrice
  • double finalPrice discountedPrice (basePrice)

33
Introduce Parameter Object
  • You have a group of parameters that naturally go
    together.
  • Replace them with an object.
  • mountInvoicedIn(start Date, end Date)
  • amountReceivedIn(start Date, end Date)
  • amountOverdueIn(start Date, end Date)
  • amountInvoicedIn(DateRange)
  • amountReceivedIn(DateRange)
  • amountOverdueIn(DateRange)

34
Big Refactorings
  • gt These have more influence on software design.
  • Tease Apart Inheritance
  • Convert Procedural Design to Objects
  • Separate Domain from Presentation
  • Extract Hierarchy

35
Refactoring Tools
  • Smalltalk
  • Smalltalk Refactoring Browser
  • Java
  • IntelliJ Idea, Eclipse, RefactorIt, XRefactory,
    etc.
  • .Net
  • ReSharper, C Refactory, C Refactoring Tool
  • C/Cr
  • SlickEdit, Ref
  • Python
  • Bicycle Repair Man

36
Thank you..
  • I'm not a great programmer I'm just a good
    programmer with great habits.
  • Kent Beck, one of the three creators of XP
Write a Comment
User Comments (0)
About PowerShow.com