you're right... got a bit infected by the horror around.
C
coolM
@coolM
Posts
-
Avoid return statement in the middle - horror or not? -
Avoid return statement in the middle - horror or not?In case of complex preconditions separate those to another function. This prevents structured-ifs and keeps the calling-function readable. If the conditions change (in thight iterative development this always happens), adaption gets easy.
bool ShouldDoSomething()
{
//no returns in middle of method, only on start
// or everywhere.. ;)
if(!flagA) return false;
if(!flagB) return false;
if(!flagC) return false;
if(!PromptUser() ) return false;
return true;
}void callingFunction()
{
if( ShouldDoSomething() )
{
DoSomething();
}
else
{
DoOtherThing();
}
}