refactoring to patterns - indicthreads...• refactoring and agile techniques • refactoring and...

61
Refactoring to Patterns A practical look into the Agile approach on Evolutionary Design Paulo Caroli ThoughtWorks

Upload: others

Post on 11-Jun-2020

61 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring to Patterns

A practical look into the Agile approach on Evolutionary Design

Paulo Caroli

ThoughtWorks

Page 2: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

About Paulo Caroli

• A technologist at ThoughtWorks US

• Master's Degree in Software Engineering

• Sun Certified J2EE Architect

• More than 13 years in SW Development.

• Agile Techniques, such as XP, TDD, Continuous Integration

• Object Oriented development practices

• More at www.caroli.org

Page 3: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Agenda

• Refactoring and Agile Techniques

• Refactoring and Functional Requirements

• Refactoring and TDD

• Simple Design and Code Smells

• Refactoring to Patterns

• Case Study

• Conclusion

• Q & A

Page 4: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring and Agile Techniques

Page 5: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Development Styles

Page 6: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Fix / Integrate $

Test

Code

DesignSpecifications

Use Cases / Functional Specs

Requirements Gathering

Project Plan/Estimation

$Release 1

Inception

$Release 2

$Release 3

Release 4 $

Development Styles

Page 7: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Agile History

Page 8: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

FDD | Feature Driven Development (Jeff DeLuca)

DSDM | Dynamic System Development Method (Dane Faulkner)

Adaptive Software Development (Jim Highsmith)

Crystal (Alistair Cockburn)

SCRUM (Ken Schwaber)

XP | Extreme Programming (Kent Beck)

Lean Software Development (Mary Poppendieck)

20002001“We are uncovering better ways of developing software by doing it and helping others do it. Through this work we have come to value:

– Individuals and interactionsover processes and tools.

– Working software over comprehensive documentation.

– Customer collaboration over contract negotiation.

– Responding to change over following a plan.

© 2001 Agile Alliance. http://www.agilemanifesto.org

Agile

manifesto

Page 9: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Agile Design Principles

Page 10: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Agile Design principles

• Do the simplest thing that could possibly work– A simple design takes less time to finish than a complex

one

• Never add functionality before it is scheduled– Concentrate on what is scheduled for today only

• Agile designs are emergent, they’re not defined up front

Page 11: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Agile Design principles

• The whole team is responsible of the design of the system

• Design is so important you should do it every day

• The Unit Tests form much of your detailed design documentation

• Document complicated things– If it is complicated, then document it thoroughly.

– Better yet, invest the time to refactor it so it is simple

Page 12: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Agile Umbrella

Page 13: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Agile Umbrella

Just the right amount of documentation

Collective Code Ownership Pair Programming

Test Driven Development

Refactoring Automated testing

Continuous Integration

Simple and Evolutionary Design

Iterative Development

Frequent Short Releases

Collocation Stand ups

Sustainable Pace Spikes

Page 14: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring (to Patterns)

Page 15: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring (to Patterns)

Page 16: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring

Page 17: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Refactoring is a development practice for restructuring an existing code, altering its internal structure without changing its external behavior.

Refactoring

Page 18: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Refactoring is a process of improvement to an existing software artifact.

Refactoring

Page 19: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• New requirement, bug fix, behavior change and architecture change are NOT Refactoring.

Refactoring

Page 20: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring and Functional Requirements

Page 21: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring and Functional Requirements

Page 22: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Business• Project manager• QA

• Developer• Performance Engineer• Build Engineer

Refactoring and Functional Requirements

Page 23: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring and Test Driven Development

Page 24: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Test Driven Development

“Test-Driven Development (TDD) is an evolutionary approach to development which instructs you to have test-first development intent. Basically, you start by writing a test and then you code to elegantly fulfill the test requirements.”

Page 25: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

TDD Cycle

Test Fail Test Pass

2. write code

1. write test

3. refactor

Page 26: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Refactor on every green bar• Look for Code Smells to Refactor• No Automated Developer Test -> Hard to Refactor

Refactoring and TDD

