What is refactoring?
Refactoring has two definition depending on context:
1) Refactoring (noun): A change made to the internal structure of software to make it easier to understand and cheaper to modify with- out changing its observable behavior.
2) Refactor (verb): To restructure software by applying a series of refactorings without changing its observable behavior.
Is refactoring just cleaning up code?
To some extent, yes. But refactoring goes further than just cleaning up your code. Refactoring makes the software easier the software easier to understand and modify. It adds productivity to you and your team in the long term.
Refactoring does not change the function of your program. The end user or (if your working on an API) the programs using your API, cannot tell that things have changed.
Why should you refactor?
Refactoring is not a “silver bullet” for software development. It is a however a valuable tool that helps you and your team efficiently maintain your code.
Refactoring Improves the Design of Software
I have never seen a poorly designed program, on day one that is. We often fail to realize that the design of software will eventually decay. This is because, people change code, whether changes are temporary just to fix an ASAP issue or changes made without the full understanding of the system—the code eventually loses its structure.
Refactoring does not necessarily changing the whole design in one go. Work is done to remove bits of code that are not in the right place.
Refactoring Makes Software Easier to Understand
In programming, the computer understands whatever code you give it. However, the problem comes in you have other programmers that work on the same code base. If it takes another programmer weeks to make changes on your code that would have only taken a few weeks if he/she understands your code like you do.
Refactoring helps you to make your code more readable to other programmers. When refactoring, you spot code that works but is not ideally structured. Little time investments spent refactoring your code to make it more readable by other programmers would give you massive productivity gains.
Source: Refactoring - Ruby Edition