I have some questions considering the exercises in "the pragmatic programmer".
It says:
1.
public void showBalance(BankAccount acct) {
Money amt = acct. getBalance() ;
printToScreen(amt .printFormat()) ;
}
The variable acct is passed in as a parameter, so the getBalance call is allowed. Calling amt.printFormat(), however, is not. We don't "own"amt and it wasn't passed to us.
But we do own amt right? It is declared in the method and the LOD states: When your method creates local objects, that method can call methods on the local objects.
Is this example breaking the LOD? The way I see it, it isn't?
2.
public class Colada {
private Blender myBlender;
private Vector myStuff;
public Colada() {
myBlender = new Blender();
myStuff = new Vector() ;
}
private void doSomething() {
myBlender.addlngredients(myStuff.elements());
}
}
Since Colada creates and owns both myBlender and myStuff, the calls to addIngredients and elements are allowed .
Now I don't understand why doSomething is allowed to make calls to myBlender and myStuff since it didn't create it.
3.
void processTransaction(BankAccount acct, int) {
Person *who;
Money amt;
amt.setValue(123.45);
acct.setBalance(amt);
who = acct .getOwner() ;
markWorkflow(who->name(), SET_BALANCE);
}
In this case, processTransaction owns amt, it is created on the stack, acct is passed in, so both setValue and setBalance are allowed. But processTransaction does not own who, so the call who->name() is in violation.
So here it does declare who but it is not allowed to make calls to it. Perhaps I misunderstand the concept of "owns".
Can someone please clarify this?
Thanks