Slide 1

Slide 1 text

everyday superpowers Improving your code Without losing your mind Chris May—April 1, 2023

Slide 2

Slide 2 text

everyday superpowers Improving your code Without losing your mind Chris May—April 1, 2023

Slide 3

Slide 3 text

everyday superpowers Source: https://blog.alexewerlof.com/p/tech-debt-day

Slide 4

Slide 4 text

everyday superpowers —Alex Ewerlöf https://blog.alexewerlof.com/p/tech-debt-day “My struggles started from day one. I could not make sense of the code base and felt frustrated. At the time, I had 19 years of programming experience. …a simple task would take me multiple days longer than I thought…

Slide 5

Slide 5 text

everyday superpowers —Alex Ewerlöf https://blog.alexewerlof.com/p/tech-debt-day “My struggles started from day one. I could not make sense of the code base and felt frustrated. At the time, I had 19 years of programming experience. …a simple task would take me multiple days longer than I thought. I felt dumb and helpless.”

Slide 6

Slide 6 text

everyday superpowers —Alex Ewerlöf https://blog.alexewerlof.com/p/tech-debt-day “Leadership did not care about the code quality as long as the stories were delivered on time. Corners were cut, and tests were skipped.”

Slide 7

Slide 7 text

everyday superpowers What did they do? • The team had an honest conversation about the problem. • Developers argued to stop developing new features to clean everything. • Project management (rightly) pushed back. • They negotiated to reserve every other Friday to reduce tech debt.

Slide 8

Slide 8 text

everyday superpowers —Alex Ewerlöf https://blog.alexewerlof.com/p/tech-debt-day “Initially it was hard to defend spending 10% of the team bandwidth on [cleaning our code]…

Slide 9

Slide 9 text

everyday superpowers —Alex Ewerlöf https://blog.alexewerlof.com/p/tech-debt-day “Initially it was hard to defend spending 10% of the team bandwidth on [cleaning our code], but the payback was huge.”

Slide 10

Slide 10 text

everyday superpowers Refactoring results at 10% • the code quality improved • delivered features faster • nearly all "embarrassingly unnecessary incidents” eliminated • enabled them to make better judgment calls when we had to cut corners due to the time constraints • enjoyed their daily work more • inspired other teams to do the same

Slide 11

Slide 11 text

everyday superpowers Most of us are too focused on 
 getting features out the door. 
 Regularly taking even a small percentage of your time to improve your code can reap huge benefits.

Slide 12

Slide 12 text

everyday superpowers Agenda • Intro to refactoring • Two kinds of refactoring • Example • Why you should refactor • Resources

Slide 13

Slide 13 text

everyday superpowers Hi, I’m Chris May! Python technical coach I help python developers and teams reach their potential by temporarily joining teams and improving how they build software. https://everydaysuperpowers.dev

Slide 14

Slide 14 text

everyday superpowers What is refactoring?

Slide 15

Slide 15 text

everyday superpowers Refactoring: Improving code without changing its behavior Refactor

Slide 16

Slide 16 text

everyday superpowers We Refactor to: • Make code understandable • Support new directions Refactor

Slide 17

Slide 17 text

everyday superpowers Top-Down vs Bottom-Up

Slide 18

Slide 18 text

everyday superpowers Top-down • Were written with less- fl exible languages in mind • Are “destinations” you can take your code to

Slide 19

Slide 19 text

