natural course of refactoring. a refactoring...

32
www.bnsit.pl Natural Course of Refactoring. A refactoring workflow. Mariusz Sieraczkiewicz @ms_bnsit_pl http://msieraczkiewicz.blogspot.com 1

Upload: others

Post on 20-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Natural Course of Refactoring. A refactoring workflow.

Mariusz Sieraczkiewicz

@ms_bnsit_pl

http://msieraczkiewicz.blogspot.com

1

Page 2: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Why refactoring is like sex? Natural Course of Refactoring

Page 3: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Once you get started, you’ll only stop because you’re exhausted.

One mistake and the consequences may be really huge.

Why refactoring is like sex?

Natural Course of Refactoring 2014 3

(adapted from http://www.thealmightyguru.com/Humor/Docs/ProgrammingIsLikeSex.html)

Page 4: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

People more talk about it than actually do.

You can do it for money or for fun.

It’s not really an appropriate topic for dinner conversation.

Why refactoring is like sex?

Natural Course of Refactoring 2014 4

(adapted from http://www.thealmightyguru.com/Humor/Docs/ProgrammingIsLikeSex.html)

Page 5: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Beginners do a lot of noise about it.

Some people are just naturally good at it,

.. but some people will never realize how bad they are, and you’re wasting your time trying to tell them.

Why refactoring is like sex?

Natural Course of Refactoring 2014 5

(adapted from http://www.thealmightyguru.com/Humor/Docs/ProgrammingIsLikeSex.html)

Page 6: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Code readability Natural Course of Refactoring

Page 7: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

.so!changesIntroDuCinG!maYBe,softwaRe,TosO,calLEdprOgRessIve,and.however.cHanGes!Modifies.usuallY,sTRucTureOFmaYbe,hoWEVeRtHecoDE,And!wHaTmaYbecumulatEdhowevEr.anD,mAkes.AnD,,and,the.LeSs!rEAdAblE,aNd.cOdeMAybe.ANd,!and,!Thenumber!of,sO,HoWEvErsodepeNdeNCiES,And.MAybeintErACtIOns!betWeenHoWevEr!differEntsyStem.moDules!inCreasESoThat,iTsO!morE.is.diffiCuLt,To,AnDuNdErstandmodIFy

Why is it difficult to understand software after some time of its evolution?

Natural Course of Refactoring 2014 7

Page 8: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Introducing changes to software (so called progressive changes) usually modifies structure of the code, what cumulated makes the code less readable. The number of dependencies and interactions between different system modules increase, so that it is more difficult to understand and modify.

Why is it difficult to understand software after some time of its evolution?

Natural Course of Refactoring 2014 8

Page 9: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Flavours of refactoring Natural Course of Refactoring

Natural Course of Refactoring 2014 9

Page 10: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Two flavours of refactoring

Natural Course of Refactoring 2014 10

Page 11: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Everyday refactoring

Within a reach of every programmer

Can be done in minutes

Mostly safe, IDE-base automatic refactorings

For local health of the code

Part of programming practice

No excuse for not doing it

Everyday refactoring

Natural Course of Refactoring 2014 11

Page 12: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Strategic refactoring

A team longer term effort

Requires aligning with iteration planning

Generates items in backlog

Risky activity that requires intensive testing (including good tests suite)

Difficult and time-consuming

Check carefully if refactoring gives you enough value (Feather’s Quadrant)

Strategic refactoring

Natural Course of Refactoring 2014 12

Page 13: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

When should I do strategic refactoring? Natural Course of Refactoring

Page 14: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

High complexity/ Seldom changes

Don’t touch it.

High complexity/ Frequent changes

Apply strategic refactoring

Low complexity/ Seldom changes

Utils, good for experiments

Low complexity/Frequent

changes

Heaven

Feather’s Quadrant

Natural Course of Refactoring 2014 14

Confront with: • business vision

Frequency of changes

Complexity

Page 15: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Natural Course of Refactoring The process

Page 16: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

Natural Course of Refactoring 2014 16

Page 17: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Step 0. Understand the code

Natural Course of Refactoring 2014 17

Page 18: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Sometimes it is the most difficult point

Try to…

• find an author and ask for help

• find somebody who worked with the code and ask for help

• find somebody who knows the system (or module) and ask for help

• Do it yourself if none of above are possible

Understand

Natural Course of Refactoring 2014 18

Page 19: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Mental tools

Simple code cleaning

Clean up the names

Add temporary comments to the code

Introduce lazy variables initialization

Make optical cleanup (make more space)

Scratch refactoring

Do some exploratory refactoring to be thrown away

The only goal is to gain more understanding of the code

Understand

Natural Course of Refactoring 2014 19

Page 20: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

They should be temporary. Delete them after refactoring.

// SMELL it doesn’t look good,

copy-paste antipattern

// REFACTOR introduce factory

// NOTE send a message

Introduce refactoring comments

Natural Course of Refactoring 2014 20

Page 21: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Step 1. Express algorithm

Natural Course of Refactoring 2014 21

Page 22: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Aim

Code that speaks to you

Mental tools

• Compose method

• Introduce Method Object Refactoring

• Extract method

• Naming conditions

Express algorithm

Natural Course of Refactoring 2014 22

Page 23: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Step 2. Extract responsibilities

Natural Course of Refactoring 2014 23

Page 24: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Mental tools

Single responsibility principle

Move method refactoring

Extract class refactoring

Introduce Domain Object

Introduce Value Object

Extract responsibilities

Natural Course of Refactoring 2014 24

Page 25: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Step 3. Introduce flexibility

Natural Course of Refactoring 2014 25

Page 26: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Mental tools

S.O.L.I.D.

Design patterns

Refactoring to patterns

Apply design patterns

Natural Course of Refactoring 2014 26

Page 27: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Step 4. Evolve architecture

Natural Course of Refactoring 2014 27

Page 28: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Mental tools

Introducing/removing layers

Introducing or replacing ORM/NoSQL/?

Important change in building blocks

Changing or introducing new framework

Introducing events

Introducing state machine

Moving towords DDD, Microservices, CQRS

Introducing Bounded-Context (DDD)

Applying Anticorruption Layer

Evolve architecture

Natural Course of Refactoring 2014 28

Page 29: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

Natural Course of Refactoring 2014 29

Page 30: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl Natural Course of Refactoring 2014 30

Everyday refactoring

Strategic refactoring

Page 31: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Why NCR?

Easy to teach

Easy to understand and remeber

Separates everyday and strategic refactoring

Indicates the simplest (safe) possible step in the moment

Gives hints what kind of refactorings can be applied in the moment

NCR IN A TEAM

Natural Course of Refactoring 2014 31

Page 32: Natural Course of Refactoring. A refactoring workflow.agileprague.com/pool/vzor/upload/Natural_Course_of_refactoring.pdf · People more talk about it than actually do. You can do

www.bnsit.pl

Natural Course of Refactoring. A refactoring workflow.

Mariusz Sieraczkiewicz

@ms_bnsit_pl

http://msieraczkiewicz.blogspot.com

32