REFACTORING in eXtreme Programming - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

REFACTORING in eXtreme Programming

Description:

John Brant and Don Roberts created 'Refactoring Browser' for the Smalltalk language. William Opdyke examined the theories of the preservation of the semantics by ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 36
Provided by: ITS8153
Category:

less

Transcript and Presenter's Notes

Title: REFACTORING in eXtreme Programming


1
REFACTORINGin eXtreme Programming
  • By Chaitanya Garikapati
  • Common refactoring methods taken from
    Refactoring Agile Development by Chris Garty

2
What is Refactoring?
  • Improving the design of existing code without
    changing its functionality
  • Each refactoring is small and reversible
  • Make the code
  • Easier to change
  • More loosely coupled
  • More cohesive
  • More understandable

3
Looking at the History..
  • Ward Cunningham and Kent Beck are the first
    people who realized the importance of Refactoring
    in 1980s
  • John Brant and Don Roberts created Refactoring
    Browser for the Smalltalk language
  • William Opdyke examined the theories of the
    preservation of the semantics by applying

4
When should we Refactor?
  • Early and often
  • Especially if you IDE support it
  • Performance problem may force your hand
  • As in todays case study

5
Bad Smells in code
  • Code that can make the design harder to change
  • Long methods
  • Big Classes
  • Big switch statements
  • Un-encapsulated fields
  • Too much checking for null objects
  • Duplicate code
  • Data Classes

6
Refactoring in the Agile Ecosystem
  • Automated unit tests support Refactoring
  • Refactoring supports doing the simplest things
    that could possibly work
  • Doing the Simplest thing allows you to deliver
    customer valued functionality early and often

7
Refactoring Agile Methods
  • Agile Development assumes that the best design
    will evolve and emerge through many iterations.
  • Refactoring is essential to an agile and
    evolutionary approach to design because design
    need to change as we learn through constant
    feedback what works and what doesnt.

8
XP Practice - Refactor Mercilessly
  • Refactor mercilessly to keep the design simple as
    you go and to avoid needless clutter and
    complexity. Keep your code clean and concise so
    it is easier to understand, modify, and extend.
    Make sure everything is expressed once and only
    once. In the end it takes less time to produce a
    system that is well groomed.
  • http//www.extremeprogramming.org/rules/refacto
    r.html

9
XP Practice Refactor Mercilessly (contd)
10
The Refactoring Process
  • Make a small change a single refactoring
  • Run all the tests to ensure everything still
    works
  • If everything works, move on to the next
    refactoring
  • If not, fix the problem, or undo the change, so
    you still have a working system

11
Refactoring in TDD
  • TDD-Test Driven Development
  • Refactoring is an essential element
  • Supports emergent design
  • The TDD cycle

12
  • Common Refactoring

13
Extract Class
  • Having the phone details as part of the Customer
    class is not a realistic OO model, and also
    breaks the Single Responsibility design
    principle. We can refactor this into two separate
    classes, each with the appropriate responsibility.

14
Extract Class
  • Public class Customer
  • Private string name
  • Private string workphonenumber
  • Private string workphoneareacode

15
Extract Class
  • Public class Customer
  • Private string name
  • Private Phone workphone
  • Public class Phone
  • Private string areacode
  • Private string number

16
Extract Method
  • Sometimes we have methods that do too much. The
    more code in a single method, the harder it is to
    understand and get right. It also means that
    logic embedded in that method cannot be reused
    elsewhere. The Extract Method refactoring is one
    of the most useful for reducing the amount of
    duplication in code.

17
Extract Method
  • public void PrintAccountDetails(Account account)
  • // print summary
  • Console.WriteLine("Account " account.Id)
  • Console.WriteLine("Balance "
    account.Balance)
  • // print history
  • foreach(Transaction tx in account.Transactions)

  • Console.WriteLine("Type " tx.Type
  • "Date " tx.Date " Amount "
  • tx.Amount)

18
Extract Method
  • public void PrintAccountDetails(Account account)
  • PrintSummary(account)
  • PrintHistory(account)
  • private void PrintSummary(Account account)
  • Console.WriteLine("Account " account.Id)
  • Console.WriteLine ("Balance "
    account.Balance)
  • private void PrintHistory(Account account)
  • foreach(Transaction tx in account.Transactions)
  • Console.WriteLine("Type " tx.Type
  • "Date " tx.Date " Amount "
  • tx.Amount)

