My FeedDiscussionsHeadless CMS
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

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

Default profile photo
·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";
   return "Pong";

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

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

return outcome;

Why is the 2nd approach better than the first?