Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I keep console statements in JavaScript application?

Tags:

javascript

I am using console.log() and console.dir() statements in 3 places in my 780 lines of code JS script. But all of them are useful for debugging and discovering problems that might appear when using the application.

I have a function that prints internal application's state i.e current value of variables:

printData: function () {
        var props = {
            operation: this.operation,
            operand: this.operand,
            operandStr: this.operandStr,
            memory: this.memory,
            result: this.result,
            digitsField: this.digitsField,
            dgField: this.dgField,
            operationField: this.operationField,
            opField: this.opField
        };
        console.dir(props);
    }

I also have a list of immutable "contants" which are hidden with closure but I can print them with accessor method called list(); in console. Something like this:

list: function () {
    var index = 0,
    newStr = "",
    constant = '';

    for (constant in constants) {
        if (constants.hasOwnProperty(constant)) {
            index = constant.indexOf('_');
            newStr = constant.substr(index + 1);
            console.log(newStr + ": " + constants[constant]);
         }
    }
}

The third place where I use console in debugging purposes is in my init(); function, where I print exception error if it happens.

init: function (config) {
    try {
        this.memoryLabelField = global.getElementById(MEMORY_LABEL);
        this.digitsField = global.getElementById(DIGITS_FIELD);
        this.digitsField.value = '0';
        this.operationField = global.getElementById(OPERATION_FIELD);
        this.operationField.value = '';
        return this;
    } catch (error) {
        console.log(error.message);
        return error.message;
    }
}

As my question states, should I keep these console statements in production code?

But they come very useful for later maintenance of the code.

Let me know your thoughts.

like image 586
Vlad Avatar asked Sep 29 '22 21:09

Vlad


1 Answers

Since these are not persistent logs you won't get much benefit out of them. Also it runs on every individuals machine since everyone has their own copy of the program. If you really need it, it would be better to have a variable that can toggle this feature. Espcially if you are targetting to debug a whole lot of pre-determined stuffs.

Client Side issues needs to be debugged slightly different from Server Side. Everyone has their own copy of the program. Browser-JS is run on client side you open the browser and all code that you wrote is with you in a full blown repl and debugging is easy compared to Server side where most likely you wouldn't have access to that system. It is so flexible that you could just override it when you are checking something in production right from your own browser without affecting anyone. Just override it with those console statements and debug the issue.

Its a good idea to have logs in Server side programming. It gives a lot of useful information and is persistent.

like image 109
Nishant Avatar answered Oct 05 '22 07:10

Nishant