Re: Need help with my logic



Mark Space wrote:
Patricia Shanahan wrote:
See http://home.earthlink.net/~patricia_shanahan/debug/ for my favorite
approach.


I didn't read all the way through that, so sorry if I missed this, but there's two things I thought I'd mention, since the OP says she couldn't understand the link.

1. Hand Execution

Learn how to execute your programs by hand. This is probably the best thing you can ever learn if you are a programmer.

Print out your program and go through it line by line. Write variable names on a piece of paper as you encounter them, and write their value next to them. When the variable changes value later, cross out the old value and write the new one next to it on the right.

This will solve a lot of problems, and also make you much better about designing programs in the future.

2. Divide and Conquer

Once your programs get very large, you really can't hand execute the whole thing any more. Learn how to divide your program into chunks (modules, classes, methods, subroutines, etc....) Then make sure each chunk is working correctly before putting them all together. This will save you a lot of time when you write (and debug) large programs.

Also see "Unit Testing."


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:

void setDebug(boolean d)
{
debug = d;
}

In each method put at least one debugging statement at the head of a method that accepts information to show the arguments:

if (debug)
System.stderr.println("method(" + variable + ","...")");

and at the tail of a method that returns anything put a debugging statement to show that is returned:

if (debug)
System.stderr.println(retval + " = method()");

If the method is short, a single statement at the end can often be used to handle both the arguments and the returned value. You might also trace the result of particularly significant operations within the method and in the constructor if it does anything significant apart from initializing variables.

Now you can easily trace the execution flow through your program and see results at significant points within methods. You'll find that this approach is *much* faster than stepping through the code with a debugger if the application is of any significant size and - bonus - can be activated in a live environment to diagnose problems caused by unexpected data. I normally use a -d option passed into the main() method to control debugging throughout the program for exactly this reason. The "if (debug) statement;" construct adds almost no runtime overhead.

4. Learn about good program design and development.

Get a copy of Kernighan & Pike's "The Practice of Programming" (Addison Wesley). Its applicable to all modern programming languages, contains examples in Java, C and C++ and is very good on all aspects of programming from design to testing and debugging.

HTH


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
.



Relevant Pages

  • Re: Office or InDesign breaks VS 2005 debugging?
    ... Given tha I was using and debugging the sub-projects in this solution before ... some getting used to) and the debug symbols for the child project are no ...
    (microsoft.public.vsnet.debugging)
  • Re: Cant Debug COM Interface
    ... When I start to debug the application the DLL is ... a com client in vb.net which is referencing the interface of the COM Server ... If I use this application as Host application I can debug without ... For debugging I can start the ...
    (microsoft.public.vsnet.debugging)
  • Re: C++ Workable Mainframe Debuggers
    ... you have to be able to read assembler to do ... IBM Debug Tool for z/OS is available. ... You can license Debug Tool as MLC or, in the form of the Debug Tool ... For graphical debugging use Rational Developer for System z (or ...
    (bit.listserv.ibm-main)
  • Re: Need help on debugging the code in .NET Web application.
    ... > I'm getting the following error while trying to debug the application. ... There are a couple of reasons why debugging fails: ... The IIS mappings are incorrect (very common when IIS installed after ... where Windows directory is normally either Windows or WINNT. ...
    (microsoft.public.dotnet.academic)
  • Re: Insertion Sort on a linked list
    ... Hand execution means you play the computer, and execute the code written out on a piece of paper. ... you'll get better at "reading" code and learning to debug algorithms faster. ... As you get even more skilled, you'll be able to debug programs in your head, with out the paper. ... If I do this for you, I'd get down to the first line of code, where you declare a new variable of type Node called "pointer." ...
    (comp.lang.java.programmer)