Sign in
Log inSign up

Coding Pattern: Why should NOT we put multiple return statements in the same function?

Default profile photo
Anonymous
·Mar 20, 2018

Why is it considered an anti pattern? Those who abide by it - What's the justification?

For example, take a look at the following code:

if (ok)
   return "Ping";
else
   return "Pong";

Many experienced programmers in my company would rewrite the above as following:

var outcome;
if (ok)
   outcome = "Ping";
else
   outcome = "Pong";

return outcome;

Why is the 2nd approach better than the first?