I have to work with C-style functions which return ERROR_SUCCESS
when successful or an error code when not. I want my code to be nice and neat and easy to read but I am not sure I like what I have.
Option one:
int Foo()
{
int result = Init();
if (result == ERROR_SUCCESS)
{
result = SetTemperature( 30 );
}
else
{
return result;
}
if (result == ERROR_SUCCESS )
{
result = SetVoltage( 5 );
}
{
return result;
}
}
Option two:
int Foo()
{
int result = Init();
if (result != ERROR_SUCCESS)
return result;
result = SetTemperature( 30 );
if (result != ERROR_SUCCESS)
return result;
result = SetVoltage( 5 );
if (result != ERROR_SUCCESS)
return result;
}
Please note that I can't use exceptions here. If there is an error then I simply need to return from the function with that error. Is there an even better way to organize this code?
Foo()
makes this borderline hypothetical code. Hypothetical code would be off-topic for Code Review, but I've allowed the question to stay open sinceInit()
,SetTemperature()
, andSetVoltage()
seem plausible enough. \$\endgroup\$ – 200_success May 14 '14 at 13:39