views:

41

answers:

4

Where I work, all our JavaScript is run through a compiler before it's deployed for production release. One of the things this JavaScript compiler does (beside do things like minify), is look for lines of code that appear like this, and strip them out of the release versions of our JavaScript:

//#debug
alert("this line of code will not make it into the release build")
//#/debug

I haven't look around much but I have yet to see this //#debug directive used in any of our JavaScript.

What is it's possible usefulness? I fail to see why this could ever be a good idea and think #debug directives (whether in a language like C# or JavaScript) are generally a sign of bad programming.

Was that just a waste of time adding the functionality for //#debug or what?

+1  A: 

If you were using a big JavaScript library like YUI that has a logger in it, it could only log debug messages when in debug mode, for performance.

icktoofay
A: 

Since it is a proprietary solution, we can only guess the reasons. A lot of browsers provide a console object to log various types of messages such as debug, error, etc.

You could write a custom console object is always disabled in production mode. However, the log statements will still be present, but just in a disabled state.

By having the source go though a compiler such as yours, these statements can be stripped out which will reduce the byte size of the final output.

Anurag
A: 

Think of it as being equivalent to something like this:

// in a header somewhere...

// debug is off by default unless turned on at compile time
#ifndef DEBUG
#define DEBUG 0
#endif

// in your code...

var response = getSomeData({foo:1, bar:2});

#if DEBUG
console.log(response);
#endif

doStuffWith(response);

This kind of thing is perfectly acceptable in compiled languages, so why not in (preprocessed) javascript?

no
A: 

I think it was useful (perhaps extremely useful) back a few years, and was probably the easiest way for a majority of developers to know what was going on in their JavaScript. That was because IDE's and other tools either weren't mature enough or as widespread in their use.

I work primarily in the Microsoft stack (so I am not as familiar with other environments), but with tools like VS2008/VS2010, Fiddler and IE8's (ugh! - years behind FF) dev tools and FF tools like firebug/hammerhead/yslow/etc., peppering alerts in your JavaScript isn't really necessary anymore for debugging. (There's probably a few instances where it's useful - but not nearly as much now.) Able to step through JavaScript, inspect requests/responses, and modify on the fly really makes debugging alert statements almost obsolete.

So, the //#debug was useful - probably not so much now.

David Hoerster
Would you say it's useful if it was logging with `console.log` instead of `alert`?
Anurag