Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Source] The Debug Class!
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
the_short1




PostPosted: Wed Jun 28, 2006 11:47 am   Post subject: [Source] The Debug Class!

Just thought i would post something usefull that people could include in their programs. I created it for use with my Pegs FP contest submission.

The Debug Class!
-usage: "Debug.out (what you want);"
-pass "ToggleDebug" to turn on / off, default is on
-if debug is off when you pass something, it stores it into array
-next time you turn debuging on, it will output all the contents from the array
-bug proof so far, and it handles "null" and "" without problems.
-if debug is off, and youve acumalated 400 entries, it will ditch oldest 100 to save from having excessively huge array at startup, to prevent overflow, as well the dos window has a 300 line max in winXP
-you can pass String, int, float, double, and boolean direct, any other format, pass (""+otherformat)

EXAMPLE:

Debug.out ("HELLO1"); // outputs normally cuz debug is on
Debug.out ("ToggleDebug"); // turn it off
Debug.out ("HELLO2"); // this isnt outputed to screen right away
Debug.out ("HELLO3"); // this isnt outputed to screen right away
System.out.println ("intermediate"); // proves its outputed before hello 2&3
Debug.out ("ToggleDebug"); // turn debugging back on

---- this is what gets outputed ---

HELLO1 // first line when debuging was off
ToggleDebug // tells you debuging was toggled (off in this case)
intermediate // notice hello 2 and 3 werent outputed yet!
ToggleDebug // debuging was switched, output anything stored in array
HELLO2 // output from array
HELLO3 // output from array

Check out TestDebug for more examples..

Hope it is of use to some of you, and Debuging is one of the most usefull tools while programming, it allows you to track down errors and glitches in your program very easily. I include at least 1 debug statement per method, and for most GUI actions (eg clicking a button)

Any questions, feel free to post em (but i will be out of town for next few days)
-Kevin



Debug.java
 Description:
The Debug Class! Store in same folder as your program, it will compile first time you use it.

Download
 Filename:  Debug.java
 Filesize:  3.36 KB
 Downloaded:  253 Time(s)


TestDebug.java
 Description:
(you dont need this), just example of it in action

Download
 Filename:  TestDebug.java
 Filesize:  841 Bytes
 Downloaded:  225 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Jun 28, 2006 6:23 pm   Post subject: (No subject)

My first impression:

Put your comments above the line they refer to, and make sure no line exceeds 80 lines. This is a mess to look at and try to comprehend. Nevertheless, I shall try, so I can make constructive remarks.

Next thing I notice:

Your conditionals. Sometimes you use braces, and sometimes none. Choose a consistent style, since it makes disambiguation much easier.

Structure your code such that you're not just using static methods and fields exclusively. Have the user of the code create and work with instances of the Debug class. Which reminds me... if you do that... choose a noun name for your class.

Having to send a special string to toggle the value of a field seems very hackish. Wouldn't it be better to just have a method which does that?

[quote]// we limit to last 300 entries because dos has 300 line max[/code]

You're using a language which can run on many platforms. It should not be crippled just because Windows is.

You might add a PrintWriter field to your class, so that output can be directed anywhere, rather than just standard out.

Don't use the adding to an empty string hack to convert primtive types to strings. Use something like:

code:
Integer.toString(num)
the_short1




PostPosted: Wed Jun 28, 2006 8:12 pm   Post subject: (No subject)

Me = [in brackets]

wtd wrote:
My first impression:

Put your comments above the line they refer to, and make sure no line exceeds 80 lines. This is a mess to look at and try to comprehend. Nevertheless, I shall try, so I can make constructive remarks.
[I used to do mine like that but i found it made the code very long...]

Next thing I notice:

Your conditionals. Sometimes you use braces, and sometimes none. Choose a consistent style, since it makes disambiguation much easier.
[See point 1]

Structure your code such that you're not just using static methods and fields exclusively. Have the user of the code create and work with instances of the Debug class. Which reminds me... if you do that... choose a noun name for your class.
[I purposely did that to make it easier and more available to beginers who dont know about creating objects of classes Wink, although keeping static objects to a minimum is better (which I normally try to do)]

Having to send a special string to toggle the value of a field seems very hackish. Wouldn't it be better to just have a method which does that?
[Now that is a good idea, i can't believe i didnt think of that]

// we limit to last 300 entries because dos has 300 line max[/code]
You're using a language which can run on many platforms. It should not be crippled just because Windows is.
[Well, Windows is the most common OS, I used that OS, and i wanted to give some explaination for why I did that]

You might add a PrintWriter field to your class, so that output can be directed anywhere, rather than just standard out.
[Will do if I think of it next time that code gets opened, thx, and maybe a method to email programmer with debugging record]

Don't use the adding to an empty string hack to convert primtive types to strings. Use something like:
code:
Integer.toString(num)

[Bah, whats the fun in that Wink... maybe if i have time... this code originated as a method from my Pegs game which had only string support, i later added more and ability for other types so i could post it here (rather rushed) because i thought it would be useful to some, after stubling upon the project for the Turing debug library project from 2003]
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: