Is unnecessary error handling recommended in business logic? eg. Null check/Percentage limit check etc
- by novice_at_work
We usually put unnecessary checks in our business logic to avoid failures.
Eg.
1. public ObjectABC funcABC(){
ObjectABC obj = new ObjectABC;
..........
..........
//its never set to null here.
..........
return obj;
}
ObjectABC o = funABC();
if(o!=null){
//do something
}
Why do we need this null check if we are sure that it will never be null?
Is it a good practice or not?
2. int pplReached = funA(..,..,..);
int totalPpl = funB(..,..,..);
funA() just puts a few more restriction over result of funB().
Double percentage = (totalPpl==0||totalPpl<pplReached) ? 0.0 : pplReached/totalPpl;
Do we again need this check?
The questions is: Aren't we swallowing some fundamental issue by putting such checks? Issues which should be shown ideally, are avoided by putting these checks.
What is the recommended way?