Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Ads

Search

Debugging: assertDefined

Ralf Bokelberg just has written about the assert concept
on his blog.
I think this technique is quiet usefull, because you get only
informed if errors happens.
I have written an little function that checks if an object exists,
if not we get an error-message and the player will stop.
If you use the assert-method as argument of a trace-call,
you can simple export without traces and no debugging
code will be published.



trace(String(_global.assertDefined = function (pStrPath) {
   if (eval(pStrPath) == undefined) {
      trace('ERROR: "' + pStrPath + '" is undefined!\n');
      var f = function () { f(); };
      f();
   }
   return '';
}).substr(0,0));

a = {};
a.b = {};
a.b.c = {};
trace(assertDefined('a.b.c')); // Newline in output-window, else happens nothing
trace(assertDefined('a.b.s')); // Output: ERROR: "a.b.s" is undefined!

Posted by hOk at May 3, 2003 03:11 PM

Comments (5)

Hi Holger,

you could also put the definition of the function assertDefined in an include file and simply remove the include, if you are finished with debugging. Defining the function inside trace looks somewhat strange, doesn't it ?

Hav a nice day,
Ralf Bokelberg

Posted by bokel at May 4, 2003 03:09 PM

Yes, you're right, really Strange,
what i am like is to put the calls
into an trace, so you can put much
as you like and dont have to remove
them by hand.
regards, Holger

Posted by at May 4, 2003 05:17 PM

Bokel & Holger, that what I thought of - making special Debug class, define in external file and use #include Debug.as. Before the application is shipped simply remove include statement to Debus.as.

I will start mine Debug.as with your "assert" and "assertObject" functions if you do not mind.

Posted by JabbyPanda at May 7, 2003 12:41 PM

Cool, please drop an info, when you
release that class,
kind of regards, Holger

Posted by at May 7, 2003 05:17 PM

In reply to a comment up there, the problem with includes is that yes you hide the method definitions when publishing, but not the method calls themselves. Therefore when publishing you are still including useless method calls. This is why it is useful to make debug method calls from inside trace.

Posted by basta at May 11, 2003 11:06 PM


Name:


Email Address:


URL:


Comments:



You can use <code>code that should be highlightned</code> to highlight code!
Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise
actionscript-highlightning will be used.

New Place