0
$\begingroup$

I have traditionally been doing things like this:

function test() // A
{
    if (/* conditions */)
        return true;
    else
        return false;
}

Of late, I have instead starting doing, and converting existing code like the example above, into this:

function test() // B
{
    if (/* conditions */)
        return true;

    return false;
}

I can see arguments for both ways, but the "new" way (to me) seems to win after all. I used to not "parse" code correctly in my head, so I thought that it could sometimes continue executing the code in a function after a "return" has been encountered, which is not actually the case in reality. At least I think (and hope) so...

I'm interested in hearing objective arguments for/against what we can call "A" or "B" for simplicity.

$\endgroup$
  • $\begingroup$ Look at the “guard” statement in Swift and think when you would use it. $\endgroup$ – gnasher729 Nov 26 '19 at 16:48
  • $\begingroup$ (Then again, the condition is a boolean - just return condition;) $\endgroup$ – greybeard Nov 26 '19 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.