23.4. Psychological Considerations in Debugging

 < Free Open Study > 

Debugging is as intellectually demanding as any other software-development activity. Your ego tells you that your code is good and doesn't have a defect even when you've seen that it has one. You have to think precisely forming hypotheses, collecting data, analyzing hypotheses, and methodically rejecting them with a formality that's unnatural to many people. If you're both building code and debugging it, you have to switch quickly between the fluid, creative thinking that goes with design and the rigidly critical thinking that goes with debugging. As you read your code, you have to battle the code's familiarity and guard against seeing what you expect to see.

Further Reading

For an excellent discussion of psychological issues in debugging, as well as many other areas of software development, see The Psychology of Computer Programming (Weinberg 1998).


How "Psychological Set" Contributes to Debugging Blindness

When you see a token in a program that says Num, what do you see? Do you see a misspelling of the word "Numb"? Or do you see the abbreviation for "Number"? Most likely, you see the abbreviation for "Number." This is the phenomenon of "psychological set" seeing what you expect to see. What does this sign say?



In this classic puzzle, people often see only one "the." People see what they expect to see. Consider the following:

  • Students learning while loops often expect a loop to be continuously evaluated; that is, they expect the loop to terminate as soon as the while condition becomes false, rather than only at the top or bottom (Curtis et al. 1986). They expect a while loop to act as "while" does in natural language.

  • A programmer who unintentionally used both the variable SYSTSTS and the variable SYSSTSTS thought he was using a single variable. He didn't discover the problem until the program had been run hundreds of times and a book was written containing the erroneous results (Weinberg 1998).

  • A programmer looking at code like this code:

    if ( x < y )    swap = x;    x = y;    y = swap;

    sometimes sees code like this code:

    if ( x < y ) {    swap = x;    x = y;    y = swap; }

People expect a new phenomenon to resemble similar phenomena they've seen before. They expect a new control construct to work the same as old constructs; programming-langauge while statements to work the same as real-life "while" statements; and variable names to be the same as they've been before. You see what you expect to see and thus overlook differences, like the misspelling of the word "language" in the previous sentence.

What does psychological set have to do with debugging? First, it speaks to the importance of good programming practices. Good formatting, commenting, variable names, routine names, and other elements of programming style help structure the programming background so that likely defects appear as variations and stand out.

The second impact of psychological set is in selecting parts of the program to examine when an error is found. Research has shown that the programmers who debug most effectively mentally slice away parts of the program that aren't relevant during debugging (Basili, Selby, and Hutchens 1986). In general, the practice allows excellent programmers to narrow their search fields and find defects more quickly. Sometimes, however, the part of the program that contains the defect is mistakenly sliced away. You spend time scouring a section of code for a defect, and you ignore the section that contains the defect. You took a wrong turn at the fork in the road and need to back up before you can go forward again. Some of the suggestions in Section 23.2's discussion of tips for finding defects are designed to overcome this "debugging blindness."

How "Psychological Distance" Can Help

Psychological distance can be defined as the ease with which two items can be differentiated. If you are looking at a long list of words and have been told that they're all about ducks, you could easily mistake "Queck" for "Quack" because the two words look similar. The psychological distance between the words is small. You would be much less likely to mistake "Tuack" for "Quack" even though the difference is only one letter again. "Tuack" is less like "Quack" than "Queck" is because the first letter in a word is more prominent than the one in the middle.

Cross-Reference

For details on creating variable names that won't be confusing, see Section 11.7, "Kinds of Names to Avoid."


Table 23-1 lists examples of psychological distances between variable names:

Table 23-1. Examples of Psychological Distance Between Variable Names

First Variable

Second Variable

Psychological Distance

stoppt

stcppt

Almost invisible

shiftrn

shiftrm

Almost none

dcount

bcount

Small

claims1

claims2

Small

product

sum

Large


As you debug, be ready for the problems caused by insufficient psychological distance between similar variable names and between similar routine names. As you construct code, choose names with large differences so that you avoid the problem.

 < Free Open Study > 


Code Complete
Code Complete: A Practical Handbook of Software Construction, Second Edition
ISBN: 0735619670
EAN: 2147483647
Year: 2003
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net