Re: Need help with my logic



Martin Gregorie wrote:
3. Diagnostic tracing:

Add a boolean debug variable to your classes to control diagnostic output. Set it either through the constructor or with a separate method:

This is a very good point, and everything Martin says here is true.

This technique is so important, that most languages support it directly. Both C and Java support the assert statement which does the if..then... part for you. (In C, it's a macro, not a statement.)

In Java, the assert statement can be controlled from the command line just as Martin describes. Combine this with the divide-and-conquer strategy, and you can turn assertion on and off in differ parts (chunks) of your program. This allows you to only see asserts from parts of your program, and the other parts run at full speed. (Because the compiler optimizes assert away when it's disabled.)

The disadvantage of assert is that it's less flexible than an if..then... so you'll sometimes still have to use them as Martin describes.

I do disagree with Martin on one point: using a debugger. My preferred method is to use different boolean debug variables for different parts of my program. (Divide and conquer, again.) Then, instead of setting the debug variables on the command line, I set them with the debugger. This allows me to change what areas I want to see output from, with out having to stop and restart the program. This is much, much faster. And it's much faster than stepping and tracing too, which should only be used when you've narrowed a problem down to a very small section of code with the boolean debug variables, and you need a lot of fine detail to find the root cause.


4. Learn about good program design and development.

Get a copy of Kernighan & Pike's "The Practice of Programming" (Addison

I've never heard of this before, I'll have to check it out. Thanks!

M
.



Relevant Pages

  • Re: Final Evening of Analog TV?
    ... that's not a case to make personal attacks to go ... What is does is merely assert wrongness (whether ... Matthew L. Martin has so often been incorrect about such assertions that I ...
    (alt.tv.tech.hdtv)
  • Re: October 13, 2006 - Seattle, Washington - set list
    ... martin@xxxxxxxxxxxxxxxxxxxxxx ... wrote: <J Buck wrote: All someone does in that position is endlessly ... assert Dylan's right to do what he wants, and argue against any negative ...
    (rec.music.dylan)
  • Re: printing line numbers for debugging purpose
    ... "Philippe C. Martin" writes: ... > want to assert and have the program exit). ... > Is it possible to print the current source file name/line number? ...
    (comp.lang.python)