-
which way you prefer
I prefer this way (I dont like too many tabs when not needed):
Code:
void somefunc() {
if (something) return;
/*
do
a
lot
of
things
*/
}
Are there any particular reasons so that I should use this one:
Code:
void somefunc() {
if (!something) {
/*
do
a
lot
of
things
*/
}
}
Thanks for help
-
Wasn't it discussed not long ago?
Code:
void somefunc()
{
if (something)
{
return;
}
/*
do
a
lot
of
things
*/
}
-
I seem to remember reading somewhere that most compilers optimize on the assumption that the if condition will be true. This means that you should put the most likely outcome in the if part of the statement, rather than the else.
Of course, this is severely compiler dependent, but interesting nonetheless.
-
The only reason I can think of to use the second version is if you aren't using good RAII techniques and returning from the function early could leave dangling resources.
I generally prefer the first (with different formatting, though). If the return is nested deep and would be hard to see, then I might use the second. You want the return to be easy to see and obvious (which is why I use different formatting).
-
I prefer the first since each level of indentation makes it a little harder to understand the code. If the "!something" branch does a lot of work, then even if it's the high-probability branch any efficiency loss from not hitting it first will be negligible.