everyday superpowers Over 60 refactoring methods • Change Function Declaration • Change Reference to Value • Change Value to Reference • Collapse Hierarchy • Combine Functions into Class • Combine Functions into Transform • Consolidate Conditional Expression • Decompose Conditional • Encapsulate Collection • Encapsulate Record • Encapsulate Variable • Extract Class • Extract Function • Extract Superclass • Extract Variable • Hide Delegate • Inline Class • Inline Function • Inline Variable • Introduce Assertion • Introduce Parameter Object • Introduce Special Case • Move Field • Move Function • Move Statements into Function • Move Statements to Callers • Parameterize Function • Preserve Whole Object • Pull Up Constructor Body • Pull Up Field • Pull Up Method • Push Down Field • Push Down Method • Remove Dead Code • Remove Flag Argument • Remove Middle Man • Remove Setting Method • Remove Subclass • Rename Field • Rename Variable • Replace Command with Function • Replace Conditional 
 with Polymorphism • Replace Constructor with 
 Factory Function • Replace Control Flag with Break • Replace Derived Variable with Query • Replace Error Code with Exception • Replace Exception with Precheck • Replace Function with Command • Replace Inline Code with 
 Function Call • Replace Loop with Pipeline • Replace Magic Literal • Replace Nested Conditional with Guard Clauses • Replace Parameter with Query • Replace Primitive with Object • Replace Query with Parameter • Replace Subclass with Delegate • Replace Superclass with Delegate • Replace Temp with Query • Replace Type Code with Subclasses • Return Modi fi ed Value • Separate Query from Modi fi er • Slide Statements • Split Loop • Split Phase • Split Variable • Substitute Algorithm

Slide 20

Slide 20 text

everyday superpowers Top-down refactoring boiled down 1. Ensure tests cover what you want to change. 2. Duplicate the implementation. 3. Adjust the code as necessary to cover all cases, running tests every time you make a change. 4. Reroute original code to new code, one instance at a time, and run tests. 5. Remove old implementation. 6. Test again.

Slide 21

Slide 21 text

everyday superpowers Refactoring styles Top-down • Were written with less- fl exible languages in mind • Are “destinations” you can take your code to Bottom-up • Starts with your code • A series of small steps allow good 
 design patterns to emerge

Slide 22

Slide 22 text

everyday superpowers Refactoring styles Bottom-up • Starts with your code • A series of small steps allow good 
 design patterns to emerge

Slide 23

Slide 23 text

everyday superpowers The Flocking Rules

Slide 24

Slide 24 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 25

Slide 25 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 26

Slide 26 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 27

Slide 27 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 28

Slide 28 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 29

Slide 29 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 30

Slide 30 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated verse ??? 1 ` fi rst` 2 `second` 3 `third`

Slide 31

Slide 31 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated verse Nth 1 ` fi rst` 2 `second` 3 `third`

Slide 32

Slide 32 text

everyday superpowers from textwrap import dedent def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 33

Slide 33 text

everyday superpowers from textwrap import dedent def nth(): pass def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 34

Slide 34 text

everyday superpowers from textwrap import dedent def nth(): pass def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 35

Slide 35 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 36

Slide 36 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 37

Slide 37 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent("""\ On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 38

Slide 38 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 39

Slide 39 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 40

Slide 40 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 41

Slide 41 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 42

Slide 42 text

everyday superpowers from textwrap import dedent def nth(): return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 43

Slide 43 text

everyday superpowers from textwrap import dedent def nth(number): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 44

Slide 44 text

everyday superpowers Tests Failed!

Slide 45

Slide 45 text

everyday superpowers from textwrap import dedent def nth(number): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 46

Slide 46 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 47

Slide 47 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent("""\ On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 48

Slide 48 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 49

Slide 49 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 50

Slide 50 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 51

Slide 51 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth()} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 52

Slide 52 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 53

Slide 53 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 54

Slide 54 text

everyday superpowers from textwrap import dedent def nth(number = 1): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 55

Slide 55 text

everyday superpowers from textwrap import dedent def nth(number): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 56

Slide 56 text

