7 points · by sandGorgon · 9y ago · 13 comments
I'm wondering if TDD is even a good idea ? Can one get to a stage where writing testcases is as easy as writing specs or whiteboarding ?
Or does TDD become one of "those processes that founders shoved down our throat" ?
There was a very interesting discussion about this a few years back on HN - https://news.ycombinator.com/item?id=2240595
P.S. A lot of responses I get are "whatever works for your team". Fair enough. But I'm looking for fairly opinionated answers - how do you do it ? And what would you do better ?
what's your mental model around writing tests ? Do you write tests after every function, every commit , every day ? When you are building a new product (and maybe hacking stuff together in the middle of the night), would you still write tests before moving forward ?
Your question is "should I use this testing technique?". A better question to start with is "what am I trying to achieve with testing?" Once you know your goal you can find a relevant testing technique to achieve it. Your goals will likely be different for different parts of your product and codebase.
Notice this means different organizations and products (or same product at different times in its lifecycle) will have different forms of testing, because they have different goals and resources.
To help with the decision process of choosing testing goals I've come up with a model, of different goals and the kinds of testing that are useful for each. I'm going to be giving this as a talk at PyCon in May, but have an essay version written up: https://codewithoutrules.com/2017/03/26/why-how-test-softwar...
Since choice of testing strategy is situation-specific it's very hard to give a general answer without understanding the specifics - ping me (itamar at codewithoutrules dot com) if you'd like to talk over your specific situation.
Then I might write a whole bunch of related code, fleshing out other parts which rely on the primary part (that I know works well enough for now), perhaps without writing tests first. But if I do that, I will then go back and write more tests until I get 100% coverage later. Undoubtedly you will find corner cases that you didn't cater for when you exercise the code in tests.
It's not done until all my code has 100% coverage, and all the main entry points, important data structs, tricky bits, etc, have documentation of some kind. But I might implement a fair chunk of the code before implementing full tests.
So: some tests up-front (because it makes coding easier), and the remainder of the tests (for full coverage) have higher priority than writing documentation on my list of todos.
In no particular order, I've written non-trivial amounts of Go, Javascript, C#, Python, Java, C/C++, ...
— But your question is not specific enough because the process can vary a lot, depending on what you're coding (e.g. I don't bother writing tests for bash scripts: they usually either work, or they don't), and who / what it is for. e.g. the above process works great if you're building a library (and if you're not building a library, it can help to try and break apart the project into some library-like pieces).
Obviously the above would be somewhat different if I were doing TDD for a web-app, but I'd still apply the same process to the bits and pieces that make up the app behind the scenes - and then have to also consider front-end testing if appropriate.
But TDD approach or writing test cases after the implementation is upto the individual developer/team's preference. Initially TDD approach would be very difficult for freshers or devs who don't have TDD experience, but in the long run, it would help.
Good luck.
The tricky thing about TDD is that Uncle Bob packages it up with morals and ethics and craftsmanship. It's probably a big reason why he is a successful consultant in addition to having coding chops...and I don't disagree with his moral and ethical opinions but they are about writing code rather than running a business and here the important decision is running a business and that's what Markham emphasizes and emphasizes particularly well in his reply to Cooper's response.
I'd put it this way, the first order benefit of TDD is confidence that refactoring will pass fewer regressions to production. But a successful startup does not depend on refactorings because growth drives rearchitecting and pivoting and those mean that many/much/all existing tests may become irrelevant. Riffing a bit, it might be that in a startup the way to determine the benefit of tests is do they get the company to the point where those tests become irrelevant more quickly? Most startups default on technical debt by either running out of money or by throwing out the code base because they acquire different operating parameters due to scale.
In a startup environment your goal is often understanding users (does anyone care about this product/feature? is it usable?) followed by correctness. And as you say, stability isn't an immediate concern if you're rewriting everything every two weeks, which is the case for many layers of code.
"I'd put it this way, the first order benefit of TDD is confidence that refactoring will pass fewer regressions to production." - I'd say automated tests, including unit tests and therefore TDD, only give you stable functionality. Not correctness, since only a human can determine that (see essay for fleshed out argument - https://codewithoutrules.com/2017/03/26/why-how-test-softwar..., section on "What means of testing can you use?").
But unit tests (and thus TDD) for code that isn't going to be a stable is actually a negative, because you're trying to enforce stability on something that keeps changing.
--
Could you link to the Cooper/Markham discussion?
https://blog.ometer.com/2016/05/04/professional-corner-cutti... has a better take than just "quality all the time" that TDD advocates sometimes take on.