So what’s the first lesson of a JS Ninja? Prepare yourself. And prepare your environement. Which means: Arming with testing and debugging.

Debugging

The command I type most often: console.log(). Is it maybe even THE most often typed command in the coder’s world? Wouldn’t surprise me.

Browsers in versions any reasonable person uses have debugging consoles integrated, so the console.log() call is super easy to use and allows interacting with any content, on any site, search for elements, check out properties, access data. Piece of cake.

But then the Ninja brings in other things and I start to get more uncomfortable already. Setup breakpoints in your scripts for debugging. Hm, well I don’t know how to code breakpoints technically. So I read. The book seems to tell me to add a line of code with log('xyz') and I understand, that that should be the breakpoint in the script. I run it and get an error that log is not defined. Hm. Is the book outdated? Am I running into trouble because I am using an envirnoment I set up with Yeoman?

YEO man

Yeah.. I know, the overhead is most likely making things far too complicated, but I recently stumbled over Yeoman and it’s quite easy to set up and a site with it sure looks prettier than anything I could have set up quick and dirty.

But … back to breakpoints … I am not THAT much of a beginner. I know that breakpoints can be set in the browser’s console directly. So I decide to not care about the log() undefined error for now and just continue to read on.

And then, I realize for the 1000st time how stupid I am, that the log('whatever') was not meant to be a breakpoint but just an example line of code where Ninja then shows the breakpoint functionality in the browser’s console. Man oh man. Do I delete this stupidity from this post? No. I don’t. I decide to bore you with all, well, let’s agree on most, my misunderstandings as well as the dropping coin moments.

Testing

Repeatability, simplicity, independence, the Ninja says. Far too early on my apprentice path I learned how important testing is for any software development. That was when I started to learn some Rails with Michael Hartl’s tutorial. I am thankful I went through with that dry topic, it helped me later on. Unfortunately that doesn’t mean I can and do write fancy tests today. (But Ninja says I am not the only one: 48% of all JS developers apparently do not write tests … and as my frontend dev colleague Jezen does write tests, I guess I am all set. Thanks mate. Well, no, I know, I will have to learn it properly.)

I have learned testing in Ninja style today though, as you might have noticed in the screenshot above: using assertions.
The red and green lines of text there have css classes added, depending on the returned value of assertions, red if false, green if true. So. I do know how to write tests now. A bit.

function assert(value, desc) {
  var li = document.createElement("li");
  li.className = value ? "pass" : "fail";
  li.appendChild(document.createTextNode(desc));
  document.getElementById('results').appendChild(li);
}
assert(true, 'Great, the test suite is running.');
assert(false, 'Fail!');