refactoring - agile tech conference 2016

74
Understanding Refactoring Bryan Beecham @BillyGarnet [email protected] Writing Healthy Code

Upload: billygarnet

Post on 13-Jan-2017

995 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Refactoring - Agile Tech Conference 2016

UnderstandingRefactoring

Bryan Beecham@BillyGarnet [email protected]

Writing Healthy Code

Page 2: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Is software designed to be simple and elegant more valuable than software that is complex and hard to maintain?

Page 3: Refactoring - Agile Tech Conference 2016

@BillyGarnet

~9600 line vb class named frm2

Page 4: Refactoring - Agile Tech Conference 2016

Continuous Improvement

Page 5: Refactoring - Agile Tech Conference 2016

Simplicity

Page 6: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Simplicity is the only thing that really scales.” –Joshua Kerievsky

Page 7: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Modern AgileMake People AwesomeMake Safety a Prerequisite Experiment and Learn Rapidly Deliver Value Continuously

Page 8: Refactoring - Agile Tech Conference 2016

@BillyGarnet

RefactoringImproving the design of existing code

Page 9: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Refactoring is the process of changing a software systemin such a way that it does not alter the external behaviour

of the code yet improves its internal structure.”

–Martin Fowler

Page 10: Refactoring - Agile Tech Conference 2016

@BillyGarnet

It must work.It must be understandable.

It must be updatable.

Needs of Software

What should software do?

Page 11: Refactoring - Agile Tech Conference 2016

Reasons to extract a function

✤ To reuse it (the only reason some consider).

✤ To name it’s operation.

✤ To make it testable.

✤ To get it out of your way so you can see the code that called it more clearly.

Page 12: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Remove the Clutter.Remove the Complexity.Remove the Cleverness.

Three Cs

Page 13: Refactoring - Agile Tech Conference 2016

@BillyGarnet

DuplicationDuplicationDuplication

Three Ds

Page 14: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Ring of FireJohnny Cash

Page 15: Refactoring - Agile Tech Conference 2016

@BillyGarnet

➤ Love is a burnin' thing,

➤ And it makes a fiery ring

➤ Bound by wild desire

➤ I fell into a ring of fire.

➤ I fell into a burnin' ring of fire

➤ I went down, down, down

➤ And the flames went higher,

➤ And it burns, burns, burns,

➤ The ring of fire, the ring of fire.

➤ I fell into a burnin' ring of fire

➤ I went down, down, down

➤ And the flames went higher,

➤ And it burns, burns, burns,

➤ The ring of fire, the ring of fire.

➤ The taste of love is sweet

➤ When hearts like ours meet.

➤ I fell for you like a child

➤ Oh, but the fire went wild.

➤ I fell into a burnin' ring of fire

➤ I went down, down, down

➤ And the flames went higher,

➤ And it burns, burns, burns,

➤ The ring of fire, the ring of fire.

➤ I fell into a burnin' ring of fire

➤ I went down, down, down,

➤ And the flames went higher,

➤ And it burns, burns, burns,

➤ The ring of fire, the ring of fire.

➤ And it burns, burns, burns,

➤ The ring of fire, the ring of fire.

➤ The ring of fire, the ring of fire

➤ The ring of fire

Page 16: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Love is a burnin' thing,

And it makes a fiery ring

Bound by wild desire

I fell into a ring of fire.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

The taste of love is sweet

When hearts like ours meet.

I fell for you like a child

Oh, but the fire went wild.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

I fell into a burnin' ring of fire

I went down, down, down,

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

And it burns, burns, burns,

The ring of fire, the ring of fire.

The ring of fire, the ring of fire

The ring of fire

Page 17: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Love is a burnin' thing,

And it makes a fiery ring

Bound by wild desire

I fell into a ring of fire.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

The taste of love is sweet

When hearts like ours meet.

I fell for you like a child

Oh, but the fire went wild.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

I fell into a burnin' ring of fire

I went down, down, down,

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

And it burns, burns, burns,

The ring of fire, the ring of fire.

The ring of fire, the ring of fire

The ring of fire

Page 18: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Love is a burnin' thing,

And it makes a fiery ring

Bound by wild desire

I fell into a ring of fire.

(CHORUS)

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

The taste of love is sweet

When hearts like ours meet.

I fell for you like a child

Oh, but the fire went wild.

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

I fell into a burnin' ring of fire

I went down, down, down,

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

And it burns, burns, burns,

The ring of fire, the ring of fire.

The ring of fire, the ring of fire

The ring of fire

Page 19: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Love is a burnin' thing,

And it makes a fiery ring

Bound by wild desire

I fell into a ring of fire.

(CHORUS)

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

CHORUS

The taste of love is sweet

When hearts like ours meet.

I fell for you like a child

