Close
Register
Close Window

DSA Coursenotes

Chapter 4 Week 5

Show Source |    | About   «  4.2. Binary Trees Part 3   ::   Contents   ::   4.4. Comparison  »

4.3. Over-Constrained Code

4.3.1. Over-Constrained Code

4.3.1.1. Over-constrained Code (1)

  • Consider the situation where we have two points. We want to know which quadrant the second point (b) is in w.r.t. the first point (a):

    if ((b.x < a.x) && (b.y < a.y))
      doNW();
    else if ((b.x < a.x) && (b.y >= a.y))
      doSW();
    else if ((b.x >= a.x) && (b.y < a.y))
      doNE();
    else if ((b.x >= a.x) && (b.y >= a.y))
      doSE();
    
  • This has the virtue of being quite logical and clear. However, it has some problems.

4.3.1.2. Over-constrained Code (2)

  • It is horribly inefficient (up to 8 comparisons), compared to alternatives.

  • But our real concern has to do with testing and code coverage.

  • Fact: No series of tests will cover all branches in this code.

  • Q: Why?

  • A: Consider every possible branch and see what can get triggered. Consider that there have to be at least 8+ branches, and only 4 possible inputs!!

  • Try to hit every branch by brute force, one at a time…

4.3.1.3. Over-constrained Code (3)

  • Reconsider this code:

    if ((b.x < a.x) && (b.y < a.y))
      doNW();
    else if ((b.x < a.x) && (b.y >= a.y))
      doSW();
    
  • Why would we be in the SW branch?
    • Because (b.x < a.x) and (b.y >= a.y).

    • But if (b.x < a.x), then WE ALREAY KNOW that (b.y >= a.y), or we WOULD HAVE gone NW!!

    • So MT setting this comparison to TRUE can’t break any test, that HAS to happen anyway.

4.3.1.4. Over-constrained Code (4)

  • Q: If we want complete code coverage when there are only four logically distinct inputs, then we had better do what?

  • A: Come up with code that has only four branches!

4.3.1.5. Over-constrained Code (5)

  • Refactored code:

    if (b.x < a.x)
      if (b.y < a.y)
        doNW();
      else
        doSW();
    else
      if (b.y < a.y)
        doNE();
      else
        doSE();
    
  • Not only can you test every branch, but this is a lot more efficient! Every branch requires 2 tests!

   «  4.2. Binary Trees Part 3   ::   Contents   ::   4.4. Comparison  »

nsf
Close Window