What is better stylewise/readability?
I have a loop that reads input and does different things according to the input, and when an error occurs, I need a simple return;. Example:
while( get_input() )
{
if( input == "somethingcool" )
{
if( !process_somethingcool() )
return; // <-- a couple of these
}
//...
else // bad input, error handling is fancier than this, but irrelevant to the question
return;
}
return;
So should I replace the individual return;s with a goto end; and place a label end: right above the last return in the example above or not? I am not in need of “use RAII” because nothing is allocated in the if blocks. Both ways would be identical in all senses of the word, except for the style/readability/performance?
I would suppose performance is identical, but just to be sure: is it?
For C, goto is reasonable (it’s used widely in the Linux kernel) as you can enhance readability with a single point of return.
For C++, because you have the possibility of anything throwing an exception, you implicitly have multiple points of return so you should always use the RAII model with multiple returns.