Refactoring 1 means changing code in a way that has no effect on functionality. It is only meant to make the code easier to understand or to prepare for some future addition of functionality. For example, sometimes you refactor code to make it easier to test. Many IDEs provide menus and key shortcuts for performing common refactorings.
There are two categories of refactoring we will cover, object-oriented and functional, corresponding to the two different programming styles.
Object-Oriented Refactoring
Changing a method or class name (renaming)
Moving a method from one class to another (delegation)
Moving a field from one class to another
Adding or removing parameters from a method
Creating a new class using a set of methods and fields from a class
Changing a local variable to a class field
Replacing a bunch of literals (strings or numbers) with a constant (static final)
Replacing some constants or literals with an enum
Moving a class from an anonymous class to a top-level class
Renaming a field
Functional Refactoring
Renaming a function
Wrapping a function in another function and calling it
Inline a function wherever it is called
Extract common code into a function (the opposite of the previous)
Renaming a function parameter
Adding or removing a parameter
You might notice some similarities between both lists. The principles of refactoring are universal.
Refactoring Examples
NetBeans, like many IDEs, supports refactoring. You can try it out by selecting some code, right-clicking, and selecting the Refactor menu. Here are some examples of refactoring code.
Renaming a Method
Moving a Method from One Class to Another (Delegation)
Replacing a Bunch of Literals (Strings or Numbers) with a Constant (Static Final)
Renaming a Function
Wrapping a Function in Another Function and Calling It
Inline a Function Wherever It Is Called
This might be done during refactoring when a function is too simple or is only used in one place.