4.6 Debugging

Basic debugging is a skill that every programmer must learn. When your program is malfunctioning and you do not have a clue where the error is located, you can use multiple puts statements to check the value of your variables at different times. Modify the code in Example 4-12 to use the assignment operator (=) instead of the comparison operator (<=) in the first conditional statement, as in Example 4-13.

Example 4-13. Debugging example 1
     1 puts "Enter the customer's age: "
     2 # Get an integer age value from the user
     3 age = gets.to_i
     4 
     5 # Determine the cost based on age
     6 case
     7 # '=' is assignment NOT equality test '=='
     8 when (age = 12) then
     9 	cost = 9
    10 when (age >= 65) then
    11 	cost = 12
    12 else
    13 	cost = 18
    14 end
    15 
    16 # Print out the final cost
    17 puts "Ticket cost: " + cost.to_s

If you run the example, it will not matter what age you input; it will always output the ticket cost as 9. This is because you are not comparing age to the integer 12 but instead assigning 12 to age, which is something that will evaluate to true. Edit the program to match Example 4-14.

Example 4-14. Debugging example 2
     1 puts "Enter the customer's age: "
     2 # Get an integer age value from the user
     3 age = gets.to_i
     4 # DEBUG
     5 puts age
     6 
     7 # Determine the cost based on age
     8 case
     9 # '=' is assignment NOT equality test '=='
    10 when (age = 12) then
    11 	cost = 9
    12 when (age >= 65) then
    13 	cost = 12
    14 else
    15 	cost = 18
    16 end
    17 # DEBUG
    18 puts age
    19 
    20 # Print out the final cost
    21 puts "Ticket cost: " + cost.to_s

By adding puts statements before the case statement and at the end of the program, you are checking what value is stored after it is entered and after the case statement was executed. If you run the new program, you will see that the first output will equal whatever age value you originally entered. However, at the end of the program, the puts will output the value 12, no matter what value was previously entered for age.

From this, you can infer that the value of age changed somewhere between the two puts statements. As you remember, we changed the comparison operator (<=) to the assignment operator (=). The assignment operator = is often mistyped when programmers meant to use the comparison operator ==.

Using puts statements to output key values is one of the simplest yet most effective ways to debug a program.

4.6.1 Alternative Styles of Debugging

Debugging your program includes printing intermediate steps. Further, it is highly advised—or more accurately, required—that you debug your program one portion at a time, rather than the entire program as a whole.

Another approach relies on one or more constants, possibly named DEBUG_MODULE_1, DEBUG_MODULE_2, and so on. In such an approach, these debugging flag constants are defined at the beginning of the program and set initially to true. In each corresponding section of the code, the programmer writes an if statement with the debugging constant as the flag that determines whether a puts statement is executed. Once the particular section of the program being tested is determined to be correct, the constant is set to false. This approach is shown in Example 4-15.

Example 4-15. Debugging example 3
     1 # Flag for debugging (change to false when finished debugging)
     2 DEBUG_MODULE_1 = true
     3 
     4 puts "Enter the customer's age: "
     5 # Get an integer age value from the user
     6 age = gets.to_i
     7 
     8 # Determine the cost based on age
     9 if DEBUG_MODULE_1
    10 	puts age
    11 end
    12 case
    13 # '=' is assignment NOT equality test '=='
    14 when (age = 12) then
    15 	cost = 9
    16 when (age >= 65) then
    17 	cost = 12
    18 else
    19 	cost = 18
    20 end
    21 if DEBUG_MODULE_1
    22 	puts age
    23 end
    24 
    25 # Print out the final cost
    26 puts "Ticket cost: " + cost.to_s

Line 2 defines the constant to be used as our debug flag. Lines 9–11 and 21–23 represent code that should be run only during debug mode to help ensure that the program is running correctly. Compare the output of the code with DEBUG_MODULE_1 set to true and false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.69