> For the complete documentation index, see [llms.txt](https://claudiu-stanciu.gitbook.io/software-craft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://claudiu-stanciu.gitbook.io/software-craft/code-craft/refactoring.md).

# Refactoring

## Before refactoring

* Check the test coverage. If not sufficient, add more
* Tests should pass before attempting to refactor
* Ensure public access points/interfaces are well tested
* Depending on the refactoring level, ensure some of the lower level aspects are tested as well

## Refactoring techniques

<https://refactoring.guru/refactoring/techniques>

### Abstract

* Encapsulate fields
* Type checking/ cases => polymorphism

### Break-up code

* Large method => smaller multiple methods
* Large class => smaller classes with limited responsibility

### Improve naming and location

* Hard constant value => variable with a constant value. Gives a better name and allows searchability
