Refactoring

What is refactoring?

Refactoring is the process of rewritting parts of a working program to make it simpler, easier to work with, and more efficient.

When and why should you refactor code?

Consider refactoring code when you've solved the same problem in multiple places. This approach follows the DRY principle.

What is the DRY priniciple?

The DRY principle is short for don't repeat yourself. Code repetition presents opportunities for errors, makes programs harder to modify, and makes programs harder and longer to read.

How do you refactor code?

To refactor code, place a repeated block of code into a separate function. Then replace the repeated code with a function call. When you need to improve that code, you only need to change the code in the function for it to work throughout the program. Refactoring can help you find more efficient ways to solve problems, and separating code into different modules makes it easier to work with.