Consider the two code segments below. Which one is better and Why? If you have any other idea, please do mention. Where can I find answers to coding p
I would personally prefer a variation on your second code segment. The short circuiting will work the same, but the conditional is less verbose.
bool MyApplication::ReportGenerator::GenerateReport()
{
if(isAdmin() && isConditionOne() && isConditionTwo() && isConditionThree())
{
return generateReport();
}
return false;
}
It says everything in one nice clean spot. "If all these conditions are met, then do this. Otherwise, don't."
I feel like your first code segment makes that logic harder to see by spreading the condition across 12 lines. Also, the encasing loop might cause someone to do a double take.