User Tools

Site Tools


steve_s_programming_tips

Steve's Secret Programming Tips (from the school of hard knocks)

General

  1. If you're not sure whether some code will work or not, or what it will do, try it.
  2. If you get an error message, odds are that the error message points to the problem. Read the message and see if I'm right.
  3. The computer is always right. If your program doesn't work then it's your fault, and you need to find and fix it.

Java

  1. Add debugging print statements to your program, to help you follow what the program is doing
    1. Put the line number of the debug print statement into the message that you're printing so you can easily see where you are in the program, especially helpful if program is really large and there are many debug print statements; and so you can follow along in the source as it runs
    2. include text DEBUG in your output so you can find these statements quickly, as you may want to comment them out at some time
    3. include key variables in the output. Indices if inside a loop. Variables that control or direct program execution
    4. Put these all throughout the program. At every decision point. Inside every loop (the index especially, and any indexed variables)
  2. If you think that an if statement does everything you've indented after the statement, think again. An if statement does only one thing if the condition is true. One. If you want to do more than one thing, then you need to make it one thing by using { }. Note: the if statement isn't the only statement that does this.

Consider the following Java code segment, see how the print statements will help you see what's wrong.

    //someValue and array are previously defined.
    System.out.println("[DEBUG 1234] About to start loop, someValue="+someValue);
    for(int i=0; i<someValue; i--) {
        System.out.println("[DEBUG 1236] In loop, index="+i);
        System.out.println("[DEBUG 1237] array[i] is "+array[i]);
    }
    System.out.println("[DEBUG 1239]");
    
steve_s_programming_tips.txt · Last modified: 2017/03/02 08:44 by smismi7