Code refactoring

Code refactoring:

Code refactoring is a disciplined technique for restructuring an existing code, altering its internal structure without changing its external behavior.

We should remember that the external behavior should not change when we do code refactoring. We should keep the following things in mind when we do code refactoring:

  • All our unit tests should pass before and after the modification
  • We should not do any change on existing test cases
  • New test cases for the code changes should not be introduced
  • We should deliver cleaner code when we are done with code refactoring
  • No new behavior should be introduced

Benefits of code refactoring

According to Ron Jeffries, one of the founders of Extreme Programming software methodology:

Refactoring is like clearing a field: Instead of working around the weeds, you clear a path through them

Following are some of the crucial benefits of code refactoring:

Saved time and money in the future

Code refactoring reduces the likelihood of errors in the future. It simplifies the implementation of new software functionality.

Reduce code complexity

If the code refactoring is done right, in most cases it reduces code complexity. It will be easier for new developers to comprehend the code and make the necessary alterations faster.

Maintainability and scalability

If we see a big chunk of dirty code, it is better to refactor it for maintainability and scalability.

Reference: