What is code refactoring, in plain english?
In my last post I spoke about code refactoring, I did not stop to realise that some people would not know what this means.The question is, when do we actually stop!! As an example, I have this example of java code. it can be refactored.
public class Bookshelf {
private int capacity = 20;
private Collection content;
public void add(Book book) {
if(content.size() + 1 <= capacity) {
content.add(book);
} else {
throw new IllegalOperationException(
"The bookshelf has reached its limit.");
}
}
}
private int capacity = 20;
private Collection content;
public void add(Book book) {
if(content.size() + 1 <= capacity) {
content.add(book);
} else {
throw new IllegalOperationException(
"The bookshelf has reached its limit.");
}
}
}
This can be refactored too the following code.
public class Bookshelf {
private int capacity = 20;
private Collection content;
public void add(Book book) {
if(isSpaceAvailable()) {
content.add(book);
} else {
throw new IllegalOperationException(
"The bookshelf has reached its limit.");
}
}
private boolean isSpaceAvailable() {
return content.size() < capacity;
}
}
private int capacity = 20;
private Collection content;
public void add(Book book) {
if(isSpaceAvailable()) {
content.add(book);
} else {
throw new IllegalOperationException(
"The bookshelf has reached its limit.");
}
}
private boolean isSpaceAvailable() {
return content.size() < capacity;
}
}
Now the question is this, how much more can we refactor this code? Anyone have any ideas on the next step?
-
I find it amusing that the example of refactoring in "plain english" is done in source code :)
Refactoring, as I interpret it, refers to optimizing implementation of logic without changing an object's external API. Your example is really good, and something I don't see enough people doing in CFCs: logic that is a candidate for reuse (determination of space being available in a book shelf) is moved out of a function (addBook()) that has nothing to do with this logic except its use, not its implementation and placed into its own (private, which is apprpriate!) method (isSpaceAvailable()).# Posted By Joe Rinehart | 5/22/08 9:32 AM -
@Joe: Code refactoring, believe it or not. Is more about code readability, most people seem to not understand that, or comprehend that by removing something so simple.
Not only makes the code more readable, but it also allows for that method to be used again down the track. But it is the making it more readable, that allows us to think along the lines of reusing the code.
Seems backwards if you ask me, but it works.# Posted By Andrew Scott | 5/22/08 9:43 AM -
@ Joe: I should have added that your interpretation is not wrong either, but I am going by the example that was given to us in our weekly workshops here at work.
That was the exact same example that was used.
@everyone else: If you haven't noticed the next logical step would have actually have been.
public class Bookshelf {
private int capacity = 20;
private Collection content;
public void add(Book book) {
if(!isSpaceAvailable())
throw new IllegalOperationException("The bookshelf has reached its limit.");
content.add(book);
}
private boolean isSpaceAvailable() {
return content.size() < capacity;
}
}
Minor change, but it is a change that makes it more readable again.
Also there are no hard / fast rules on refactoring and it comes down to expereince to the level you code at.# Posted By Andrew Scott | 5/22/08 9:48 AM



TweetBacks