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?

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct