Thursday, October 12, 2006

ObjectUtil - Stop debugging the old way

As far as I know, not enough people take advantage of the ObjectUtil class. Everyone, from novice to expert should know about it, because it is extremely useful.

The function that I use the most is the "toString" method. The "toString" method "pretty-prints the specified Object into a String. All properties will be in alpha ordering".

I've noticed that a lot of people debug their applications by doing trace statements like:
trace( myObject.toString() );
trace( myObject.myAttribute.toString() );
trace( myObject.myProperty.toString() );
trace( myObject.myArray.toString() );
Wouldn't it be much easier if you could do that with one statement? Well, you can. Use the ObjectUtil.toString() method to get a string representation of your object including extended properties.
trace( ObjectUtil.toString( myObject ) );
Now, lets look at a real world example. In this demo I use a http service to get the latest RSS feed from MXNA. Then I compare the output of a toString statement and an ObjectUtil.toString based on the http service. Take a look at the output and you can decide for yourself which one works for you.



Launch Application
View Source
Download Source

Word of caution: ObjectUtil does not work in all scenarios. For instance: you will get an exception if you do this
trace( ObjectUtil.toString(mx.core.Application.application) );

Questions or comments, you can contact me at andrew.trice( at )cynergysystems.com.

0 Comments:

Post a Comment

<< Home