I have some code that’s written like so:
private double function1()
{
double result2 = function2();
if (result2 < 0) { return result2; }
double result3 = function3();
if (result3 < 0) { return result3; }
return 0;
}
I need to re-write it such that it has only one return statement. Is there an easy way to do this? It strikes me as inefficient to begin with that the same if construct is used twice. How can this inefficiency be cleaned up?
Keep it simple, keep it readable.
A shorter version, probably easier on the eyes:
And
There is nothing inefficient about that. If the pattern was repeated (much) more you could start to think about an extra method or something to aid readability.