Refactoring is the process of changing code so that is functions in the same way. Refactoring is usually used to clean code up and make it more readable without effecting the functionality of the code. Testing can be used to verify the process of refactoring, and to ensure that the functionality of the code has stayed the same when the refactoring process has been completed.

Let’s take a simple multiplication function, a function that takes two parameters and return the multiplication of those two parameters. The function below takes two integer parameters, called ‘a’ and ‘b’ and returns the interger value of those two number multiplied together.

1 2 3 |
public int multiply(int a, int b){ return a * b; } |

The below code is a refactored version of the above. It functions the exact same, it take two parameters and returns the multiplication of those parameters. All we have done is swap the multiple order of the two parameters, this still returns the same result.

1 2 3 |
public int multiply(int a, int b){ return b * a; } |

The code below is the exact same as the two above snippets, it takes two parameters and returns the multiplication result of those parameters. The difference is we have refactored the code to rename the parameter variables to give them more meaningful names. Renaming variables does not change the functionality of code but it can make it more readable and easier to understand.

1 2 3 |
public int multiply(int firstNum, int secondNum){ return firstNum * secondNum; } |