YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← 03-28 Infeasible Code

03-28 Infeasible Code

Get Embed Code
1 Language

Showing Revision 2 created 07/16/2014 by Udacity Robot.

  1. Now, I'm going to talk about the question--What does it mean when we have code
  2. that doesn't get covered--for example, if we're using statement coverage,
  3. what happens when we have some statements that we haven't been able to cover?
  4. There're basically 3 possibilities. The first possibility is that the uncovered code was simply infeasible.
  5. An infeasible code means exactly what it sounds like.
  6. It's a code that can't be executed under any circumstances.
  7. Let's say that we're writing the check Rep function for a balance tree-data structure
  8. At some point in the check Rep, we're going to assert that the tree is balanced
  9. then in the implementation of the balanced method,
  10. we're going to check if the left subtree and right subtree have the same height,
  11. and if not, we're going to return false.
  12. If the tree becomes unbalance, we're going to return false causing an assertion evaluation.
  13. Assuming that the code is not bugged and assuming that we're testing a correct tree,
  14. we'll never going to be able to return false from a balanced function.
  15. And of course, a coverage tool is going to tell us,
  16. we failed to achieve code coverage for this particular statement in the code.
  17. We have to ask ourselves, is that a bad thing?
  18. Is that bad that we failed to execute this line of code,
  19. and of course, it's not bad because that code only can execute if we made a mistake somewhere.
  20. So, the proper response to this kind of situation is,
  21. we just need to tell our coverage tool that we believe this line to be infeasible
  22. and then the tool won't count this line against us when we're measuring code coverage,
  23. and so let's look an example of this--we have here is an open source AVL tree.
  24. An AVL tree is somewhat analogous to the splay tree that we already looked at.
  25. in the sense that it's a self-balancing binary search tree.
  26. As you can see, there's quite a lot of code here.
  27. AVL tree has a reputation for being fairly complicated as these things go.
  28. The code comes with its own test suite so let's run it under the coverage monitor.
  29. Okay. Now, let's look at the output.
  30. You could see here that out of the 389 statements in the AVL tree we failed to run 61 of them
  31. and 20 branches were partially executed.
  32. So, you can see that there're some various minor failures of the test,
  33. which exercise interesting behaviors.
  34. So, here is the AVL tree standard recheck function and what it does is check a bunch of properties
  35. of the AVL tree and if any of them fail, raises an exception.
  36. Every statement here that raises an exception has failed to have been covered,
  37. and then the branches which test the condition,
  38. which leads to raising the exception only partially covered.
  39. So superficially, we haven't gotten very good coverage of this function,
  40. but actually, of course, what we hope is that this AVL tree code is correct
  41. and these are truly infeasible.
  42. And if we really believe that, what we can start doing is go ahead and start telling the coverage tool
  43. to ignore this and the way we do that is to go back to the source code
  44. and left some of this with a comment that has a special form pragma, no cover.
  45. And then we go ahead do this for the rest of these and we should see that it does sort of thing.
  46. All right. So, now we run the coverage tool again and see if things look a little bit better.
  47. Okay. Good. We'll, it's starting to get all of this code.
  48. But we can see now that the coverage tool has marked these in sort of a light gray color
  49. indicating that it's ignoring the fact that they weren't coverage because we told it to.