My preference for option A is because it leaves less room for errors
to creep in. If the invocation of a callback should end processing in
a given branch, then it should be coupled directly with a return. If
you do callback(...); return;, there's a chance that later on someone
might stick some logic between the invocation of the callback and the
return statement which could potentially cause unexpected issues. It
also means that once you get into that habit places where you forget
to return with your callback invocation (which can be a very common
cause of errors in node) really stand out.

Maybe this whole debate goes away in 0.8.x when we have Domains though ;).

On Wed, Apr 11, 2012 at 10:57 AM, Tim Caswell <t...@creationix.com> wrote:
> I also initially preferred approach B "{callback();return}", but now use
> approach A "return callback()".  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.
>
> If any of the V8 developers could shed more light on how these two options
> affect the optimizer or runtime performance that would be interesting.
>
> I prefer the single statement because:
>
>  - It's a habit from languages with tail recursion.  If you return statement
> is a call to a function, it's more like a goto in cost than a function call.
>  JavaScript doesn't have this in any engine I'm aware of so it's more a
> habit than anything.  (also the return value is almost always ignored or we
> wouldn't be having this discussion in the first place)
>
> - It's less code.  When in doubt go for less.
>
>
> On Wed, Apr 11, 2012 at 9:29 AM, Mark Volkmann <r.mark.volkm...@gmail.com>
> wrote:
>>
>> On Tue, Apr 10, 2012 at 6:17 PM, Ken <ken.woodr...@gmail.com> wrote:
>> > Assuming that the callback doesn't return a value, does v8 behave any
>> > differently when invoking callbacks in one of these forms vs. the
>> > other?  I
>> > find the first approach to be a convenient shorthand in many cases, but
>> > am
>> > wondering (after observing some unexpected timings when profiling async
>> > methods) if it leads to v8 doing something odd with the stack.
>> >
>> > Approach A, return the invoked callback:
>> >
>> > function foo(a, callback) {
>> >   var bar = ...;
>> >   return callback(bar);
>> > }
>> >
>> > foo("derp", function(b) { ...; return; });
>> >
>> > Approach B, invoke callback, then return:
>> >
>> > function foo(a, callback) {
>> >    var bar = ...;
>> >    callback(bar);
>> >    return;
>> > }
>> >
>> > foo("derp", function(b) { ...; return; });
>>
>> 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.
>>
>> --
>> 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 nodejs@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>
>
> --
> 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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to