English subtitles

← Art of Deduction - Software Debugging

Get Embed Code
3 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. Needless to say, Sherlock Holmes would make an excellent program debugger
  2. if he were living these days.
  3. The problem in front of us can be stated as follows:
  4. "Something impossible occurred, and the only solid information is that it did occur.
  5. So we must think backwards from the result to discover the reasons."
  6. Quote from Kernighan and Pike, "The Practice of Programming"
  7. Thinking backwards from the result.
  8. This is the art of deduction applied to debugging.
  9. Let me illustrate this--how this works--using a familiar example.
  10. Here again we have a function to remove HTML markup.
  11. Just as a reminder, what this function is supposed to do is to take HTML input
  12. such as this one: a foo text enclosed into HTML tags,
  13. one to switch on bold rendering and one to switch off bold rendering,
  14. and to turn this into a text in which the HTML markup has been removed.
  15. This makes use of a number of variables:
  16. Tag checks whether we currently are processing a tag.
  17. That is, during these three characters, tag should be true.
  18. And only if we are not in tag mode do we actually add
  19. the characters to the out variable.
  20. And the out variable at the end is being returned.
  21. So while we are processing this code,
  22. tag would be true for these three characters.
  23. Then tag would be false.
  24. F-O-O would be added to the output.
  25. Finally, we would have 4 more tag characters which would not be added.
  26. In the end, what we get is this string: foo.
  27. Now let's practice the art of deduction on this example
  28. simply from the observation that this final assertion
  29. has failed.
  30. So all we know is that indeed this code can produce an output
  31. in which HTML markup is still present.
  32. This is the observation from which we start.
  33. Let's go and think backwards
  34. how the assertion possibly could have failed.
  35. What we know is that out contains an opening tag
  36. that is a less-than character.
  37. The only place where this character could have been added
  38. to the out variable is here.
  39. In order to reach that line, a number of conditions must be fulfilled.
  40. All of these conditions cannot have failed.
  41. Whereas this one must have failed.
  42. Let's focus on the tag variable here.
  43. What was the value of the tag variable
  44. when the less-than sign had been added?
  45. Over to you.