Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: should I keep `assert()`s in production code?

A methodological question:

I'm implementing an API interface to some services, using node.js, mongodb and express.js.

On many (almost all) sites I see code like this:

method(function(err, data) {
  assert.equal(null, err);
});

The question is: should I keep assert statements in my code at production time (at least for 'low significance' errors)? Or, are these just for testing code, and I should better handle all errors each time?

like image 882
MarcoS Avatar asked Oct 12 '15 14:10

MarcoS


People also ask

Should you use assert in production code?

While assert statements help you debug your code and test it internally, they should be used sparingly and must not be relied on to validate data or apply security checks. The reason is simple: for performance reasons, assert statements can be disabled in production.

Should I use assert in Javascript?

assert .) The usual meaning of an assert function is to throw an error if the expression passed into the function is false; this is part of the general concept of assertion checking. Usually assertions (as they're called) are used only in "testing" or "debug" builds and stripped out of production code.

Why we use assert in node JS?

The assert module provides a way of testing expressions. If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated. This module was built to be used internally by Node.

What is assert deepEqual?

The assert. deepEqual() method tests if two objects, and their child objects, are equal, using the == operator. If the two objects are not equal, an assertion failure is being caused, and the program is terminated. To compare the objects using the === operator, use the assert.


1 Answers

You definitively should not keep them in the production environment.

If you google a bit, there are a plethora of alternative approaches to strip out them.

Personally, I'd use the null object pattern by implementing two wrappers in a separate file: the former maps its method directly to the one exported by the module assert, the latter offers empty functions and nothing more.

Thus, at runtime, you can plug in the right one by relying on some global variable previously correctly set, like process.env.mode. Within your files, you'll have only to import the above mentioned module and use it instead of using directly assert.

This way, all around your code you'll never see error-prone stuff like myAssert && myAssert(cond), instead you'll have ever a cleaner and safer myAssert(cond) statement.

It follows a brief example:

// myassert.js
var assert = require('assert');
if('production' === process.env.mode) {
    var nil = function() { };
    module.exports = {
        equal = nil;
        notEqual = nil;
        // all the other functions
    };
} else {
    // a wrapper like that one helps in not polluting the exported object
    module.exports = {
        equal = function(actual, expected, message) {
            assert.equal(actual, expected, message);
        },
        notEqual = function(actual, expected, message) {
            assert.notEqual(actual, expected, message);
        },
        // all the other functions
    }
}


// another_file.js
var assert = require('path_to_myassert/myassert');
// ... your code
assert(true, false);
// ... go on
like image 86
skypjack Avatar answered Oct 02 '22 22:10

skypjack