|
Page DebugDarling.
May/june 2007 de&&bug( "there");"That's my 'Live comments' pattern for efficient tracing." -- JeanHuguesRobert
Hi! Trace messages are my number one debugging tool. I believe it is often a good idea to keep some of the trace messages that have been so useful during debugging, because chances are that they will be needed again sometimes later. But keeping these messages is intrusive and sometimes inefficient. Unless... Unless a reasonably efficient & transparent method is available. That is what de&&bug aims at. How it worksde&&bug is a javascript syntactic construct that you can use to optionnaly issue trace messages at runtime. You should use it like a comment, but a live comment in a way, a comment about what is actually happening, not a comment about why some source code is written in a certain way : // We plan for this xxx because of that xxx de&&bug( "and we get here because zzz happened"); The construction is made of 3 parts:
The small trick is about operator &&, the "progressive And" operator. The right operand is evaluated only if the left one is true. i.e. if de is false, bug() is not called. It is a very fast solution because de is a boolean variable and therefore it costs very little to detect if tracing is turned off. It costs much less than would cost a test done inside some debug( msg) function, because that later method implies the evaluation and passing of the msg parameter even if that parameter will actually not be used at all when tracing is disabled. In large programs I often end up using many categories of trace messages and usually only a very small subset of all the possible messages is turned on at any time. To implement that usage with de&&bug, I usually declare and initialize a de local variable at the very beginning of my functions. I initialize it to true or false depending on some global mechanism that specify what trace messages are needed. Note: Please don't confuse debug trace messages with other type of messages like warnings or error messages, because these messages don't target the same audience. Debug messages are really for software developers only. Messages for other audiences require their own mechanism and that is something very interesting too but beyond the subject of this article about debugging.
// With Firebug on Firefox I have a console
var de = true_or_false, bug = console.log;
// **Oops**, that does not work,
// it raises an access violation to some weird Function.__paren__.
// Use this instead:
var de = true_or_false;
var bug = function(){ console.log.apply( this,arguments); };
// Or do the poor man's alert()
var de = true_or_false, bug = alert;
See also the 'lite' cross-browser version of Firebug http://www.getfirebug.com/lite.html Ruby $de = true_of_false; def bug( *args ) p args end C int de = true_or_false; #define bug( x ) (printf( "%s", x),0); Python, Perl, PHP, Java, others ??? On some other subjectsBrea(pointsIf you need a break...point, see http://trimpath.com/blog/?p=24
breakpoint( function(expr){return eval(expr);},
"convertList() currCounter is " + currCounter);
Hence, here is my special of the day solution: brea(point());
brea = eval;
point = function ( msg ){
brea.msg = msg;
return "breakpoint( function ( expr ) { return eval( expr); }, brea.msg);";
}
Then you can do this: brea(point( "convertList() currCounter is " + currCounter)); Poin(erspointers in Javascript!Tricky, convulated, unorthodox, evil (and untested):
/* Example use case */
var l = {v:""};
var lvp = Poin(er( "l.v"));
l.v = "Hello";
alert( lvp()); => Hello
lvp._( "World");
alert( l.v); // => World
/* Implementation */
Poin = eval;
er = function ( lv ){
return [
"var poin = function (){ return ",
lv,
"}; poin._ = function( x ){ ",
lv,
" = x }; return poin;"
].join();
}
I first did something similar (cleaner) in RubyLanguage. Li(eLife or Lie with the evil Eval
|