Comments (5)
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.
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.
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.
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.
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)
- `import/no-unresolved` doesn't support package.json exports field HOT 1
- Style guide question: how to handle names with consecutive initialisms? HOT 2
- Deprecation of formatting rules HOT 3
- [spam]
- [spam] HOT 1
- [spam]
- [spam] HOT 1
- [spam]
- [spam]
- [spam]
- [spam]
- [spam]
- `defaultProps` is deprecated for functional components HOT 1
- [spam]
- [spam]
- Support Biome extends HOT 3
- [spam] HOT 1
- [spam]
- [spam] HOT 1
- Multiline comments HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript.