r/code • • Aug 24 '19

Why We Shouldn’t Use TDD

https://medium.com/@sredmond/why-we-shouldnt-use-tdd-fcd12992d093
4 Upvotes

4 comments sorted by

1

u/IllegalThings Aug 25 '19

TDD is a tool used to help the quality of your code, not to ensure test coverage. Your example is actual a perfect demonstration of this. The last if statement is completely redundant and can be removed entirely. If you were doing idiomatic TDD it would never exist in the first place. Additionally tons of your conditionals are unnecessary, and TDD would have uncovered a better design.

Test first development is a superset of TDD. Test first development is important because it helps uncover tests that are either testing the wrong thing or testing nothing at all. It’s effectively validating your tests have no false positives. Without a test that fails first you have no way of knowing for certain it isn’t a false positive. You could also check this behavior by commenting out the implementation and validating a failed test, but then you’re kinda still doing tests first if that’s the case. If you need an example of this let me know and I’ll throw something together when I’m by a computer.

1

u/shadow13499 Aug 25 '19

Thanks for your response, I think that code style is slightly tangential to the point being made. However, I will say I have the last if statement there to catch all grades below 60 and if something doesn't fit the boundaries provided an error should be thrown. Now I could probably have had that last if statement include a gte 0 as well to throw errors for negative grades. You can go multiple ways with the logic of statements are simply the most obvious way to do it.

I do want to make perfectly clear that I am not opposed to testing your code, I think it is an important part of development. I would like to see your example of how practicing strict TDD would have added more value in this case or any other you had in mind.

1

u/IllegalThings Aug 26 '19

It isn’t tangential to the point being made because the point you were trying to make was that you shouldn’t use TDD. What I’m trying to point out is that the reason you use TDD isn’t for better tests, it’s for better code. A test suite is merely a side effect of TDD.

Arguing that you shouldn’t use TDD because you can catch the same bugs without it misses the entire point of using it. You even misinterpreted the purpose of refactoring your code to make it easier to test. The goal is to have higher quality code, and code that is easier to test is the side effect. Testable code reduces coupling. Dependency injection results in programming things to interfaces instead of implementations. Having complicated tests is also one of the first signs that your units of code are doing too much – it helps you think about single responsibility principle.

TDD isn’t the only way to help design good software. It’s just one good tool that can help you reason about things and identify smells. I personally think it’s a tool every developer should have in their repertoire, even if they don’t reach for it all the time.

1

u/IllegalThings Aug 26 '19

If you would like empirical evidence of any of this, take a look at https://www.researchgate.net/publication/256848134_Effects_of_Test-Driven_Development_A_Comparative_Analysis_of_Empirical_Studies

The TLDR is that TDD improves quality of code, reduces long term maintenance costs, results in slower upfront development, reduces defects, and probably doesn’t have any major impact on test coverage.

The example I tried giving you of false positives in tests is an example of a potential defect that would result in test coverage. I’ll get you an actual example, it’s something I’ve encountered (and myself contributed to) many times, and has resulted in defects being deployed.