TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

When I do TDD and when I don’t

71 pointsby adrianomartinsover 3 years ago

22 comments

thatswrong0over 3 years ago
Agree with the reasoning here, so as a result I almost never use TDD: I pretty much always have to spike + iterate in order to come to a reasonable and clean solution for whatever I&#x27;m working on.. no matter how much I&#x27;ve tried to spec things out exactly how they should work, it always ends up being different. I&#x27;m also just not great at abstract vs. hands on thinking.<p>On top of this, I&#x27;ve also come to find unit tests to generally require more work and be less useful for identifying regressions than integration tests for the work I do. Maybe this is just a result of the kind of work I do though.
评论 #30377967 未加载
评论 #30377783 未加载
评论 #30378804 未加载
orobinsonover 3 years ago
I find TDD is great when you have an interface to some code defined and are changing the behaviour of what happens when interacting with that interface. You can then add some tests to the existing test suite for the interface, watch them fail, then get to work implementing and be content that you’re done when all the tests are passing.<p>However, if you’re still developing the interface definition itself, TDD just wastes time when it transpires the interface you envisaged and wrote tests for doesn’t quite match up with the implementation as you write it.
评论 #30378516 未加载
评论 #30377388 未加载
DerArztover 3 years ago
I will recommend folks take the time to go and read the O.G. book on TDD[1] to really get a feel for what it was intended to be originally and form their opinion from there. Most of the time when I see people talk about TDD they are either cargo cultists or uninformed people.<p>[1] <a href="https:&#x2F;&#x2F;www.goodreads.com&#x2F;book&#x2F;show&#x2F;387190.Test_Driven_Development" rel="nofollow">https:&#x2F;&#x2F;www.goodreads.com&#x2F;book&#x2F;show&#x2F;387190.Test_Driven_Devel...</a>
评论 #30378889 未加载
评论 #30378389 未加载
Jenkover 3 years ago
I find TDD helps me mind-map out my implementations. It&#x27;s a bit hand-wavy, but generally I&#x27;ll use TDD when things are complex and I need to clear my head of the bigger picture in favour of focussing on the smaller picture (I.e., lean on the the &quot;next interesting test&quot; mantra) to let the tests lead me through the path.<p>Will I use TDD for basic things? No. I&#x27;ll very probably write tests for them though just to provide regression coverage.
sdoeringover 3 years ago
I am not a programmer. And I am by far not good enough in any language to spontaneously think architectural enough to create unit tests and do TDD.<p>I always feel like I should be doing more tests. That this is the correct way to do things.<p>Being a data&#x2F;web analyst I have to say that I once created extensive tests for a specific part of custom javascript logic setup by a former agency of my client in the tag management solution they used.<p>I even had to mock basic functionality from the tag manager (Adobe Launch) to make it work locally.<p>When I took over it was a mess of code that mapped url parameters to their specific marketing channel logic (you could have done this purely in Adobe Analytics, though - but I was never able to find a way to explain the unnecessary complexity of the implemented solution).<p>In the end I had created around 60 test cases to ensure this fragile bit was in a way that enabled refactoring. It worked.<p>Especially when around one year later, a colleague of mine who had taken over the client needed to change stuff in there, had it break and asked me. I thought his solution should work, but the tests told a different story. Within half an hour we had it nailed down, fixed and running flawlessly.<p>If I ever have such a complex piece of logic anywhere I will surely learn how to write tests in the respective language of choice. Until then I will happily fatfingered stuff on my own amateur projects, though.
kevinskiiover 3 years ago
No matter how carefully and thoroughly we design a system up front, we almost discover more elegant solutions once we start writing the actual code. I suspect this is true for nearly all non-trivial systems. If we were to fully implement every unit test up front we would usually end up wasting a lot of time.
评论 #30377925 未加载
erdoover 3 years ago
I do TDD in one specific case, and it&#x27;s always a unit test. Very occasionally I will need to write a function or a small class do some complicated logic that I&#x27;m too lazy or stupid to work out how to do. I do know exactly what the results should look like though, including all the possible edge cases.<p>( I think the last time I did this was for a point of sale terminal I was working on, which needed a solution similar to the change making problem <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Change-making_problem" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Change-making_problem</a> )<p>Anyway, for those situations, I write a large number of tests cases, covering every reasonable scenario, plus a bunch of unreasonable scenarios.<p>Then I write a half-assed implementation that fails on several tests, and I keep hacking about until more of the tests pass. Once they all pass, I stop. Even if at that point I have no idea why that particular version of the code completely works.<p>It&#x27;s nasty I know, but sometimes it&#x27;s the quickest way to a robust implementation
LouisSayersover 3 years ago
Working on my own project as a solo dev, I hardly ever write tests.<p>What I&#x27;ve learnt over the years is that tests lock your code in making it hard to adapt (you could say making you less agile).<p>When I&#x27;m smashing out new code and I haven&#x27;t established patterns then it doesn&#x27;t make sense to lock that new code in.<p>Once I do have a pattern going then I might add tests, but it&#x27;s not for the sake of test coverage or to gloat that I have tests, it&#x27;s purely because I actually care about making sure those bits of the code work as intended and that the edge cases are covered.<p>For these tests I do indeed do TDD.<p>Otherwise it&#x27;s a trade-off - would I rather be closer to a feature people can use or would I rather be slogging away writing and maintaining tests.<p>This being said, I will say that when I come to a point of adding more people to the team tests will become much more important.<p>The way I see it though, not having tests and having people complaining about it means that I&#x27;ve done something right.
cjvirtucioover 3 years ago
I find tests more helpful when the implementation is stable. It&#x27;s a little tedious to re-write test cases as unknowns come up.
bobobob420over 3 years ago
Just write the code and then write the test. Or just write the test and then write the code. Or think about the test when writing the code. Or think about the code when writing the test. It doesn&#x27;t really matter right. The thought process is going to occur anyways. Focusing on resuable patterns, consistency, and creating reliable and easy to read code is 10x more important than the TDD drivel that is out there. Honestly, honestly, The only reason we discuss this crap is because there are people who make off running around the country giving talks and writing books on all things developers (nothing wrong with that).
Anon6747463over 3 years ago
TDD is climbing with ropes; it&#x27;s useful in situations where you don&#x27;t know how to get all the way to the top but want to try stuff from where you&#x27;ve gotten to without falling all the way to the bottom.
评论 #30378571 未加载
jbverschoorover 3 years ago
I think people confuse TDD with just writing tests
GuB-42over 3 years ago
TDD always felt wrong to me. And in fact, a lots of my worst code was written with a TDD-like approach.<p>The basic idea is: read the specs, write the tests, then write the code that passes the tests. The big problem here is that you add a level of indirection, and something may be lost in translation. That is, if the tests are wrong, then your code will be wrong.<p>Since you already have the tests when you start coding, there is a tendency to forget the specs and code the tests, since your goal now is to have &quot;all green&quot;. And because if you did it well, it is very easy to test your code, it is tempting to just tweak it until it passes, without you understanding what you are doing. I know because that&#x27;s the process that lead me to the first mentioned terrible code.<p>For a simple analogy, TDD would be like giving students the test subject before they start studying. Sure, they will get good grades, but don&#x27;t count on them studying what is not on the test, and some will simply memorize the answers. That&#x27;s why at school, you almost never have the test in advance.<p>Obviously, I am not against testing, especially automated unit testing, but the core idea of TDD, that is writing the test first, is, I think, a bad idea. The often cited argument is that because you start with a failing test (because you haven&#x27;t implemented the feature yet), you are guaranteed to actually test something, but I find it to be of limited value, except maybe on very large projets where not implementing a feature at all is something that can happen accidentally.<p>An approach that I think works better is to write the code, write the tests, pass the tests, intentionally break your code, check that the tests fail, revert your changes. Code and tests can be written at the same time, ideally by different people, but if you do thing in order, I would go code first.<p>In fact, I think the only real &quot;advantage&quot; of TDD is that it simply forces you to write tests. It is an anti-corner-cutting technique.
cgrealyover 3 years ago
The main value in TDD is not when you write the initial implementation. Sure, it can help, but most devs are competent enough to implement a function&#x2F;class&#x2F;whatever and have it do what the programmer thinks it will do.<p>The value from TDD is documenting those assumptions. &quot;The db will always be connected before this class is used&quot;, &quot;This function assumes the user is authenticated&quot; and so on.<p>Then in 6 months, a year or even 5, when you or someone else comes back to this code, you can be reasonably certain changes to it will break tests, because the tests tell you how the REST of the codebase is using this.<p>Of course, that assumes a decent shelf life for the code. If you can be reasonably certain you&#x27;re going to handover this and never touch it again, don&#x27;t bother with the tests. And delete the repo while you&#x27;re at it.... (not comfortable with that, are we? :D )
评论 #30378241 未加载
rileymat2over 3 years ago
One thing I do not see mentioned is that tests let me try out the interfaces I am building as a consumer of them earlier than I would otherwise. So even in exploration it can be useful, sometimes.
markmaglanaover 3 years ago
I tend to go by how much anxiety a piece of code might give me once it goes to production. If I expect the level of anxiety to breach a certain threshold (usually when there’s too many logic branching than I can keep in my head), then I write the tests first.<p>The above assumes though that I’ve done enough previous discovery to have a relatively robust model of the problem domain. If not, I prototype first and during this stage I won’t be using TDD. Prototypes produced are throwaway, of course.
somewhereoutthover 3 years ago
Either testing finds the bugs, or the user finds the bugs.<p>If the user is a machine, use a machine to test the code. If the user is a human, use a human.<p>The user does not know or care about unit tests.<p>The state space of any non-trival algorithm is far greater than can be sensibly enumerated.<p>Software is design - designing something inside out makes no sense.
strife25over 3 years ago
I&#x27;ve had the most luck w&#x2F; TDD for designing APIs.<p>When I write the tests first, it gives me a feel for how consumers of the API will use it. It&#x27;s a great method to understand the UX of the re-usable code.
评论 #30378783 未加载
评论 #30378581 未加载
amriksohataover 3 years ago
Any surveys done worldwide as to how many people do TDD, how many just wrap in tests afterwards, and how many don&#x27;t unit test at all ? At different N tier levels?
picturover 3 years ago
TDD is something I have never tried before. but since I like to prepare a certain checklist first and develop on it, I guess I can.
bvirbover 3 years ago
Is TDD really about never figuring out unknowns using untested code? What about using diagrams? Or white boarding?<p>Test first design makes a lot of sense, but when you’re unsure about the design using code to figure it out seems just as reasonable as any other method.<p>If not what’s left? Do everything waterfall?
评论 #30378540 未加载
johnklosover 3 years ago
You know, not everyone thinks TDD has to do with development:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Telecommunications_device_for_the_deaf" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Telecommunications_device_for_...</a>
评论 #30378438 未加载
评论 #30377735 未加载