Refactoring Strategies: Beyond the Basics

67 %
33 %
Information about Refactoring Strategies: Beyond the Basics
Technology

Published on February 25, 2014

Author: dtsato

Source: slideshare.net

Description

Refactoring is a fundamental practice for an agile developer. Each refactoring is a small and isolated activity, but the value comes when you perform a sequence of refactorings to effect larger changes. This workshop will guide participants through hands-on exercises designed to teach three essential skills to become good at refactoring:
1. Identifying what needs refactoring: what makes code smell, confusing, or complex?
2. Identifying the end goal: what are the design options and which one will yield the best results?
3. Tracing a strategy to get the code from 1 to 2.

REFACTORING STRATEGIES: BEYOND THE BASICS Danilo Sato (@dtsato) dsato@thoughtworks.com Mariana Bravo (@marivb) mbravo@thoughtworks.com

WHAT WE ARE DOING TODAY PacMan Other 2

AGENDA ▫︎ ▫︎Introduction Exercise 1: Step-by-Step ▫︎ Exercise 2: On Your Own ▫︎ Form Pairs and Setup 3

FORM PAIRS & SETUP ▫︎ ... if you have internet connection: $ git clone -b workshop_start https://github.com/dtsato/ pacman_workshop.git pacman_workshop ▫︎ ... if you have local network: $ git clone git@<IP>:/tmp/workshop pacman_workshop IP: X.X.X.X Password: **** pen ▫︎Oun allproject in IDE (Eclipse or IntelliJ) R tests ▫︎ 4

INTRODUCTION Concepts and Examples 5

REFACTORING "Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior" 6

CODE SMELL "Code smell is a symptom in the source code that possibly indicates a deeper problem with the system" 7

http://martinfowler.com/articles/workflowsOfRefactoring/ 8

EMERGENT DESIGN: JUNIT 9

REFACTORING STRATEGIES 10

EXERCISE 1 Step-by-Step 11

Maze 12

Tile Current Tile determined by Actor’s center 13

X Y [0, 6] TileCoordinate 14

WHAT IS THE PROBLEM? coordinates are exposed and used throughout the codebase as int x, y Tile Code smells: ● Primitive Obsession ● Data Clump 15

OBJECTIVES Finish the refactoring to use a new TileCoordinate class Focus on Maze class Encapsulate int x, y inside the TileCoordinate class 16

Refactoring Strategy STEP-BY-STEP Exercise_1_instructions.html

DISCUSSION What have you learned? 18

R LIMITED E D 19

CONTINUOUS DELIVERY 20

ALWAYS RELEASABLE 21

EXPAND-CONTRACT (A) Client A Subject oldWay() Client B 22

EXPAND-CONTRACT (A) Client A Client B Subject oldWay() newWay() 23

EXPAND-CONTRACT (A) Client A Client B Subject oldWay() newWay() 24

EXPAND-CONTRACT (A) Client A Client B Subject newWay() Inline Method 25

EXPAND-CONTRACT (B) Client A Subject oldWay() Client B 26

EXPAND-CONTRACT (B) Client A Client B Subject oldWay() newWay() 27

EXPAND-CONTRACT (B) Client A Client B Subject oldWay() newWay() 28

EXPAND-CONTRACT (B) Client A Client B Subject oldWay() newWay() 29

EXPAND-CONTRACT (B) Subject Client A Client B newWay() Safe Delete 30

EXERCISE 2 Your own Refactoring Strategy 31

Actor center, direction + collidesWith(Actor) + advance(millis) + getNextTile(current) Ghost Pacman 32

WHAT IS THE PROBLEM? The fields required to decide where to move are specific to each subclass of Actor and the implementations are very different Code Smells: ●Hard to Test ●Different constructors for Pacman/Ghost ●Different implementations 33

Actor MovementStrategy + getNextTile() Ghost Strategy Pacman Strategy 34

OBJECTIVES Extract a new interface MovementStrategy to decide what is the next Tile Implement this interface twice: PacmanStrategy and GhostStrategy Replace “Template Method” with composition in Actor class 35

REFACTORING STRATEGY ▫︎Keep coverage high and tests passing! ▫︎ Now it’s your turn! ▫︎Decide your strategy ▫︎Implement! ▫︎ Explore the code 36

DISCUSSION What have you learned? 37

REFERENCES ▫︎ Refactoring: Improving the design of existing code ▫︎ Martin Fowler, Kent Beck, et.al ▫︎ Refactoring to Patterns ▫︎ Joshua Kerievsky ▫︎ Extreme Programming Explained 2nd Edition ▫︎ Kent Beck, Cynthia Andres ▫︎ The Mikado Method ▫︎ Ola Ellnestam, Daniel Brolund ▫︎ Domain Driven Design: Tackling Complexity in the Heart of Software ▫︎ Eric Evans 38

39

THANK YOU For questions or suggestions: Danilo Sato (@dtsato) dsato@thoughtworks.com Mariana Bravo (@marivb) mbravo@thoughtworks.com

Add a comment

Related presentations

Related pages

GitHub - dtsato/pacman_workshop: PacMan game implemented ...

README.md Refactoring Strategies: Beyond the Basics. Code repository used for the "Refactoring Strategies: Beyong the Basics" workshop. Presented at: Agile ...
Read more

Session 3-4 – Refactoring Strategies: Beyond the Basics ...

Refactoring is a fundamental practice for an agile developer. Each refactoring is a small and isolated activity, but the value comes when you perform a ...
Read more

Sessions 3 & 4 – Refactoring Strategies: Beyond the Basics ...

Sessions 3 & 4 – Refactoring Strategies: Beyond the Basics & Workshop – Danilo Sato – Video June 6, 2016. Sessions 3-4 – Refactoring Strategies ...
Read more

Uploads from CoderConference - YouTube

Uploads from CoderConference ... Distributed Caching with JCache and Beyond Featuring Terry ... Refactoring Strategies: Beyond the Basics with Danilo ...
Read more

Devnexus 2014 - Dustin Whittle - The DevOps Pay Raise ...

... Danilo Sato - Refactoring Strategies - Beyond the Basics.mp3. 16/08/2014 | 01:27:57. Devnexus 2014 - Craig Walls - Whats new in Spring.mp3 ...
Read more

DevNexus 2014 | Presentations

Make It Great: Refactoring That’s Smart and Satisfying. Busy Developer’s Intro to NodeJS. New Linting Rules. ... Refactoring Strategies: Beyond the Basics.
Read more

Anna Mukhina (@annamukhina) | Twitter

Thank you @dtsato for Refactoring Strategies:Beyond the Basics workshop at this year's #chicoder2016! See it here http:// bit.ly/29uHees .
Read more

Martin Fowler

My name is Martin Fowler: ... Refactoring. Refactoring has ... JSON documents can often be made easier by encapsulating a combination of loading strategies ...
Read more