Imagine a simple banking application wherein a use case of fund transfer is being realized. While writing transfer fund operation, the programmer/designer has following two options
-
Write an operation to check if there are sufficient funds or not and return a Boolean based on which the subsequent fund transfer option will be carried out. This require creating two function viz. the checkSufficientFunds() and transferFund()
-
Write a single operation which itself checks if the funds are sufficient or not. This will throw an checked Exception if there are no sufficient funds and calling method have to handle this.
I understand that this is an oversimplified scenario. My question is, in theory, what are the performance implications (Memory and CPU) of these two approaches? How do they compare?
The performance difference in the current JVMs should be negligible. However, stick to the rule that exceptions should be used in exceptional situations. It is a normal flow not to have sufficient funds, so I would use a method
public boolean transferFund()that returnstrueif successful, andfalseotherwise. This suggestion violates the command/query separation, but I think it’s fine.