Code Monkey home page Code Monkey logo

Comments (5)

romanfuchs avatar romanfuchs commented on May 5, 2024

Well, I think you can distinguish two cases. If it's input validation at the top of the function, then I think you should return right away.

If you're further down in a more complicated function, you probably want to avoid having too many exits.

from javascript.

hshoff avatar hshoff commented on May 5, 2024

If a function is getting complicated it probably should be refactored into smaller more readable chunks.

It might help to look at some code for this. Maybe someone can come up with an example where it's harder to read while returning early?

from javascript.

horaceko avatar horaceko commented on May 5, 2024

This example isn't strictly JS, but I'd point to any of our over-long controller actions (e.g. payments#book) as return-anywhere taken to the terrible extreme. There's no reason to think that (a milder form of) this wouldn't happen for JS.

Handling returns in a single location means that it's absolutely clear what the execution path through the function is, which makes debugging code a lot easier; there's no question of "did this block of code get executed?"

from javascript.

reissbaker avatar reissbaker commented on May 5, 2024

The real problem with payments#book is that it's hella long. A short function with trivial base-case return statements at the top is pretty easy to read. I like returning early in recursive functions where there are a few base cases and then a recursive case, like this:

function factorial(n) {
  if(n <= 1) return 1;
  return n * factorial(n - 1);
}

The opposite looks much less readable to me, and results in the unnecessary creation of a variable:

function factorial(n) {
  var fact;
  if(n <= 1) {
    fact = 1;
  } else {
    fact = factorial(n - 1);
  }
  return fact;
}

It might just be context-dependent, though, like @romanfuchs said.

from javascript.

horaceko avatar horaceko commented on May 5, 2024

The single-return pattern is great for functions that are going to get lots of stuff tossed into it (e.g. glue code, boilerplate) because it makes code bloat more maintainable. The multi-return pattern is great for small, well-scoped functions with well-encapsulated functionality (e.g. library code) because it simplifies the implementation and is easier to read.

We shouldn't vilify either pattern. Let's explain their merits instead, and let the developer decide.

from javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.