« March 2012 | Main | May 2012 »

April 24, 2012

Underscore Mixin for Error Handling

A common pattern that pops up in my node.js programming is this:

// callback is defined elsewhere
object.someAsyncFunction(param1, function(err, response) {
    if (err) {
        return callback(err);
    }
    // do something with response
    callback(undefined, transformedResponse);
});

When I'm really paranoid about ensuring the callback is called I wrap the code in a try/catch:

// callback is defined elsewhere
object.someAsyncFunction(param1, function(err, response) {
    if (err) {
        return callback(err);
    }
    try {
        // do something with response
        callback(undefined, transformedResponse);
    } catch(err) {
        callback(err);
    }
});

Talk about a lot of boilerplate code. Since I use underscore heavily throughout my code I created the following mixin:

_.mixin({
    errTryWrap: function(fn, callback, context) {
        return function() {
            // check for err
            if (arguments[0]) {
                return callback(arguments[0]);
            }
            try {
                // strip the err from the arguments passed down
                return fn.apply(context, _.rest(arguments));
            } catch(err) {
                callback(err);
            }
        };
    }
});

With that in place the paranoid version now becomes:

// callback is defined elsewhere
object.someAsyncFunction(param1, _.errTryWrap(function(response) {
    // do something with response
    callback(undefined, transformedResponse);
}, callback));

Much cleaner. I'm not crazy about the name of the mixin but it makes it easy to find in the code should I come up with a better name later.

Tags: nodejs

April 23, 2012

JSCoverage Report for Jasmine

Wanted to announce the first release of a Jasmine reporter that we've been using in combination with JSCoverage and jasmine-node to capture code coverage metrics and produce an Emma style coverage report. The output of this get consumed by our Jenkins build and gives us nice coverage trends and detailed information for each build. As this is the first public release it is a little rough around the edges but is getting the job done. If anyone wants to check it out you can find it at:

npm install jscoverage-reporter

https://github.com/NeoPhi/jscoverage-reporter

Tags: jasmine jscoverage nodejs

April 16, 2012

The Shallows

The Shallows: What the Internet is Doing to Our BrainsThe Shallows: What the Internet is Doing to Our Brains by Nicholas G. Carr

My rating: 3 of 5 stars


The books covers many topics related to reading, writing, learning, and memory as it has been influenced by the growth of the Internet. While there are studies mentioned throughout the book many of the observations felt more derived from anecdotal evidence. While worthy of a read I don't feel the author fully supported his claim.



View all my reviews

My notes:

  • The medium influences us more than content on it [3]
  • Our brains are not fixed [26]
  • Born with templates that are shaped by experience [28
  • Thinking through an action repeatedly is like doing it [33]
  • Can wire bad habits [34]
  • Map and clock are intellectual technologies [44]
  • Technology advances mark turning points in history [48]
  • Oral cultures are more sensuous but harder to advance [57]
  • Spaces and punctuation are introduced in 13th century [62]
  • Writing moved from dictation to author [65]
  • Silent reading [67]
  • Books increased content and accuracy [72]
  • Can't remove importance of book and written word from society [77]
  • TV time is unchanged despite increased Internet time [87]
  • Electronic readers may make us more likely to read, but in different ways [104]
  • Web turns media into social media [106]
  • Lack of final version in books changes author's approach [107]
  • Online activities about recognition and acknowledgment [118]
  • Linear reading leads to higher comprehension [128]
  • People most skim web pages [136]
  • Personal digital system exacerbated information overload [170]
  • External memory allows us to use capacity for other goals [180]
  • Short-term memory is synapse change, long-term memory is anatomical [185]
  • Tools "numbs" our sense while extending it [210]
  • Unhelpful software if learned led to learning more [216]
  • Moral decisions take time [221]

Tags: books