3 ms·
One drawback of returning early is it can complicate debugging. If I want to stop on a breakpoint when a function exits, it's nice to be able to set a single br
by ryandrake 9y ago
One drawback of returning early is it can complicate debugging. If I want to stop on a breakpoint when a function exits, it's nice to be able to set a single breakpoint on the final return statement. When you spew returns all over the function, your life gets more complicated. Some debuggers let you put the breakpoint on the final brace and will do the right thing, others make you find and put a breakpoint on each return.
Another thing I avoid doing because it complicates debugging is putting a final calculation on the same line as the return:
return combine(foo.calculate(), bar.calculate());
Some debuggers make it difficult for me to examine the result of the combine() function and/or the results of the .calculate() methods. Better to be explicit:
auto foo_result(foo.calculate());
auto bar_result(bar.calculate());
auto combine_result(combine(foo_result, bar_result);
return combine_result;
There, much better, arguably more readable, and I can stop at whatever step I need to.
I've had to debug far more code in my career than I've had to write, which has encouraged practices that make debugging as straightforward as possible.