This time I feel like sharing my (maybe) strange looking console.log() strategy, but I find it very effective, read on to learn why.

If I can’t debug, for whatever reason and have to fall back to console.log()s you normally see my code looking like this:

console.log("i = ", i);
    if (i==0){
        // Do something here
    } else {
        // Do else

You can see two things here:

  • the first line prints out a variable’s value, simple I guess
  • all console.log()s are not indented at all

Let me touch on the “not indenting” first.
I don’t like to indent console.log()s because this just makes the code look ugly, and that is intentional, because console.log() normally just doesn’t belong in the code. By making the code look ugly I am quickly forced to remove them again, the latest I will stumble on it before committing, because I never commit without looking at the diff.
If I ever forget to remove the console.log() I can be sure that a coworker will be annoyed by it and hopefully either ping me or remove it. But I am trying my best not to forget it :).

Komodo macro

Back to the first console.log() you see in the code example up there, that one is actually one keystroke away for me, I have hacked a simple macro in my editor (Komodo Edit) that takes the text I selected and creates a console.log() in the line below throwing out this variable and it’s value.
This little script looks like this:

var s = ko.views.manager.currentView.scimoz;
var t = s.selText;

// Only if something was selected put it there.
t = t ? "'"+t+" = ', "+t+"" : "arguments";
s.insertText(s.currentPos, "console.log("+t+");");

// Set the cursor inside the "()" at the end, so we can add
// parameters

Just create a new macro with it and attach a key-combination to it and logging a variable is just one key stroke away.