FAQ
Hi,

Struggling to change coding practice, following has two samples, which
one is correct? or any other approaches available?

exports.get_user = function (id, func) {
var found = false
for (var i = 0; i < users.length; i ++) {
if (users[i].id === id) {
found = true
func(null, users[i])
break;
}
}
if (!found)
func("NOT_FOUND", null)
}


exports.get_user = function (id, func) {
for (var i = 0; i < users.length; i ++) {
if (users[i].id === id) {
func(null, users[i])
break;
}
if (i == users.length)
func("NOT_FOUND", null)
}
}

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

Search Discussions

  • Axel Kittenberger at Mar 9, 2012 at 11:03 am
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);
    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.

    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Angelo Chen at Mar 9, 2012 at 11:32 am
    oh, return is cool!
    On Mar 9, 7:02 pm, Axel Kittenberger wrote:
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);

    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.
    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mark Volkmann at Mar 9, 2012 at 4:40 pm
    Here's another approach that I prefer:

    exports.get_user = function (id, cb) {
    var user;
    users.some(function (u) {
    var found = u.id === id;
    if (found) user = u;
    return found;
    });
    cb(user ? null : 'not found', user);
    }
    On Fri, Mar 9, 2012 at 5:02 AM, Axel Kittenberger wrote:
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);
    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.

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


    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Diogo Resende at Mar 9, 2012 at 4:48 pm
    That doesn't work..

    --
    Diogo Resende

    On Friday, March 9, 2012 at 16:40 , Mark Volkmann wrote:

    Here's another approach that I prefer:

    exports.get_user = function (id, cb) {
    var user;
    users.some(function (u) {
    var found = u.id === id;
    if (found) user = u;
    return found;
    });
    cb(user ? null : 'not found', user);
    }
    On Fri, Mar 9, 2012 at 5:02 AM, Axel Kittenberger (mailto:axkibe@gmail.com)> wrote:
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);
    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.

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


    --
    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 (mailto:nodejs@googlegroups.com)
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com (mailto:nodejs+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Diogo Resende at Mar 9, 2012 at 4:59 pm
    My mistake. Thinking about users.some() as not being Array.some() and possibly executed later on. Array.some() is perfect for this case.

    --
    Diogo Resende

    On Friday, March 9, 2012 at 16:48 , Diogo Resende wrote:

    That doesn't work..

    --
    Diogo Resende

    On Friday, March 9, 2012 at 16:40 , Mark Volkmann wrote:

    Here's another approach that I prefer:

    exports.get_user = function (id, cb) {
    var user;
    users.some(function (u) {
    var found = u.id === id;
    if (found) user = u;
    return found;
    });
    cb(user ? null : 'not found', user);
    }
    On Fri, Mar 9, 2012 at 5:02 AM, Axel Kittenberger (mailto:axkibe@gmail.com)> wrote:
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);
    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.

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


    --
    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 (mailto:nodejs@googlegroups.com)
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com (mailto:nodejs+unsubscribe@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 (mailto:nodejs@googlegroups.com)
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com (mailto:nodejs+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Nathan Rajlich at Mar 9, 2012 at 5:05 pm
    Except that you can't return in one case, then fire a callback in the
    other case :\
    On Fri, Mar 9, 2012 at 8:58 AM, Diogo Resende wrote:
    My mistake. Thinking about users.some() as not being Array.some() and possibly executed later on. Array.some() is perfect for this case.

    --
    Diogo Resende

    On Friday, March 9, 2012 at 16:48 , Diogo Resende wrote:

    That doesn't work..

    --
    Diogo Resende

    On Friday, March 9, 2012 at 16:40 , Mark Volkmann wrote:

    Here's another approach that I prefer:

    exports.get_user = function (id, cb) {
    var user;
    users.some(function (u) {
    var found = u.id === id;
    if (found) user = u;
    return found;
    });
    cb(user ? null : 'not found', user);
    }
    On Fri, Mar 9, 2012 at 5:02 AM, Axel Kittenberger (mailto:axkibe@gmail.com)> wrote:
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);
    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.

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


    --
    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 (mailto:nodejs@googlegroups.com)
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com (mailto:nodejs+unsubscribe@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 (mailto:nodejs@googlegroups.com)
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com (mailto:nodejs+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mark Volkmann at Mar 9, 2012 at 5:42 pm
    That is not what the code is doing. The return that is in the function
    passed to Array.some is only used to determine when it can stop
    iterating through the array and what the return value of the some
    function will be. It does not return the from the get_user function.
    On Fri, Mar 9, 2012 at 11:04 AM, Nathan Rajlich wrote:
    Except that you can't return in one case, then fire a callback in the
    other case :\
    On Fri, Mar 9, 2012 at 8:58 AM, Diogo Resende wrote:
    My mistake. Thinking about users.some() as not being Array.some() and possibly executed later on. Array.some() is perfect for this case.

    --
    Diogo Resende

    On Friday, March 9, 2012 at 16:48 , Diogo Resende wrote:

    That doesn't work..

    --
    Diogo Resende

    On Friday, March 9, 2012 at 16:40 , Mark Volkmann wrote:

    Here's another approach that I prefer:

    exports.get_user = function (id, cb) {
    var user;
    users.some(function (u) {
    var found = u.id === id;
    if (found) user = u;
    return found;
    });
    cb(user ? null : 'not found', user);
    }
    On Fri, Mar 9, 2012 at 5:02 AM, Axel Kittenberger (mailto:axkibe@gmail.com)> wrote:
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);
    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.

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


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


    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Isaac Schlueter at Mar 9, 2012 at 5:12 pm
    Do you see the bug?

    exports.get_user = function (id, func) {
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    func(null, users[i])
    break;
    }
    if (i == users.length) // <-- Never happens, ever.
    func("NOT_FOUND", null)
    }
    }

    Because the if(i == users.length) is INSIDE the for loop, it can't be
    reached, since the for loop conditional will break out of the loop
    before then. It should go one line down, after the end of the
    for(;;).



    On Fri, Mar 9, 2012 at 08:40, Mark Volkmann wrote:
    Here's another approach that I prefer:

    exports.get_user = function (id, cb) {
    var user;
    users.some(function (u) {
    var found = u.id === id;
    if (found) user = u;
    return found;
    });
    cb(user ? null : 'not found', user);
    }
    On Fri, Mar 9, 2012 at 5:02 AM, Axel Kittenberger wrote:
    Many coders forget that you can use the "return" statement before the
    end of the function. At least most people who finished some coding
    course hardly ever do this. It usually simplifies many functions:

    exports.get_user = function (id, func) {
    for (var i = 0, z = users.length; i < z; i ++) {
    var u = users[i];
    if (u.id === id) {
    func(null, u);
    return;
    }
    }
    func('NOT_FOUND', null);
    }

    Depending on optimizing speed vs memory use, or if this function is
    heavly used compared to only seldomly (profiler!) l it might be a good
    idea to keep a table handy, where the users are sorted with their id.

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


    --
    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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Cooper at Mar 9, 2012 at 2:30 pm

    On Fri, Mar 9, 2012 at 2:30 AM, Angelo Chen wrote:
    Hi,

    Struggling to change coding practice, following has two samples, which
    one is correct? or any other approaches available?
    For this particular use case, it would be more convenient if 'users'
    was an object instead of an array, since you could then simply
    reference users[id] instead of having to walk the array. In fact, even
    if you do need an array for other use cases, it may be worth
    considering keeping an object as well, especially if the number of
    users gets long and / or get_user is called a lot.

    --
    Martin Cooper

    exports.get_user = function (id, func) {
    var found = false
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    found = true
    func(null, users[i])
    break;
    }
    }
    if (!found)
    func("NOT_FOUND", null)
    }


    exports.get_user = function (id, func) {
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    func(null, users[i])
    break;
    }
    if (i == users.length)
    func("NOT_FOUND", null)
    }
    }

    --
    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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Roly Fentanes at Mar 9, 2012 at 6:36 pm
    btw http://www.devthought.com/2011/12/22/a-string-is-not-an-error/
    On Friday, March 9, 2012 3:30:49 AM UTC-7, Angelo Chen wrote:

    Hi,

    Struggling to change coding practice, following has two samples, which
    one is correct? or any other approaches available?

    exports.get_user = function (id, func) {
    var found = false
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    found = true
    func(null, users[i])
    break;
    }
    }
    if (!found)
    func("NOT_FOUND", null)
    }


    exports.get_user = function (id, func) {
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    func(null, users[i])
    break;
    }
    if (i == users.length)
    func("NOT_FOUND", null)
    }
    }
    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Wawrusch at Mar 9, 2012 at 6:49 pm

    On Fri, Mar 9, 2012 at 10:36 AM, Roly Fentanes wrote:

    btw http://www.devthought.com/2011/12/22/a-string-is-not-an-error/


    +1 for that, and while we are at it: The whole thread is a great example
    why it is so important to use libraries like underscore's find:
    http://documentcloud.github.com/underscore/#find even for seemingly trivial
    tasks.

    And getUsers is probably a better name than the rubyfied one.


    On Friday, March 9, 2012 3:30:49 AM UTC-7, Angelo Chen wrote:

    Hi,

    Struggling to change coding practice, following has two samples, which
    one is correct? or any other approaches available?

    exports.get_user = function (id, func) {
    var found = false
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    found = true
    func(**null, users[i])
    break;
    }
    }
    if (!found)
    func("**NOT_FOUND", null)
    }


    exports.get_user = function (id, func) {
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    func(**null, users[i])
    break;
    }
    if (i == users.length)
    func("**NOT_FOUND", null)
    }
    }
    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mark Volkmann at Mar 9, 2012 at 6:56 pm
    For what it's worth, I was trying to minimize the changes to the function
    and mainly demonstrate the use of the Array "some" method for iteration
    that can benefit from stopping early.

    ---
    R. Mark Volkmann
    Object Computing, Inc.

    On Mar 9, 2012, at 12:49 PM, Martin Wawrusch wrote:


    On Fri, Mar 9, 2012 at 10:36 AM, Roly Fentanes wrote:

    btw http://www.devthought.com/2011/12/22/a-string-is-not-an-error/


    +1 for that, and while we are at it: The whole thread is a great example
    why it is so important to use libraries like underscore's find:
    http://documentcloud.github.com/underscore/#find even for seemingly trivial
    tasks.

    And getUsers is probably a better name than the rubyfied one.


    On Friday, March 9, 2012 3:30:49 AM UTC-7, Angelo Chen wrote:

    Hi,

    Struggling to change coding practice, following has two samples, which
    one is correct? or any other approaches available?

    exports.get_user = function (id, func) {
    var found = false
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    found = true
    func(**null, users[i])
    break;
    }
    }
    if (!found)
    func("**NOT_FOUND", null)
    }


    exports.get_user = function (id, func) {
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    func(**null, users[i])
    break;
    }
    if (i == users.length)
    func("**NOT_FOUND", null)
    }
    }
    --
    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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Jimb Esser at Mar 9, 2012 at 9:14 pm
    Though Errors definitely have their place, such as when an error
    occurs, using an error for regular return codes during expected events
    (assuming this function expects the look up to fail some times) is a
    bit overkill, and horrible for performance.

    http://jsperf.com/a-string-is-not-an-error

    On Mar 9, 10:36 am, Roly Fentanes wrote:
    btw http://www.devthought.com/2011/12/22/a-string-is-not-an-error/






    On Friday, March 9, 2012 3:30:49 AM UTC-7, Angelo Chen wrote:

    Hi,
    Struggling to change coding practice, following has two samples, which
    one is correct? or any other approaches available?
    exports.get_user = function (id, func) {
    var found = false
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    found = true
    func(null, users[i])
    break;
    }
    }
    if (!found)
    func("NOT_FOUND", null)
    }
    exports.get_user = function (id, func) {
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    func(null, users[i])
    break;
    }
    if (i == users.length)
    func("NOT_FOUND", null)
    }
    }
    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Angelo Chen at Mar 10, 2012 at 2:15 pm
    Hi All,

    surprised to see many discussions about this, I'd say i learnt a lot:

    1) return from loop
    2) Array.some
    3) return error instead of string
    4) string is faster sometimes

    all are very valuable, after evaluating my use case, I choose 2 and 4,
    and reserve the rest for future use, very educational, thanks to all.

    Angelo

    On Mar 10, 5:14 am, Jimb Esser wrote:
    Though Errors definitely have their place, such as when an error
    occurs, using an error for regular return codes during expected events
    (assuming this function expects the look up to fail some times) is a
    bit overkill, and horrible for performance.

    http://jsperf.com/a-string-is-not-an-error

    On Mar 9, 10:36 am, Roly Fentanes wrote:






    btwhttp://www.devthought.com/2011/12/22/a-string-is-not-an-error/
    On Friday, March 9, 2012 3:30:49 AM UTC-7, Angelo Chen wrote:

    Hi,
    Struggling to change coding practice, following has two samples, which
    one is correct? or any other approaches available?
    exports.get_user = function (id, func) {
    var found = false
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    found = true
    func(null, users[i])
    break;
    }
    }
    if (!found)
    func("NOT_FOUND", null)
    }
    exports.get_user = function (id, func) {
    for (var i = 0; i < users.length; i ++) {
    if (users[i].id === id) {
    func(null, users[i])
    break;
    }
    if (i == users.length)
    func("NOT_FOUND", null)
    }
    }
    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Axel Kittenberger at Mar 10, 2012 at 3:50 pm
    If your user are not in a particular order, add to this: don't use an
    array if there is no order, but a hash (object).

    users = {};

    e.g. with adding a user by: users[id] = {|somenewuserdata|};

    In that case getting a new user is easy as:

    exports.get_user = function(id, func) {
    func(users[id] ? null : "NOT_FOUND", users[id] || null);
    }

    I wonder, why you need a callback after all? Its not an IO operation
    with a delay. If not needed due to overall structure or so, it gets
    even simpler:

    exports.get_user = function(id) {
    return users[id] || null;
    }

    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Angelo Chen at Mar 10, 2012 at 10:32 pm
    This does bring out another topic, when we need a call back? Any
    rule ? In this case the function wii be replaced with a database one
    later, so it might have some IO.

    On Mar 10, 11:49 pm, Axel Kittenberger wrote:
    If your user are not in a particular order, add to this: don't use an
    array if there is no order, but a hash (object).

    users = {};

    e.g. with adding a user by: users[id] = {|somenewuserdata|};

    In that case getting a new user is easy as:

    exports.get_user = function(id, func) {
    func(users[id] ? null : "NOT_FOUND", users[id] || null);

    }

    I wonder, why you need a callback after all? Its not an IO operation
    with a delay. If not needed due to overall structure or so, it gets
    even simpler:

    exports.get_user = function(id) {
    return users[id] || null;







    }
    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Axel Kittenberger at Mar 12, 2012 at 1:55 pm
    Simply said, you need a callback, when you have to do one. This means,
    you use another IO-related API that has a callback. Or a little more
    elaborate, you need a callback when your code waits for something,
    most times disc or network data, and thus the current flow logic
    stalls until it arrives.

    Putting a callback where one is not yet needed is fine, for mock-up
    where something more complicated is inserted later. To simulate right
    mechanics, it might be a good idea to user process.nextTick(). Its not
    good style, but some calling code gets confused if the callback is
    executed immediately even before the wrapper function returns.
    On Sat, Mar 10, 2012 at 11:32 PM, Angelo Chen wrote:
    This does bring out another topic, when we need a call back? Any
    rule ? In this case the function wii be replaced with a database one
    later, so it might have some IO.

    On Mar 10, 11:49 pm, Axel Kittenberger wrote:
    If your user are not in a particular order, add to this: don't use an
    array if there is no order, but a hash (object).

    users = {};

    e.g. with adding a user by: users[id] = {|somenewuserdata|};

    In that case getting a new user is easy as:

    exports.get_user = function(id, func) {
    func(users[id] ? null : "NOT_FOUND", users[id] || null);

    }

    I wonder, why you need a callback after all? Its not an IO operation
    with a delay. If not needed due to overall structure or so, it gets
    even simpler:

    exports.get_user = function(id) {
    return users[id] || null;







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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedMar 9, '12 at 10:31a
activeMar 12, '12 at 1:55p
posts18
users10
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase