Here is a class that leaks its internals. What is wrong with it, and how would you fix it without breaking the way it is used?
The problem is that the field is public, so the caller can put anything into it and take anything out of it, and the class has no control over its own state.
public class Main {
public static void main(String[] args) {
Invoice invoice = new Invoice();
invoice.lines.add("Widget");
System.out.println(invoice.total()); // this cannot work
}
}
And here is the class with the problem in it.
public class Invoice {
public final java.util.List<String> lines = new java.util.ArrayList<>();
public double total() {
double sum = 0;
for (String line : lines) {
sum = sum + priceOf(line);
}
return sum;
}
}
So what is wrong with it? Five things.
the problem why it matters
----------------------------------- --------------------------------------------
the list can be changed by anyone the class cannot keep the total
right
nothing is checked a bad line can go in
the total is worked out each time it is slow, and it can disagree
with the list
there is no way to add a discount every rule has to go into the
class later
the caller has to know the shape the list type is part of the
contract
And the first one is the worst. If the caller removes a line then the total changes, and if the caller adds a line that has no price then the total method throws an exception. So the class can be made to misbehave by code that has nothing to do with the class.
So how do you fix it without breaking the way it is used? You keep the old method and add the new one, and you change the callers over one at a time.
public class Invoice {
private final java.util.List<Line> lines = new java.util.ArrayList<>();
private double total = 0;
public void addLine(String description, double price) {
if (price < 0) {
throw new IllegalArgumentException("price cannot be negative");
}
lines.add(new Line(description, price));
total = total + price;
}
public double total() {
return total;
}
}
And the new method is addLine, which takes the description and the price and checks the price and keeps the total up to date. The old field is now private, so the callers have to change from invoice.lines.add("Widget") to invoice.addLine("Widget", 9.99).
And the way to do that without breaking anything is a sequence.
the step what you do
-------------------------------- --------------------------------------------
1. add the new method the new way works, the old way
still works
2. change the callers one caller at a time
3. make the field private now nothing can use it
4. delete the old code once nothing calls it
And the order matters. If you make the field private at step one then the code stops compiling and everybody has to change at the same time. So you add first, change second, and hide last.
And there is one detail worth knowing about. The old public field was a list of strings, and the new method takes a description and a price. So the callers have to know the price as well. If the old code only had the description then the prices must have come from somewhere else, and that somewhere else is the price lookup. In that case the new method should look the price up itself, so that the caller does not have to know about prices at all.
So the rule to remember is this. Add the new method first, move the callers over, and hide the field last.
The last question in this topic is about reviewing a whole code base for this.