Refactoring
Refactoring[14] is the practice of improving the design of code without breaking its functionality. In order to keep code simple and prevent the cost of making changes from skyrocketing, you must constantly refactor. This keeps your code as simple as possible.
Here is a simple refactoring. Suppose you have this code:
public class Person {
private String firstName;
public void setFirst(String n) {
this.firstName = n;
}
}This code can be improved by picking a better argument name, changing
n to firstName:
public class Person {
private String firstName;
public void setFirst(String firstName) {
this.firstName = firstName;
}
}The code can be improved even further by renaming the method to
setFirstName( ):
public class Person {
private String firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}The method has been refactored and is now more easily understandable. Of course, changing the method name requires you to change all references to the method throughout your application. This is where a good IDE can help out, because it can identify all usages and update the calls automatically.
Goals
You refactor code to make it simpler. Each individual refactoring introduces a small improvement; however, these small improvements add up over time. By constantly striving to keep code as concise and as simple as possible the cost of making changes to an application does not rise so dramatically over time.
Removing duplication is another goal of refactoring ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access