# Refactoring journey

## Objectives

* Practice refactoring tips to improve your day-to-day efficiency
* Use your IDE features

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTgp-YlfxOGwycoWe2%2F-MRThN9ncVWGcSYbV2va%2Fimage.png?alt=media\&token=5c6f3845-616c-44cd-b739-3bf4f97315c2)

### Before we start

* Clone the repository [here](https://github.com/ythirion/refactoring-journey)
* Make sure you can run the tests

## Connection

### Refactoring, you said refactoring ?

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTgp-YlfxOGwycoWe2%2F-MRTkJlw8bDTtWCbTWWW%2Fimage.png?alt=media\&token=fca9934a-3b87-4efa-9823-de81b2e5bff6)

***Names of Refactorings :***

* Write down names of refactorings you already know
* Write up names on a whiteboard or flipchart

Remind them some they use probably : Rename Variable and Extract Interface.

## Concepts

### Several ways to write code

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTgp-YlfxOGwycoWe2%2F-MRTkiNm7iP29axl6M4O%2Fimage.png?alt=media\&token=c2d8824a-1e4b-47d8-9f10-dccb042e36ba)

### What is refactoring ?

> *Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.*
>
> *Refactoring (verb): to restructure software by applying a series of refactorings without changing its observable behavior*. - [Martin Fowler](https://martinfowler.com/bliki/DefinitionOfRefactoring.html)

{% hint style="success" %}
Refactoring is a systematic process of **improving code** without creating new functionality that **can transform a mess into clean code and simple design**.
{% endhint %}

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTgp-YlfxOGwycoWe2%2F-MRTl2C4HzbPK82ctWvP%2Fimage.png?alt=media\&token=1fa08fbc-7685-43a3-9507-df01225268b6)

> ***The main purpose of refactoring is to fight technical debt***

### When to refactor ?

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTgp-YlfxOGwycoWe2%2F-MRTn4ShUnxB4Ys9eTO9%2Fimage.png?alt=media\&token=dfbe3867-8914-41d7-a158-3f1d70b5deed)

#### When adding a feature

* If you must deal with someone else’s dirty code, try to refactor it first
  * Clean code is much easier to grasp
* Refactoring makes it easier to add new features
  * Easier to make changes in clean code

#### Rule of Three (duplication)

* You can copy and paste the code once
* BUT when the same code is replicated **3 times**, it should be extracted into a new procedure

#### When fixing a bug

* Clean your code and the errors will practically discover themselves.

#### During a code review

* Code review may be the last chance to tidy up the code before it becomes available to the public
* Best to perform reviews in a pair with an author : *you could fix simple problems quickly*

#### When paying your debt

* Dev teams can create technical debt consciously at one time and book some time later to tackle it
* See [technical debt quadrants](https://martinfowler.com/bliki/TechnicalDebtQuadrant.html)

### How to ?

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTgp-YlfxOGwycoWe2%2F-MRToMGe-vvYotkkUcQ0%2Fimage.png?alt=media\&token=1bf49778-6986-4195-8e90-d37f0eea5c2d)

There are two cases when tests can break down after refactoring :&#x20;

* You made an ***error during refactoring***
  * Go ahead and fix the error
* Your ***tests were too low-level*** (for example, you were testing private methods of classes)
  * In this case : the tests are to blame
  * Either refactor the tests themselves or write an entirely new set of higher-level tests

## Concrete Practice

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTowNOVQl4CaedaobV%2F-MRTpUnhI5s-BK6POjhv%2Fimage.png?alt=media\&token=3e31c351-2c8b-45a5-8b8e-43ab9a868a74)

Follow the journey step by step as explained in [my website](https://ythirion.github.io/refactoring-journey/) :

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MTBmNOTN_eTzFJB6WHr%2F-MTBmWEHCaKHDr3QYmS7%2Fimage.png?alt=media\&token=c3503846-1d92-45a4-b0d2-1532f5c9d5ba)

#### Enjoy the Journey

![Refactoring Journey map](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MTo4MJMc-axRCCnes5g%2F-MTo4_ZPTzIWbjX19lam%2Frefactoring-map.webp?alt=media\&token=ef8930e4-c8d1-4710-892f-d62366a41981)

## Conclusion

**Learning feedback**

* Each participant add his/her name on the quadrant
* Please explain ***why you are there***

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MRTowNOVQl4CaedaobV%2F-MRTqNEHu_kHtSNlf1AL%2Fimage.png?alt=media\&token=6b921257-1ecb-48b5-af4e-edb71b09a3f5)
