Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Let’s look at an example of the design benefits that TDD affords. To demonstrate them, we need a problem that isn’t so trivial as to be a throwaway, but not so complex that we get caught up in the details. A perfect candidate (pun intended) is a perfect number finder. A perfect number is a number whose factors (minus the number itself) add up to the number. For example, 6 is a perfect number because the factors of 6 (1, 2, 3, and 6) minus the 6 add up to 6. Let’s write some code in Java to find perfect numbers.
The following code was written without TDD, just by applying simple logic and some minor mathematical optimizations: