--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
What about short circuiting? The example didn't illustrate
short-circuiting but it's somewhat common and probably the OP's
inspiration.
function (opts, cb) {
thing(opts, function (err, data) {
if (err) {
cb(err);
return;
}
var result = transform(data);
cb(null, result);
});
});
In this case, we use return to stop callback execution, and for async
use the actual return value doesn't matter because it can't get
assigned to anything so combining the lines into `return cb(err)`makes
for a reasonable shorthand. Other than that, they're roughly
equivalent.
--Josh
--
Joshua Holbrook
Engineer
Nodejitsu Inc.
jo...@nodejitsu.com
Initially I really hated approach A, but it seems that approach is
very common and it has grown on me. I always use approach A now.
--
R. Mark Volkmann
Object Computing, Inc.
Maybe this whole debate goes away in 0.8.x when we have Domains though ;).
At the end of the day there's not a huge difference between options A
and B, so any preference for one or the other can easily be
questioned, I tend to err on the side of trying to prevent stupid
mistakes through convention whenever possible, in my experience it
tends to keep quality stable over the lifespan of the code's existence
and across many maintainers.
Yes there is a slight difference under the hood, at least in the initial abstract syntax tree. Usually this is done in an async callback where the return value is going to be ignored anyway.
function (opts, cb) {
thing(opts, function (err, data) {
if (err) {
cb(err);
} else {
var result = transform(data);
cb(null, result);
}
});
});
Aside from readability and number of characters to type, is this
option C better or worse than using return?
First: Would you be so kind and stop talking in third person about me
when I'm around? Thank you.
Second: I know that this requires to intend the rest of the code.
But what I really want to know is about technical reasons, not style.
I want to know if it makes any difference to the JIT, v8, or whatever
stacks around. Is not using return an improvement, an deterioration or
does it make no difference for performance/code optimization.
So, under the scope of the question of the OP: What about not using return?
I'm sorry. I think I wanted to write "this", not "his". That probably looked a bit offensive. :(
> Second: I know that this requires to intend the rest of the code.
>
> But what I really want to know is about technical reasons, not style.
> I want to know if it makes any difference to the JIT, v8, or whatever
> stacks around. Is not using return an improvement, an deterioration or
> does it make no difference for performance/code optimization.
>
> So, under the scope of the question of the OP: What about not using return?
If you're concerned that much about speed, you might want to hack in C or so. :D And if
you believe in microbenchmarks: http://jsperf.com/return-vs-no-return
:-) And maybe I was just too thin-skinned.
>> Second: I know that this requires to intend the rest of the code.
>>
>> But what I really want to know is about technical reasons, not style.
>> I want to know if it makes any difference to the JIT, v8, or whatever
>> stacks around. Is not using return an improvement, an deterioration or
>> does it make no difference for performance/code optimization.
>>
>> So, under the scope of the question of the OP: What about not using return?
>
> If you're concerned that much about speed, you might want to hack in C or so. :D And if
> you believe in microbenchmarks: http://jsperf.com/return-vs-no-return
I'm not really concerned, just curious.
And if I would believe in the benchmarks you mentioned, than I had to
say that there is no definite answer.
BTW: It comes from python, where they say: Do not return early, use if else.