home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / gutenburg / guten96e / sgcpv22 / HINTS.22A < prev    next >
Text File  |  1996-06-05  |  18KB  |  160 lines

  1.     If you write and run your own program, it probably won't work.
  2.     Your first reaction will be to blame the computer. Don't!
  3.     The probability is 99.99% that the fault is yours. Your program contains an error. An error is called a bug. Your next task is to debug the program, which means get the bugs out.
  4.     Bugs are common; top-notch programmers make errors all the time. If you write a program that works perfectly on the first run and doesn't need debugging, it's called a gold-star program, and means you should have tried writing a harder one instead!
  5.     It's easy to write a program that's almost completely correct, but hard to find the little bug that's fouling it up. Most of the time you spend at the computer will be devoted to debugging.
  6.     Debugging can be fun. Hunting for the bug is like going on a treasure hunt ___ or solving a murder mystery. Pretend you're Sherlock Holmes. Your mission: to find the bug and squish it! When you squish it, have fun: yell out, ``Squish!''
  7.     How can you tell when a roomful of programmers is happy? Answer: when you hear continual cries of ``Squish!''
  8.     To find a bug, use three techniques:
  9. Inspect the program.
  10. Trace the computer's thinking.
  11. Shorten the program.
  12.     Here are the details. . . . 
  13.  
  14.                         Inspect the program
  15.     Take a good, hard look at the program. If you stare hard enough, maybe you'll see the bug.
  16.     Popular typos Usually, the bug will turn out to be just a typing error, a typo. For example. . . . 
  17. Maybe you typed the letter O instead of zero? Zero instead of the letter O?
  18. Typed I instead of 1? Typed 1 instead of I?
  19. Pressed the SHIFT key when you weren't supposed to? Forgot to press it?
  20. Typed an extra letter? Omitted a letter?
  21. Typed a line you thought you hadn't? Omitted a line?
  22.     Fix your strings You must put quotation marks around each string, and a dollar sign after each string variable:
  23. Right:        a$ = "jerk"
  24. Wrong:    a$ = jerk
  25. Wrong:    a = "jerk"
  26.     Too much? Here are three reasons why the computer might print too much:
  27. 1. You forgot the insert the word END or EXIT DO into your program.
  28.  
  29. 2. Into a DO loop or FOR loop, you inserted a PRINT line that should be OUTSIDE the loop.
  30.  
  31. 3. When you started typing the program, you forgot to choose New from the file menu; so the computer is including part of the previous program.
  32.  
  33.                    Trace the computer's thinking
  34.     If you've inspected the program thoroughly and still haven't found the bug, the next step is to trace the computer's thinking. Pretend you're the computer. Do what your program says. Do you find yourself printing the same wrong answers the computer printed? If so, why?
  35.     To help your analysis, make the computer print everything it's thinking while it's running your program. For example, suppose your program uses the variables b, c, and x$. Insert lines such as these into your program:
  36. 10 PRINT "I'm at line 10.  The values are"; b; c; x$
  37. 20 PRINT "I'm at line 20.  The values are ; b; c; x$
  38. Then run the program. Those extra lines tell you what the computer is thinking about b, c, and x$ and also tell you how many times the computer reached lines 10 and 20. For example, if the computer prints what you expect in line 10 but prints strange values in line 20 (or doesn't even get to line 20), you know the bug occurs after line 10 but before line 20.
  39.     Here's a good strategy. Halfway down your program, insert a line that says to print all the values. Then run your program. If the line you inserted prints the correct values, you know the bug lies underneath that line; but if the line prints wrong values (or if the computer never reaches that line), you know the bug lies above that line. In either case, you know which half of your program contains the bug. In that half of the program, insert more lines, until you finally zero in on the line that contains the bug.
  40.  
  41.                                                                                                 Shorten the program
  42.     When all else fails, shorten the program.
  43.     Hunting for a bug in a program is like hunting for a needle in a haystack: the job is easier if the haystack is smaller. So make your program shorter: delete the last half of your program. Then run the shortened version. That way, you'll find out whether the first half of your program is working the way it's supposed to. When you've perfected the first half of your program, tack the second half back on.
  44.     Does your program contain a statement whose meaning you're not completely sure of? Check the meaning by reading a book or asking a friend; or write a tiny experimental program that contains the statement, and see what happens when you run it.
  45.     Hint: before you shorten your program (or write tiny experimental ones), save the original version (by choosing Save from the file menu), even though it contains a bug. After you've played with the shorter versions, retrieve the original (by choosing Open from the file menu) and fix it.
  46.     To write a long, correct program easily, write a short program first and debug it, then add a few more lines and debug them, add a few more lines and debug them, etc. So start with a small program, perfect it, then gradually add perfected extras so you gradually build a perfected masterpiece. If you try to compose a long program all at once ___ instead of building it from perfected pieces ___ you'll have nothing more than a mastermess ___ full of bugs.
  47.     Moral: to build a large masterpiece, start with a small masterpiece. To build a program so big that it's a skyscraper, begin by laying a good foundation; double-check the foundation before you start adding the program's walls and roof.     If the computer can't obey your command, the computer will print an error message. The following error message are the most common. . . . 
  48.  
  49.                            Syntax errors
  50.     If you say ``prind'' instead of ``print'', the computer will say:
  51. Syntax error
  52. That means the computer hasn't the faintest idea of what you're talking about!
  53.     If the computer says you have a syntax error, it's usually because you spelled a word wrong, or forgot a word, or used a word the computer doesn't understand. It can also result from wrong punctuation: check your commas, semicolons, and colons. It can also mean your DATA statement contains a string but your READ statement says to read a number instead; to fix that problem, change the READ statement by putting a dollar sign at the end of the variable's name.
  54.     If you try to say PRINT 5 + 2 but forget to type the 2, the computer will say:
  55. Expected: expression
  56.     If you type a left parenthesis but forget to type the right parenthesis that matches it, the computer will say:
  57. Expected: )
  58.     If you accidentally type extra characters (or an unintelligible word) at the end of the line, the computer will say:
  59. Expected: end-of-statement
  60.  
  61.                           Numeric errors
  62.     If the answer to a calculation is a bigger number than the computer can handle, the computer will say:
  63. Overflow
  64. To help the computer handle bigger numbers, remember to put a decimal point in any problem whose answer might be bigger than 32 thousand.
  65.     If you try to divide by zero, the computer will say:
  66. Division by zero
  67.     If you feed the computer a number that's inappropriate, the computer will say:
  68. Illegal function call
  69. That's what the computer will say if you try saying WIDTH 50 instead of WIDTH 40, or you try saying LPRINT CHR$(1200) instead of LPRINT CHR$(12).
  70.  
  71.                           Printer errors
  72.     If your printer runs out of paper, the computer will say:
  73. Out of paper
  74.     If your printer isn't communicating well with the computer, the computer will say:
  75. Device fault
  76. That means printer's cable to the computer is unplugged or loose or defective or plugged into the wrong socket, or the printer is turned off, or the printer is off-line (because you pressed a button that turned off the printer's ON LINE light), or the paper is jammed, or there's no more ink left, or the printer broke.
  77.  
  78.                                                                                                    Logic errors
  79.     The words DO and LOOP form a pair. If you say DO but no