Oh, but the fire went wild.

CHORUS

CHORUS

And it burns, burns, burns,

The ring of fire, the ring of fire.

The ring of fire, the ring of fire

The ring of fire

Page 20: Refactoring - Agile Tech Conference 2016

@BillyGarnet

(CHORUS)

I fell into a burnin' ring of fire

I went down, down, down

And the flames went higher,

And it burns, burns, burns,

The ring of fire, the ring of fire.

The taste of love is sweet

When hearts like ours meet.

I fell for you like a child

Oh, but the fire went wild.

And it burns, burns, burns,

The ring of fire, the ring of fire.

The ring of fire, the ring of fire

The ring of fire

Love is a burnin' thing,

And it makes a fiery ring

Bound by wild desire

I fell into a ring of fire.

CHORUS

CHORUS

Page 21: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Wow!That’s much easier to read

Page 22: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Why Refactor?

Page 23: Refactoring - Agile Tech Conference 2016

Development slows over time

Page 24: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Page 25: Refactoring - Agile Tech Conference 2016

Return on Investment (ROI)

Page 26: Refactoring - Agile Tech Conference 2016

@BillyGarnet

How many people have been on a project where the requirements didn’t change?

Page 27: Refactoring - Agile Tech Conference 2016

@BillyGarnet

QuotesFrom some people I like…

Page 28: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“It seems that perfection is attained not when there is nothing more to add,

but when there is nothing more to remove.” –Antoine de Saint Exupéry

Page 29: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Refactoring ought to be invisible. It should be part of every task, and not a task on its own.”

~Tim Ottinger

Page 30: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“It (refactoring) is the cheapest and safest way to read code. It is also the only way to safely change untestable code so that you can then start wrapping it in tests.”

~Arlo Belshee

Page 31: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“ Getting an app to work is the app-titude test for a programmer.There is a lot more to programming than just getting your app to work”

~James Grenning

Page 32: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Refactoring can be done in smaller steps than you think it can, never breaking the program.”

~Ron Jeffries

Page 33: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Relentless refactoring is the single best thing you can do to fix and avoid technical debt”

~Declan Whelan

Page 34: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“I refactor for one reason: to reduce volatility in the marginal cost of features.”

~J.B. Rainsberger

Page 35: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Refactoring is like breathing. You should do it all of the time without giving it much thought.”

~Jeff ‘Cheezy’ Morgan

Page 36: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“We refactor to embrace change.” ~Bryan Beecham

Page 37: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“You have two options, you can refactor, or you can build a big ball of mud.” ~Chet Hendrickson

Page 38: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“For each desired change, make the change easy (warning: this may be hard), then make the easy change.”

~Kent Beck

Page 39: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“If you’re a little better each day, next year you’ll be great. If you’re a little worse each day, next year you’ll be horrible.”

~Llewellyn Falco

Page 40: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Imagine how good a bowler you could be if you could adjust the direction of the ball once it's left your hands. Refactoring is what does that for us in writing code.”

~Woody Zuill

Page 41: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Business is well served by continuous refactoring, yet the practice of refactoring must co-exist harmoniously with business priorities.”

~Joshua Kerievsky

Page 42: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Refactoring, above all else, is a tool for managing risk.”~Amitai Schlair

Page 43: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“To me, refactoring is the most pleasant part of programming. I find it meditative and soothing, to change my code and my design

until it can be grasped almost instantly.” ~Mike ‘GeePaw’ Hill

Page 44: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“Rename class is the most powerful refactoring.”

~Michael Feathers

Page 45: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“The way to arrest entropy in software is to refactor it.” ~Joseph Yoder

Page 46: Refactoring - Agile Tech Conference 2016

@BillyGarnet

“So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.”

~Uncle Bob Martin

Page 47: Refactoring - Agile Tech Conference 2016

@BillyGarnet

HumanRefactoringGet up! Get on up!

Page 48: Refactoring - Agile Tech Conference 2016

Inside Refactoring

Page 49: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Inside Refactoring

Prepare

Improve

Clean

Page 50: Refactoring - Agile Tech Conference 2016

Prepare

✤ Prepare has also been referred to as nesting, making room, prefactoring or rough-in. We make small changes to the code so the improvement can take place.

Page 51: Refactoring - Agile Tech Conference 2016

Improve

✤ Improve is when we make an improvement to the code without changing its external behaviour.

Page 52: Refactoring - Agile Tech Conference 2016

Clean

✤ Clean is when we remove code we have made unnecessary and put the code back in a state to start something new.

✤ At this point we are done refactoring and can run tests, and, assuming they pass, integrate this refactoring.

Page 53: Refactoring - Agile Tech Conference 2016

Make a sandwich

✤ Get out all the things you need

✤ Assemble the sandwich

✤ Put everything away