everyday superpowers from textwrap import dedent def nth(number): if number == 2: return 'second' return 'first' def recite(verse: int) -> str: if verse == 2: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree. """) else: return dedent(f"""\ On the {nth(verse)} day of Christmas my true love gave to me: a Partridge in a Pear Tree. """) 1.Identify what to work on 1.Find the things that are most alike 2.Identify the smallest di ff erence between them 2.Make them identical. 1.Create a component to resolve the variations. 2.Implement code to supply one variation. 3.Replace one of the di ff erences with a call to the component. 4.Delete any unused code 5.Repeat until the di ff erences are eliminated

Slide 57

Slide 57 text

everyday superpowers Inspiration

Slide 58

Slide 58 text

everyday superpowers —Brett Slatkin (author, Principal Software Engineer at Google) @ PyConUS 2016 “Great programmers [write code that] makes 
 so much sense… it's so easy to understand.” “[Most stop] at the point where [their code] works functionally. 
 A great programmer continues on and refactors the 
 code base so that it's easy to understand and the code 
 is obvious.” “They do this because it provides a better foundation for the future... Over the long run, it saves you time.”

Slide 59

Slide 59 text

everyday superpowers @app.get("/prices") async def compute_price( type: str, age: Optional[int] = None, date: Optional[datetime.date] = None, ): result = await database.fetch_one( select(base_price_table.c.cost) .where(base_price_table.c.type == type), ) if age and age < 6: return {"cost": 0} else: if type != 'night': holidays = await database.fetch_all(select(holidays_table)) is_holiday = False reduction = 0 for row in holidays: if date: if date == row.holiday: is_holiday = True if not is_holiday and date and date.weekday() == 0: reduction = 35 # TODO apply reduction for others if age and age < 15:

Slide 60

Slide 60 text

everyday superpowers if date: if date == row.holiday: is_holiday = True if not is_holiday and date and date.weekday() == 0: reduction = 35 # TODO apply reduction for others if age and age < 15: return {"cost": math.ceil(result.cost * .7)} else: if not age: cost = result.cost * (1 - reduction / 100) return {"cost": math.ceil(cost)} else: if age > 64: cost = result.cost * .75 * (1 - reduction / 100) return {"cost": math.ceil(cost)} else: cost = result.cost * (1 - reduction / 100) return {"cost": math.ceil(cost)} else: if age and age >= 6: if age and age > 64: return {"cost": math.ceil(result.cost * .4)} else: return result else: return {"cost": 0}

Slide 61

Slide 61 text

everyday superpowers @app.get("/prices") async def compute_price( type: str, age: Optional[int] = None, date: Optional[datetime.date] = None, ): result = await database.fetch_one( select(base_price_table.c.cost) .where(base_price_table.c.type == type), ) if age and age < 6: return {"cost": 0} else: if type != 'night': holidays = await database.fetch_all(select(holidays_table)) is_holiday = False reduction = 0 for row in holidays: if date: if date == row.holiday: is_holiday = True if not is_holiday and date and date.weekday() == 0: reduction = 35 # TODO apply reduction for others if age and age < 15:

Slide 62

Slide 62 text

everyday superpowers async def compute_price( type: str, age: int = AGE_MISSING, date: datetime.date = DATE_MISSING ) -> int: if type == 'night': return await NightTicket(age).price return await NormalTicket(age, date).price class Ticket: ticket_kind = '' async def get_base_price(self): return await get_base_price_for(self.ticket_kind) class NightTicket(Ticket): ticket_kind = 'night' def __init__(self, age: int = AGE_MISSING, date: datetime = DATE_MISSING): self.age = age @property async def price(self): if self.age <= 6: return 0 if self.age > 64: return math.ceil(await self.get_base_price() * .4) return await self.get_base_price()

Slide 63

Slide 63 text

everyday superpowers if self.age <= 6: return 0 if self.age > 64: return math.ceil(await self.get_base_price() * .4) return await self.get_base_price() class NormalTicket(Ticket): ticket_kind = '1day' def __init__(self, age: int = AGE_MISSING, date: datetime.date = DATE_MISSING): self.age = age self.date = date @property async def price(self): if self.age <= 6: return 0 if self.age < 15: return math.ceil(await self.get_base_price() * .7) reduction = 1 - 35 / 100 if await self.is_holiday() else 1 if self.age > 64: return math.ceil(await self.get_base_price() * .75 * reduction) return math.ceil(await self.get_base_price() * reduction) async def is_holiday(self): holidays = {row.holiday for row in await database.fetch_all(select(holidays_table))} return self.date in holidays

Slide 64

Slide 64 text

everyday superpowers async def compute_price( type: str, age: int = AGE_MISSING, date: datetime.date = DATE_MISSING ) -> int: if type == 'night': return await NightTicket(age).price return await NormalTicket(age, date).price class Ticket: ticket_kind = '' async def get_base_price(self): return await get_base_price_for(self.ticket_kind) class NightTicket(Ticket): ticket_kind = 'night' def __init__(self, age: int = AGE_MISSING, date: datetime = DATE_MISSING): self.age = age @property async def price(self): if self.age <= 6: return 0 if self.age > 64: return math.ceil(await self.get_base_price() * .4) return await self.get_base_price()

Slide 65

Slide 65 text

everyday superpowers async def compute_price( kind: str, age: int = AGE_MISSING, date: datetime.date = DATE_MISSING, ): base_price = await get_base_price(kind) if kind == 'night': if age < 6: return 0 if 64 < age: return math.ceil(base_price * .4) return base_price else: if age < 6: return 0 if age < 15: return math.ceil(base_price * .7) price_with_date_discount = ( base_price * await _calculate_date_discount(date) ) if 64 < age: return math.ceil( price_with_date_discount * 0.75 ) else: return math.ceil(price_with_date_discount)

Slide 66

Slide 66 text

everyday superpowers —Brett Slatkin (author, Principal Software Engineer at Google) @ PyConUS 2016 “Great programmers [write code that] makes 
 so much sense… it's so easy to understand.” “[Most stop] at the point where [their code] works functionally. 
 A great programmer continues on and refactors the 
 code base so that it's easy to understand and the code 
 is obvious.” “They do this because it provides a better foundation for the future... Over the long run, it saves you time.”

Slide 67

Slide 67 text

everyday superpowers Source: https://pyvideo.org/pycon-us-2016/brett-slatkin-refactoring-python-why-and-how-to-restructure-your-code-pycon-2016.html

Slide 68

Slide 68 text

everyday superpowers —Hynek Schlawack (creator of attrs and other elegant things) “I don’t really think of refactoring as a separate thing. It’s just the result of having tests and thinking about the design of your code all the time.”

Slide 69

Slide 69 text

everyday superpowers Source: https://thebent.co/the-ruthless-edit/

Slide 70

Slide 70 text

everyday superpowers Refactoring results at 10% • the code quality improved • delivered features faster • nearly all "embarrassingly unnecessary incidents” eliminated • enabled them to make better judgment calls when we had to cut corners due to the time constraints • enjoyed their daily work more • inspired other teams to do the same

Slide 71

Slide 71 text

everyday superpowers Refactoring results at 10% • the code quality improved • delivered features faster • nearly all "embarrassingly unnecessary incidents” eliminated • enabled them to make better judgment calls when we had to cut corners due to the time constraints • enjoyed their daily work more • inspired other teams to do the same

Slide 72

Slide 72 text

everyday superpowers How to learn to refactor • Ask a Python meetup to do a refactoring night. • Talk to your teammates about it. • Read through a code smell or refactoring method for 
 10 minutes each week. • Practice often.

Slide 73

Slide 73 text

everyday superpowers Resources

Slide 74

Slide 74 text

everyday superpowers https://everydaysuperpowers.dev/toolkit/

Slide 75

Slide 75 text

everyday superpowers Connect https://everydaysuperpowers.dev/ @_chrismay fosstodon.org/@_chrismay

Slide 76

Slide 76 text

everyday superpowers