So if I have a big function I break it to smaller ones to increase readability.
If I have parts in my big function where I want it to return. I want my subfunctions to have the ability to make the main function terminate as well, if a condition is true in the subfunction. What's the best approach for this?
Making subfunctions return a bool variable seems messy with the too many ifs in the main function.
OriginalFunction
void MainFunction()
{
//DoStuff1
....
....
if(condition1)
return;
//DoStuff2
....
....
if(condition1)
return;
//DoStuff3
....
....
if(condition1)
return;
//DoStuff4
....
....
if(condition1)
return;
}
Modified Function
void MainFunctionModified()
{
if(DoStuff1())
return;
if(DoStuff2())
return;
if(DoStuff3())
return;
if(DoStuff4())
return;
}
bool DoStuff1()
{
....
....
if(condition1)
return true;
return false;
}