J
Jordan
While writing some code, I realized I had never developed a consistent
pattern for checking errors from a method. I have two styles I jump
back and forth between, but I'm wondering which is better.
Scenario:
* In method that returns success/fail
* Call method that also returns success/fail
* Need to check return state and continue or exit outer method
I personally think Case 1 looks a little cleaner but it breaks more
than case 2. Any opinions?
Case 1:
bool flag = myFunc1( );
if( flag == false )
{
return;
}
flag = myFunc2( );
if( flag == false )
{
return;
}
// more code
Case 2:
bool flag = myFunc1( );
if( flag == true )
{
flag = myFunc2( );
if( flag == true )
{
flag = myFunc3( );
if( flag == true )
{
// continue code
}
}
}
pattern for checking errors from a method. I have two styles I jump
back and forth between, but I'm wondering which is better.
Scenario:
* In method that returns success/fail
* Call method that also returns success/fail
* Need to check return state and continue or exit outer method
I personally think Case 1 looks a little cleaner but it breaks more
than case 2. Any opinions?
Case 1:
bool flag = myFunc1( );
if( flag == false )
{
return;
}
flag = myFunc2( );
if( flag == false )
{
return;
}
// more code
Case 2:
bool flag = myFunc1( );
if( flag == true )
{
flag = myFunc2( );
if( flag == true )
{
flag = myFunc3( );
if( flag == true )
{
// continue code
}
}
}