Page 54: Refactoring - Agile Tech Conference 2016

Working Effectively with Legacy Code

✤ 1. Identify the code you want to extract and comment it out.

✤ 2. Think of a new name for a method and create it as an empty method.

✤ 3. Place a call to the new method in the old method.

✤ 4. Copy the code you want to extract into the new method.

✤ 5. Lean on the Compiler to find out what parameters you’ll have to pass and what values you’ll have to return.

✤ 6. Adjust the method declaration to accommodate the parameters and return value (if any).

✤ 7. Run your tests.

✤ 8. Delete your commented out code.

Page 55: Refactoring - Agile Tech Conference 2016

Prepare

✤ 1. Identify the code you want to extract and comment it out.

✤ 2. Think of a new name for a method and create it as an empty method.

✤ 3. Place a call to the new method in the old method.

Page 56: Refactoring - Agile Tech Conference 2016

Improve

✤ 4. Copy the code you want to extract into the new method.

✤ 5. Lean on the Compiler to find out what parameters you’ll have to pass and what values you’ll have to return.

✤ 6. Adjust the method declaration to accommodate the parameters and return value (if any).

✤ 7. Run your tests.

Page 57: Refactoring - Agile Tech Conference 2016

Clean

✤ 8. Delete your commented out code.

Page 58: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Inside RefactoringExample

Page 59: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Page 60: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Inside Refactoring

Prepare

Improve

Clean

Page 61: Refactoring - Agile Tech Conference 2016

@BillyGarnet

def convert(arabic): numeral = "" for i in range(0, arabic): numeral += "I" return numeral

Page 62: Refactoring - Agile Tech Conference 2016

@BillyGarnet

def convert(arabic): numeral = "" current_number = arabic

for i in range(0, current_number): if current_number >= 10: numeral += "X" current_number -= 10

for i in range(0, current_number): numeral += "I"

return numeral

Page 63: Refactoring - Agile Tech Conference 2016

@BillyGarnet

def convert(arabic): numeral = ""

for i in range(0, arabic): if arabic >= 10: numeral += "X" arabic -= 10

for i in range(0, arabic): if arabic >= 1: numeral += "I" arabic -= 1

return numeral

Page 64: Refactoring - Agile Tech Conference 2016

@BillyGarnet

def convert(arabic): numeral = ""

arabic_value = 10 roman_value = "X" for i in range(0, arabic): if arabic >= arabic_value: numeral += roman_value arabic -= arabic_value

arabic_value = 1 roman_value = "I" for i in range(0, arabic): if arabic >= arabic_value: numeral += roman_value arabic -= arabic_value

return numeral

Page 65: Refactoring - Agile Tech Conference 2016

@BillyGarnet

def convert(arabic): numeral = ""

arabic_digits = [10, 1] roman_digits = ["X", "I"] for i in range(0, len(arabic_digits)): while arabic >= arabic_digits[i]: numeral += roman_digits[i] arabic -= arabic_digits[i]

# arabic_digits = 1 # roman_digits = "I" # for i in range(0, arabic): # if arabic >= arabic_digits: # numeral += roman_digits # arabic -= arabic_digits

return numeral

Page 66: Refactoring - Agile Tech Conference 2016

@BillyGarnet

def convert(arabic): numeral = ""

arabic_digits = [10, 1] roman_digits = ["X", "I"] for i in range(0, len(arabic_digits)): while arabic >= arabic_digits[i]: numeral += roman_digits[i] arabic -= arabic_digits[i]

return numeral

Page 67: Refactoring - Agile Tech Conference 2016

@BillyGarnet

arabic_digits = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] roman_digits = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']

Page 68: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Inside Refactoring

Prepare

Improve

Clean

Page 69: Refactoring - Agile Tech Conference 2016

Bryan Beecham@BillyGarnet [email protected]

Questions and Answers

Page 70: Refactoring - Agile Tech Conference 2016

Questionable Code

Page 71: Refactoring - Agile Tech Conference 2016

Clarity

if (x){ return true } else if (!x) { return false }

if (x) return true return false

if (x){ return true

} else if (!x) { return false

}

return x

Page 72: Refactoring - Agile Tech Conference 2016

Clutter

;

Page 73: Refactoring - Agile Tech Conference 2016

Clutter

// // This is cool// I saw it on a blog and thought if we changed our code// it could work. Call me if you need more explanation////

Page 74: Refactoring - Agile Tech Conference 2016

@BillyGarnet

Books

✤ Refactoring - Martin Fowler

✤ Working Effectively with Legacy Code - Michael Feathers

✤ Refactoring to Patterns - Joshua Kerievsky

✤ Refactoring Workbook - Bill Wake

✤ Healthy Code - Getting Started with Refactoring - Bryan Beecham