Hello everyone, I have a question about LEARN TDD WITH MOCHA, specifically regarding the Edge Case. In this section, I had already ensured the test of an empty array passing by using the reduce method (setting the initial value in reduce to 0, like this: return inputArray.reduce((sum, value) => { return sum + value; }, 0);
). Given that we have this method, why would we need to use an if statement for control? Is there something else I’m not understanding here, or is it an unnecessary part?
it’s an uneccessary part. I also got a little confused because my first attempt used reduce and not if. However, I was able to figure out that the lesson just expected us to try an if statement first so that then we can go through a refactor stage. Two important take-aways from the lesson are:
- Using reduce is more efficient than if for this example
- Refactoring is a heavily important part of TDD. The whole TDD principles rely on quick, heuristical approaches that work and are fine-tuned for optimality through separate refactoring steps.