Refactoring to Patterns

    Refactoring to Patterns. Jim Fawcett CSE776 – Design Patterns Summer 2005. Reference. Refactoring to Patterns, Joshua Kerievsky, Addison-Wesley, 2005. Agenda. This presentation provides a summary of the content from this well received book. Background. Evolutionary Programming

    Refactoring – . refactoring. noun: “a change made to the internal structure of software to make it easier to

    Refactoring – . principles and examples. the goals of a module. functionality the reason for the module’s existance

    Refactoring – . mathematics: factor. fac · tor one of two or more quantities that divides a given quantity without a

    Refactoring – . http://c2.com/cgi/wiki?extremenormalform your classes are small and your methods are small; you’ve said

    Refactoring – . refactoring overview. what is refactoring? what are four good reasons to refactor? when should you

    Refactoring – . small changes to code to improve it. refactoring defined. a change made to the internal structure of

    Refactoring – . an automated tool for the tiger language leslie a hensley [email protected]. what i’m going to

    Refactoring – . refactoring. ”a change made to the internal structure of software to make it easier to understand and

    Refactoring – . small changes to code to improve it. refactoring defined. a change made to the internal structure of

    Refactoring – . what is refactoring? martin fowler’s definition. refactoring is the process of changing a software

    Towards an Investigation of Opportunities for Refactoring to Design Patterns – . norihiro yoshida , katsuro inoue osaka

    Refactoring to Patterns – . cse301 university of sunderland harry r erwin, phd. introduction. want to use a pattern in an

  1. Refactoring to Patterns Jim Fawcett CSE776 – Design Patterns Summer 2005

  2. Reference • Refactoring to Patterns, Joshua Kerievsky, Addison-Wesley, 2005

  3. Agenda • This presentation provides a summary of the content from this well received book.

  4. Background • Evolutionary Programming • A development process that focuses on incremental development. • Usually set up as a sequence of development cycles, each culminating in a software release. • Each item from the sequence provides some planning, design, implementation, and test. • Sometimes the elements are inverted, as is the case for extreme programming:

  5. Extreme Programming • Process Model Phases: • A sequence of releases as per Evol. Prog. • Steps to achieve a release: • Specify the functionality for next release • Design Test(s) • Write code to make tests pass • Refactor code to improve the design • Constantly run regression tests on the entire build • Goal is to stay in state where all tests pass • Release when the implemented functionality is incorporated in the build.

  6. Refactoring • Refactoring is a major part of the Evolutionary style of programming, especially for Extreme Programming. • But how do you successfully refactor? • What are the goals? • When should you refactor? • How do you do it? • How do you know when to stop?

  7. A Selection of Chapters • Chap 3: Patterns • Chap 4: Code Smells • Chap 5: Catalog of Refactorings to Patterns • Chap 6: Creation • Chap 7: Simplification • Chap 8: Generalization • Chap 9: Protection • Chap 10: Accumulation • Chap 11: Utilities

  8. Patterns • What is a pattern? • “Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution” – Christopher Alexander, Architect • There are many ways to implement a pattern • “Every pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the samy way twice.” – Christopher Alexander

  9. Refactoring to, towards, and away from Patterns • “Good designers refactor in many directions, always with the goal of reaching a better design. While many of the refactorings I apply don’t involve patters (i.e., they’re small, simple transformations, like Extract Method … ), when my refactorings do involve patterns, I refactor to, towards, and even away from patterns.” – Joshua Kerievsky

  10. Code Smells • “It’s … necessaryto learn common design problems so you can recognize them in your code.” • Robert Martin, Martin Fowler, and Kent Beck have all written about specific “Code Smells”

  11. Catalog of Code Smells • Duplicated Code • Long Method • Conditional Complexity • Primitive Obsession • Indecent Exposure • Solution Sprawl • Alternative Classes with Different Interfaces • Lazy Class • Large Class • Switch Statements • Combinational Explosion • Oddball Solution

  12. Catalog of Patterns • Replace Ctors with Creation methods, chain Ctors • Encapsulate Classes with Factory • Introduce Polymorphic Creation with Factory Method • Replace Conditional Logic with Strategy • Form Template Method • Compose Method • Replace Implicit Tree with Composite • Encapsulate Composite with Builder • Move Accumulation to Collecting Parameter • Extract Composite, Replace one/many with Composite. • Replace Conditional Dispatcher with Command • Extract Adapter, Unify Interfaces with Adapter • Replace Type Code with Class

  13. Catalog of Patterns • Replace State-Altering Conditionals with State • Introduce Null Object • Inline Singleton, Limit Instantiation with Singleton • Replace Hard-Coded Notifications with Observer • Move Embellishment to Decorator, Unify Interfaces, Extract Parameter • Move Creation Knowledge to Factory • Move Accumulation to Visitor • Replace Implicit Language with Interpreter

  14. A Typical Catalog Item • States Pattern Name and Intent • Gives an application example • Discusses motivation • Benefits and Liabilities • Mechanics • Specific things to do • Presents detailed example

  15. Some non-GoF Patterns • Compose Method • Extract blocks of code as methods with intent revealing names • Chain Constructors • Remove duplicate code in constructors by calling, in a constructor, other constructor(s). • Extract Parameter • Assign, to a field, a caller provided parameter instead of a locally instantiated object. • Move Accumulation to Collecting Parameter • Replace a complex function that accumulates results with a set of Composed Methods which are passed a Collecting Parameter.

  16. Summary • Interesting ideas, presented clearly • Book is on restricted hold in Sci-Tech library.