Wednesday, July 14, 2010

Debug easier by naming your closures

I was profiling some code when I noticed that several routines were listed as "(?)()" in Firebug, and "JScript anonymous function" in MS's Visual Studio. This proved frustrating, as recognizing my closures were critical to evaluating my code - I couldn't tell which were performing well, let alone executing.

It was burdensome, but I named all the anonymous functions. Hopefully today's JavaScript optimizers and minimizers know enough to remove such useless text (otherwise, I'll have to), but it sure helped my debugging effort.

So, if your code has lines like these:
obj.critical = function () {};
useCriticalFunc(function () {}); 
You might try naming the anonymous functions, like so:
obj.critical = function criticalMethod() {};
useCriticalFunc(function criticalFunc() {});
Now, you should be able to recognize the closures, in your debugger of choice!


No comments:

Post a Comment