In Ruby, the traveling distance between the written code and the debugger is quite short. Calling the debugger often times as simple as adding ‘debugger’ to the code, and restarting the interpreter. This keyword can be dropped inside the source pretty much anywhere, and can usher all sorts of useful bits of information to the developer. This close kinship makes delving into murky code or framework internals a bit less scary.

Software engineers are accustomed to a particular cycle of software development. First comes writing code. Then comes running code. Finally comes evaluating code results. Because the ‘debugger’ keyword itself is code, users can stick pretty close to this familiar cycle even when debugging code. And it is only because Ruby is an interpreted language, that such an arrangement is feasible. Were Ruby a compiled language like Java, the effort of recompiling and deploying the instrumented code with a debugger statement insertion, would be much too taxing to appeal to most programmers. In this case, a climb out of that rut of writing, running, and evaluating code would be required. The developer would have to start a debug session manually, insert breakpoints, and step through the lines one-by-one. So much for efficiency and laziness!

Various JavaScript engines also provides a useful hook of a similar nature. Like Ruby programmers, Javascript programmers can bring up the debugger with the debugger keyword. Don’t believe me? Try it out in your browser. Open the JavaScript console, type in ‘debugger’, and hit enter. If everything has worked correctly, you should be teleported into the debugger. Of course, because this is the web, it does not work everywhere. This magic word works for me on Safari and Chrome, but I have yet to get it to work on Firefox. Don’t even mention Internet Explorer!

Because Node uses the same Javascript engine as Chrome, V8, this ‘debugger’ keyword also works in a Node script. We can add debugger keywords to our code, and observe that the program drops into the debugger console on startup. Interestingly, however, it does not work with mocha as-is. With mocha, I have add a debug option. I have to type something like:

mocha debug test.js

instead of:

mocha test.js

Otherwise, Mocha appears to ignore or strip out the ‘debugger’ keyword. Weird!