Skip to main content
Engineering LibreTexts

14.8: Leap of Faith

  • Page ID
    18882
  • Following the flow of execution is one way to read programs, but it can quickly become overwhelming. An alternative is the leap of faith: when you come to a method invocation, instead of following the flow of execution, you assume that the method works correctly and returns the appropriate value.

    In fact, you are already practicing a leap of faith when you use methods in the Java library. When you invoke Math.cos or System.out.println, you don’t examine the implementations of those methods. You just assume that they work properly.

    You should apply the same reasoning to your own methods. For example, in Section 6.5 we wrote a method called isSingleDigit that determines whether a number is between 0 and 9. Once we convince ourselves that this method is correct – by testing and examination of the code – we can use the method without ever looking at the implementation again.

    The same is true of recursive methods. When you get to the recursive call, instead of following the flow of execution you should assume that the recursive invocation works. For example, “Assuming that I can find the factorial of n−1, can I compute the factorial of n?” Yes you can, by multiplying by n.

    Of course, it is strange to assume that the method works correctly when you have not finished writing it, but that’s why it’s called a leap of faith!

    • Was this article helpful?