19
Extract Subclass
  • When a class has features (attributes and
    methods) that would only be useful in specialised
    instances, we can create a specialisation of that
    class and give it those features. This makes the
    original class less specialised (i.e., more
    abstract), and good design is about binding to
    abstractions wherever possible.

20
Extract Subclass
  • public class Person
  • private string name
  • private string jobTitle

21
Extract Subclass
  • public class Person
  • protected string name
  • public class Employee Person
  • private string jobTitle

22
Form Template Method
  • When you find two methods in subclasses that
    perform the same steps, but do different things
    in each step, create methods for those steps with
    the same signature and move the original method
    into the base class.

23
Form Template Method
  • public abstract class Party
  • public class Person Party
  • private string firstName
  • private string lastName
  • private DateTime dob
  • public void PrintNameAndDetails()
  • Console.WriteLine ("Name " firstName " "
    lastName)
  • Console.WriteLine ("DOB " dob.ToShortDateStri
    ng())
  • public class Company Party
  • private string name
  • private string companyType
  • private DateTime incorporated

24
Form Template Method
  • public abstract class Party
  • public void PrintNameAndDetails()
  • PrintName()
  • PrintDetails()
  • public abstract void PrintName()
  • public abstract void PrintDetails()
  • public class Person Party
  • private string firstName
  • private string lastName
  • private DateTime dob
  • public override void PrintName()
  • Console.WriteLine("Name " firstName " "
    lastName)

25
Refactoring Tools
  • .NET
  • Resharper
  • C Refactory
  • C Refactory Tool
  • C/C
  • SlickEdit
  • Ref
  • Xrefactory
  • Java
  • JFactor
  • JBuilder
  • Eclipse

26
Refactoring Browser
  • The following are the enhancements of Refactoring
    Browser
  • Buffers
  • Drag Drop
  • Hierarchy
  • Lint
  • Menus
  • Multi-window

27
Steps when refactorying a software
  • Find the place where a refactoring has to be
    applied
  • If a unit test for the code under consideration
    exits,run the test to see if it is completed
    correctly
  • Otherwise write all necessary unit tests and get
    them running
  • Locate a refactoring that can be sensibly
    applied.

28
Steps when refactorying a software(contd)
  • Follow the step by step instructions to implement
    the refactoring
  • Run the tests between each step to ensure that
    the behavior has not changed
  • If necessary adapt the test code to changed
    interfaces
  • When the refactoring is successfully used to
    restructure the code, run the test again.
  • If no problem shows up you are done

29
Steps taken when applying the refactoring
  • Small enough to oversee the consequences they
    have
  • Reproduced to allow others to understand them and
    perhaps get rid of them
  • Generalized the way, that they are more a rule
    that can be applied to any structure that has a
    special appearance
  • Written down to allow sharing these steps and to
    keep a reference, examples with step by step
    instructions how to apply them.

30
Benefits of Refactoring
  • Refactoring helps in Understanding Code
  • Refactoring as a Tool to improve the (Non)
    Existing Design
  • Code Review
  • Introducing Refactoring to the Management

31
Risks of Refactoring
  • Waste of time (you fix the code that youll fix
    laterhoping these later fixes will be easier on
    account of that), which may lead to schedule
    slips
  • Risk of degarding quality introducing new bugs
    while refactoring.
  • Simplify code with one idea in mind may actually
    make some of the subsequent changes more
    difficult.

32
VS2005 Refactoring Highlights
  • Rename Variable, Parameter, Method, Class
  • Reorder/Remove Parameters
  • Surround with
  • Extract Method
  • Encapsulate Field
  • Extract Interface TDD helper (mocks)
  • General Methods Stub TDD helper

33
Future Development
  • Change Signature (add/remove/re-order parameters
    of a method/indexer)
  • Inline Method (opposite of Extract Method)
  • Encapsulate Field (creates properties with
    optional accessors)
  • Rename Namespace 

34
Conclusion
  • Refactoring is an integral part of XP
  • Benefits of Refactoring compliment XP
  • Potential problems reduced by XP
  • Refactoring improves the design of software

35
Resources
  • Refactoring.com
  • - http//www.refactoring.com/
  • The Father of Refactoring
  • - http//www.martinfowler.com
  • C Refactoring Developer
  • - http//geocities.com/bryantrsmith/refactoring
    /
  • Refactoring to Patterns
  • - http//www.industriallogic.com/xp/refactoring
    /
Write a Comment
User Comments (0)
About PowerShow.com