Google's big web apps like Gmail, Maps, and Calendar, use the Closure Compiler to minify and type check their JavaScript. Since my current theory is that type checking makes it faster to develop large software, I figured a good experiment would be to run the Closure compiler on Mitro's JavaScript (a few thousand lines). The summary is that it found real bugs, but they were all in rarely executed error paths, or cross-browser issues that our testing hadn't noticed yet. Perhaps the most valuable part is that when we rearrange JavaScript files, it finds duplicate or missing includes (script tag references). I found the Closure compiler annoying to get working, however now that I understand how to make it work, if I were to start a new, large JavaScript project from scratch, I would try to use it from the beginning. Below is a summary of the bugs it found in our code.
event
variable in event handlers (2 instances). On Chrome, window.event
is automatically defined, so referencing a variable called event
works, but on Firefox this is a bug. Example:
element.addEventListener('click', function() { var data = event.target.getAttribute('data-attribute'); // ... });
var someMagicGlobal = globalInit({option: 'default'}); // ... some other file ... var someMagicGlobal = globalInit({option: 'alternative'});
outerHeight
takes a boolean and returns a value): $('.sidebar').outerHeight(0)
attr
can return undefined. If this happened, our code would have thrown an exception:
var name = $icon.attr('data-icon-text'); var $newDiv = generateIcon(name); // name could be undefined, causing an Exception!
this
. Since the method currently didn't reference this
, it was not currently a bug, but it would have caused difficult debugging if the method was ever modified in the future.