Page 27: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring and TDD

Page 28: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring and Simple Design

Page 29: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Think about tomorrow, but design, test and code for Today’s need

• Produce simple artifacts that could be made flexible instead of Complex flexible artifacts

Refactoring and Simple Design

Page 30: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Do the simplest thing that could possibly work

• Evolve into elaborate solutions

Refactoring and Simple Design

Page 31: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

The Simplest Thing That Works

Page 32: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

The simplest thing that works

• Don’t try to solve big problems in one go

– Break them down into little problems and

solve them one at a time

• Do the simplest thing that meets the current requirement

Page 33: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

The simplest thing that works

• Use interfaces / abstractions to insulate areas of risk or unknown

• Use TDD + refactoring + interfaces to keep the system extensible

Page 34: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Evolutionary Design

Page 35: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

•The complexity of your design should support the current requirements at hand (being built in this Iteration) and no more.

Evolutionary Design

Page 36: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Do not design for future flexibility, let new requirements "pull" more complex designs - Refactor later.

Evolutionary Design

Page 37: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Evolutionary Design should not be practiced without the ability to refactorand evolve the design.

Evolutionary Design

Page 38: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Code Smell

Page 39: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Code smell is a hint that something can be improved somewhere in your code.

Code Smell

Page 40: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Code Smell Samples

Page 41: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Duplicated Code• Methods too big • Nested “if” statements • Classes with too many instance variables • Classes with too much code

Code Smell Samples

Page 42: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Strikingly similar subclasses • Too many private (or protected) methods • Similar looking code sections • Dependency cycles • Passing Nulls To Constructors • Classes with too little code

Code Smell Samples

Page 43: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring (to Patterns)

Page 44: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Simple Design -> Code Smell -> Refactor

• Refactoring (to Patterns) is the ability to transform a “Code Smell” into a positive design pattern

Refactoring (to Patterns)

Page 45: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Design Patterns

Page 46: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Proven solution to a problem in context

• Classic solutions to common OO design problems

• Become a common language among developers

Design Patterns

Page 47: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Applying Design Patterns

Page 48: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Applying Design Patterns - Upfront Design

Page 49: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Pre-emptive Design

• Sounds Great! But do I really need it?

• Wow, 3 weeks designing, beautiful diagrams!

• I generated lots of Pattern code (functional and test)

Applying Design Patterns - Upfront Design

Page 50: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Applying Design Patterns - Emergent Design

Page 51: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Evolutionary Design

• Positive Patterns were “pulled” naturally

• Several components were just fine without Design Patterns

• Unit Test (TDD) validates the Refactoring to Patterns

Applying Design Patterns - Emergent Design

Page 52: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Case Study

Page 53: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Recommended Readings

Page 54: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

• Refactoring: Improving the Design of Existing Code, Martin Fowler , Addison-Wesley, 2000.

• Refactoring to Patterns, Joshua Kerievsky, Addison Wesley, 2004.

• Design Patterns: Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm , Ralph Johnson, John Vlissides, Addison-Wesley 1995

• http://www.thoughtworks.com/ • http://agilemanifesto.org/

Recommended Readings

Page 55: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

RefactoringTips and Suggestions

Page 56: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring Tips and suggestions

• Design every day

• Isolate the unknown with abstractions / interfaces

• Mock out dependent systems for testing

Page 57: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Refactoring Tips and suggestions

• Refactor code so it communicates intent

• Test everything – don’t refactor without tests

• Focus on simplifying complexity rather than documenting it

Page 58: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Session Learning’s

Page 59: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Session Learning's

• Understand Refactoring and TDD as Agile techniques

• Understand Agile Simplicity and Evolutionary Design Techniques

Page 60: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Session Learning's

• Understand how simple design evolves into more elaborate design by means of TDD and Refactoring

• Understand what code smells are and how they can be Refactored into Design Patterns (when appropriate)

Page 61: Refactoring to Patterns - IndicThreads...• Refactoring and Agile Techniques • Refactoring and Functional Requirements • Refactoring and TDD • Simple Design and Code Smells

Questions?

THANK YOU!