# A Set of Refactorings

In this article, we will have a look at 5 ways you can improve the existing code.

# Extract Function

Sometimes you have a small piece of logic that when extracted to a function with a meaningful name, the code can become more readable and easier to work with and because you know what the function's intent is, you may also know whether you need to have a look into that function or not when reading the code.

Separate intent and implementation 

**From**

![Sample where the function could be extracted](https://cdn.hashnode.com/res/hashnode/image/upload/v1624633559609/NEUvV6_A8.png)

**To**

![Sample where the function is extracted](https://cdn.hashnode.com/res/hashnode/image/upload/v1624633601313/uI-wWbBEZ.png)

# Inline Function

In some instances, it is more readable to inline the function.

**From**

![Screenshot from 2021-06-26 09-11-22.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1624691598242/gl9PAm-Ve.png)

**To**

![Sample where the function is inlined](https://cdn.hashnode.com/res/hashnode/image/upload/v1624691521719/HrptVjPHJ.png)

# Renaming

This one is pretty simple, but will come in handy many times, renaming variables, for them to become more readable and requiring less cognitive load to understand.

**From**

![Sample with bad variable name](https://cdn.hashnode.com/res/hashnode/image/upload/v1624692330450/W9D5R9VsZ.png)

**To**

![Sample with good variable name](https://cdn.hashnode.com/res/hashnode/image/upload/v1624692372090/essGPncRh.png)

# Splitting variables

Sometimes you come across variables being reassigned, which itself is a code smell, oftentimes in such cases, the variable in one of the positions has a bad or wrong name.

This can be improved by instead of reassigning the variable, declaring a new one with a proper name.

Personally, from my experience, reassigning variables makes the code confusing, and often requires a lot of cognitive loads to understand the code.

**From**

![Sample with reassignment](https://cdn.hashnode.com/res/hashnode/image/upload/v1624692896678/H47kUx0bb.png)

**To**

![Sample with two variables](https://cdn.hashnode.com/res/hashnode/image/upload/v1624692977195/Y4vHik972.png)

# Extracting variables

In some instances, you may have a lot of logic going on, and by breaking up the logic into variables, it is not just easier to read the code and requires less cognitive load to understand, but also easier to debug the code because you can put in a debugger or log out the values.

**From**

![Sample with inlined variables](https://cdn.hashnode.com/res/hashnode/image/upload/v1624694734009/UUhVlSppS.png)

**To**

![Sample with extracted variables](https://cdn.hashnode.com/res/hashnode/image/upload/v1624694773730/gQHexDaHt.png)
