Slide 1

Slide 1 text

A comprehensive view of refactoring 1 12/06/2025

Slide 2

Slide 2 text

2 Ryanair + Codurance Software crafters Madrid

Slide 3

Slide 3 text

¡Hola! And Welcome [email protected] https://www.linkedin.com/in/javier -raez-labrado-12b9b215a/ [email protected] linkedin.com/in/marabesi Javier Raez Labrado 3 Matheus Marabesi

Slide 4

Slide 4 text

4 Agenda 1. Part I ○ Theory aspect 2. Part II ○ Refactoring time 3. Part III ○ Holistic view

Slide 5

Slide 5 text

Part I 5 Theory

Slide 6

Slide 6 text

What is refactoring? Any thoughts? 6

Slide 7

Slide 7 text

What is refactoring? Is the process of improving the code without changing its behaviour (Fowler) 7

Slide 8

Slide 8 text

Gilded rose (Typescript) Hi and welcome to team Gilded Rose. As you know, we are a small inn with a prime location in a prominent city ran by a friendly innkeeper named Allison. We also buy and sell only the finest goods. Unfortunately, our goods are constantly degrading in quality as they approach their sell by date. We have a system in place that updates our inventory for us. It was developed by a no-nonsense type named Leeroy, who has moved on to new adventures. Your task is to add the new feature to our system so that we can begin selling a new category of items: ● All items have a SellIn value which denotes the number of days we have to sell the item ● All items have a Quality value which denotes how valuable the item is ● At the end of each day our system lowers both values for every item Pretty simple, right? Well this is where it gets interesting: ● Once the sell by date has passed, Quality degrades twice as fast ● The Quality of an item is never negative ● “Aged Brie” actually increases in Quality the older it gets ● The Quality of an item is never more than 50 ● “Sulfuras”, being a legendary item, never has to be sold or decreases in Quality ● “Backstage passes”, like aged brie, increases in Quality as its SellIn value approaches; ○ Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but ○ Quality drops to 0 after the concert Our task We have recently signed a supplier of conjured items. This requires an update to our system: “Conjured” items degrade in Quality twice as fast as normal items Rules Feel free to make any changes to the UpdateQuality method and add any new code as long as everything still works correctly. However, do not alter the Item class or Items property as those belong to the goblin in the corner who will insta-rage and one-shot you as he doesn’t believe in shared code ownership (you can make the UpdateQuality method and Items property static if you like, we’ll cover for you). Just for clarification, an item can never have its Quality increase above 50, however “Sulfuras” is a legendary item and as such its Quality is 80 and it never alters. https://kata-log.rocks/gilded-rose-kata 8

Slide 9

Slide 9 text

Change is the constant of software ● Improve the structure of a program to improve maintenance (less complex code) ● To make room for new features 9

Slide 10

Slide 10 text

10 - Improve the structure of a program to improve maintenance - To make room for new features Why do we refactor? ● We want to reduce the cost and keep it as close as possible to the coding part ● Reaching production is costly A constant loop

Slide 11

Slide 11 text

Refactor vs rewrite? What are the differences between them? 11

Slide 12

Slide 12 text

Refactor vs rewrite? ● Refactor ○ We work on the existing code ○ Iterative - step by step ● Rewrite ○ Big bang - new code ○ Static ○ Rewrite does not mean no refactor, it delays the decision

Slide 13

Slide 13 text

Misconceptions about refactoring 13 ● Misconception ○ We should stop everything and focus on refactoring ○ It will take too long ● Alternatives ○ Campament scout rule ○ Ways of working ○ Refactor early, refactor often - Pragmatic Programmer

Slide 14

Slide 14 text

3 min BREAK 14

Slide 15

Slide 15 text

● Refactoring is the practice of improving existing code without changing its behaviour ● We do it because : ○ we want to make code maintainable - at individual level ○ reduce the cost of change - at organization level 15 Part I - Recap

Slide 16

Slide 16 text

Part II 16 Refactoring

Slide 17

Slide 17 text

Refactoring guidelines ● Get familiar first ○ get familiar with the setup ● Stay in green ○ tests should be testing behavior, so there is no reason why we should break any tests during refactoring. If you break any, undo, go back to green and start over ● Commit as often as possible ○ Make use of source control to safely and quickly revert to a known good point, provided we took small steps and committed frequently ● Refactor readability before design ○ Prefer small improvements in code readability in order to proceed with later design changes.

Slide 18

Slide 18 text

Most popular ones ● Rename ○ Change the name of classes, methods, variables, etc. ● Extract ○ Extract a class (or methods or variables …), creating a new abstraction. ● Inline ○ The inverse of extract. Inline a method (or variable), deconstructing an abstraction. ● Move ○ Move a class (or methods or variables…) to some other place in the codebase. ● Safe delete ○ Delete code and its usages in the code base.

Slide 19

Slide 19 text

Readability x Design 'To change something, first we need to understand something'. 19 Favor

Slide 20

Slide 20 text

Readability ● Format ○ Format consistently and don’t force the reader to waste time due to inconsistent formatting. Keep an eye on code conventions. ● Rename ○ Rename bad names, variables, arguments, methods, etc. Make abbreviations explicit. ● Remove ○ Delete unnecessary comments, delete dead code ● Extract ○ Constants from magic numbers and strings ● Reorder ○ Refine scope for improper variable scoping, and make sure variables are declared close to where they are used. ● Flip if/else ○ reduce cognitive load, instead of negate the condition uses it as positive

Slide 21

Slide 21 text

Design ● Extract private methods from deep conditionals. ● Extract smaller private methods from long methods, and encapsulate cryptic code in private methods. ● Return from methods as soon as possible. ● Encapsulate where we find missing encapsulation. ● Remove duplication

Slide 22

Slide 22 text

Parallel change (expand, migrate, contract) This is a useful technique to implement breaking changes safely while staying in green. It consists of 3 steps: ● Expand ○ Introduce new functionality by adding new code instead of changing existing one. ● Migrate ○ Deprecate old code and allow clients to migrate to new expanded code, or change client code to point to new code ● Contract ○ Once all client code is migrated to new code, remove old functionality by removing deprecated code and its tests.

Slide 23

Slide 23 text

3 min BREAK 23

Slide 24

Slide 24 text

● The refactoring process (each commit with a single change) - guidelines ● Improved readability ● Improved design ● Improved the system - now we have a new functionality - Parallel change (expand) 24 Part II - Recap

Slide 25

Slide 25 text

Part III 25 Holistic view

Slide 26

Slide 26 text

Types of refactoring ● Code ✅ ● Persistence ● Architecture

Slide 27

Slide 27 text

Persistence Rules: 1. Parallel change by default 2. Migration scripts (versioned in the repository) 27

Slide 28

Slide 28 text

Architecture/Re-architect? ● DDD - Boundaries ● Coupling 28

Slide 29

Slide 29 text

29 Code Persistence Arch Short cycles, as much as possible Longer cycles, Less frequent Limit Priority

Slide 30

Slide 30 text

● Practice with the kata we used (or others) ● Check the resources we have used: Refactoring and Refactoring catalog by Martin Fowler, Migrating databases by Edson Yanaga, cost of change by agilemodeling.com 30 Where to go from here?

Slide 31

Slide 31 text

¡Gracias! Let's keep in touch [email protected] https://www.linkedin.com/in/javier-raez-l abrado-12b9b215a/ [email protected] linkedin.com/in/marabesi Javier Raez 31 Matheus Marabesi