8 ms·
What TDD is good for
- recursivedoubts 2y agoagree w/ the classifications of greefield, brownfield & maintenance i would also add regressions as a place where TDD is excellent: reproduce the regression with a failing test, then fix it it is exasperating that TDD advocates will often not admit that there are any tradeoffs or contexts in which it is more or less appropriate, and I think it does a lot of damage to the reputation of the technique (which can be effective in some contexts) as consultants snake-oil
- vitus 2y ago> reproduce the regression with a failing test, then fix it Not just regressions, but bugs in general. If you can reproduce your bug in a test, then verify that your fix causes your test to pass, that's a great way to build confidence before trying out your code in production. And in fact, this is mentioned in the article as part of maintenance: > Writing a test first to prove out a bug before you fix it is pretty neat.
- herpdyderp 2y agoIt's also a great way to codify "hey we found this edge case once" and make sure it doesn't happen again.
- litenboll 2y agoThat's the only type of test I know for sure has value, which makes them easy to write. At work I feel a certain pressure to test more or less everything, but personally I much prefer a "common sense"-approach: Writing tests is motivated by either bugs found (including any issues you run into during the initial implementation) and/or complex operations that are easy to get wrong and could need the extra documentation that tests provide. I dispise aiming for 100% code completion, so much of that is just idealism and wasteful.
- ncann 2y agoThe only thing that should be said about TDD is that it's incredibly polarizing, one of the most polarizing things in software development in fact. It's incredibly personal, some swear by it, some loathe it. Thus, every TDD introduction should have a big disclaimer up front that hey, maybe this isn't for you and that's totally fine. Just don't be a zealot about it.
- ikiris 2y agoIt’s basically the handwashing for doctors argument over again. Lots of people really hate the idea they could be doing anything wrong and absolutely hate being inconvenienced for something they don’t personally see as necessary no matter how beneficial it might be overall.
- yowlingcat 2y agoI think it's lazy and superstitious to make an analogy between practices in hygiene in medicine and software engineering. But if you want to draw your analogy to its absurd conclusion, I think a lot of people would hate the idea of needing to wash their hands to do a surgery remote controlled by a joystick, because the sterility of the instrument doing the operation is independent of the sterility of the hands remote controlling it from afar.
- mnsc 2y agoDoctors found out that washing their hands caused less people to die, and it's a bit of a nuisance but totally worth it. Software devs have not found out that anyone dies unless we do strict tdd. But we have found out that unless we have a good amount of automated tests, the software becomes brittle and hard to change. TDD is more of a style that some swear by that has alleged properties making everything better, like the way doctors wash hands in a peculiar way. But just "washing the hands" thoroughly would probably also help people not dying.
- ikiris 2y agoTherac-25
- thewanderer1983 2y ago[flagged]
- th0ma5 2y agoBillable hours?
- 0xbadcafebee 2y agoThis is good advice. But I am particularly bugged by the idea that, as you continue the "exploratory" phase of a large greenfield project, there has to be a point where you start writing (and rewriting) tests. Because no matter how great you think you are, once you reach a certain amount of stuff, it becomes much easier to break other stuff without realizing it. Even if you think your code is quite cohesive and modular, with well-defined interfaces and such. At some point you spend a lot of time fixing broken stuff, while simultaneously just trying to get the thing to work. There is a break-even point where you will be wasting time on something (either writing tests, or fixing broken stuff, or both). I have tried just writing end-to-end tests. I have tried writing unit tests after the fact. I have tried writing unit tests before the fact (TDD?). They all lead to problems. I would like to lean towards a new model, that I would call "contractor-driven engineering" (CDD). The idea is to engineer software systems like a contractor builds a building. First you get a dumb person, who has the basic skills of a trade (so, your average software developer today). But then you bolster their naivete and inexperience with known quantities and an approved architectural design. The end result is a person with a basic level of skill, that can still be relied upon to create a reliable system. This requires two things first, though: 1) The "known quantities" would be your well-defined, pre-produced, quantifiable engineering parts. Like how a 10d and 16d nail are built to a specification, so that when you architect a building frame, you know that the 10d nail will work in this one part, and the 16d nail will work in this other part, and you can be assured the building will not fall down under a strong wind. We do not have known quantities built to an engineering specification in the software world. We have software "things" that "an experienced person" says "should work", but if we built skyscrapers like that, everything'd be fucking falling over all the time (the way it falls over in the software world all the time, but nobody minds, until all of Delta's flights around the world get cancelled....). 2) The "approved architectural design" - we need people who are actually trained and certified as architects, to dictate the design to the software development team. I think in my 20-year career I have seen perhaps one team actually build software the right way (and by "the right way" I mean "not falling into obvious traps that anyone with enough experience has seen and would avoid"). That architectural training needs to involve the codification of a lifetime of engineering discipline (that is available, if you ask us old fucks for advice), combined with the "known quantities" above, so that designs actually meet stated goals and requirements. This, by the way, completely jives with the idea of "changing software all the time", because you can just ask the architect to look at the change and tell you if it'll work or not. Why would I think this is better than TDD? Because TDD still assumes the developer is going to simply test their way out of bad decisions. You can't do that. But what you can do, is abstract away the decisions that are hard, and leave the work that is predictable and not hard.
- et1337 2y agoI’ve only ever heard of TDD in the context of greenfield development. I thought the whole point of TDD as opposed to just “writing a lot of tests” is that you write the tests first, and that more or less determines the design of the system? TDD looks at tests not just as a cost center that pays off on the future, but as a tool to help you design stuff right now. This is my perception as a non-TDD person. Maybe someone can validate if this makes sense or not.
- sebtron 2y agoI too find it hard to apply TDD to the big existing project I am working on. Most of my day-to-day coding is adding small bits here and there in the right place, discovering the architecture of the whole thing as I go. Without knowing what I have to do to achieve the desired result in the first place, I cannot begin writing a test with the half dozen mocks it needs (I know it's bad). The only tasks where I can succesfully apply TDD are bug fixing, where I write a new test to prove that I understood the bug (the test should fail with existing code), before I fix it. But I have colleagues (working in other projects) who told me how they are succesfully applying TDD to refactor an old codebase. They start with big, not-so-"Unit" tests to make sure that they don't break the thing at any step, and then they move to more specialized tests when tackling specific parts of the code.
- motorest 2y ago> Most of my day-to-day coding is adding small bits here and there in the right place, discovering the architecture of the whole thing as I go. Without knowing what I have to do to achieve the desired result in the first place, I cannot begin writing a test with the half dozen mocks it needs (I know it's bad). Nothing in your example prohibits or even hinders TDD. The only thing required by TDD is that you specify an interface and add tests to specify what behavior you expect from them. The moment you have something you can compile and run, your TDD path is completely open. Also, I'm noting the hints of the same specious reasoning that plagues discussions on microservices, mainly the "it's-all-or-nothing" fallacy. You can benefit from TDD if you, say, are writing a RESTful interface and start off by only adding unit tests checking for controller behavior. If your project can live with 0% line coverage, it can certainly live with TDD buying you 20% line coverage.
- __MatrixMan__ 2y ago> Everything you think you know will change in the exploratory phase of a new project as you code, and you’ll strain your sanity by rewriting tests over and over again. This seems to assume that rewriting your tests is slower than rewriting your implementation which has no tests. But if you keep things bite sized, and the logging you collect from tests is good enough, it's often faster to change a test or a type and have an LLM rewrite the relevant part of the implementation. Also, I think we have this bias where we get so excited about the implementation that's blossoming in our mind that we forget to check really important stuff before writing that implementation. A bias towards rationalism and away from empiricism, or perhaps towards our own aesthetic leanings and away from those of our peers. I like writing a few tests first (even before the types or frameworks they're made of even exist) because it puts me in the caller's mindset. With those few tests to guide me, I can then let myself have a lot of fun building something with the callee's mindset, and tests will hold me back when I've gotten too creative for the caller's needs. If I try to just build it without those rails, I end up with something really interesting and not very useful.
- MortyWaves 2y agoI think unit testing, property based testing, TDD, and the like are valuable and I can’t imagine doing development without them, I consider it good engineering. In fact I always make sure to write good tests even for interview take home tests etc. I’m providing this background because I’m still fairly wound up by a terrible interviewing experience I had with the only company so far that specifically mentioned TDD and made a big deal about it. I’ve never experienced such a farcical technical test. It was an hour, and the interviewer (only one, red flag already as it allows for bias) spent twenty minutes debating the name of a single test and kept circling back to the topic when I tried moving past it. Kept making a big deal about “TDD test names”. Refused to let me write anything. Refused to elaborate on any requirements or even what he really wanted me to do. After the hour was up I’d written one working test and one broken test. The actual system under test was a one line method, because he spent another fifteen minutes getting angry that I wanted to even run the test. Absolutely ridiculous and it’s unfortunately made me very wary of anywhere that makes a big deal about TDD despite being a big proponent of testing myself. So yeah, if that’s people’s exposure to TDD I’m not surprised some people don’t like it. If you’re ever interviewing for a UK based company that works in the arts and theatre booking space that mentions TDD, run a mile.
- ninetyninenine 2y agoNot sure why you didn’t name the company.
- MortyWaves 2y agoDidn’t want to get the wrath of the mods etc. I’d have to check my email to find the name.
- MortyWaves 2y agohttps://www.spektrix.com/en-gb/ https://www.spektrix.com/en-gb/ Spektrix
- deleted 2y ago[deleted]
- grakker 2y agoI've found that if I'm in a fight with someone with a better ground game than I have, my TDD really is important.
- jim-jim-jim 2y agoAny fad historians here? I got into the game late, but my impression is that TDD peaked when untyped interpreted languages like Ruby and JS were en vogue. A lot of the maintenance/brownfield benefits the author cites can also come from the compiler itself if your language has branded types, ADTs, non-empty collections, and whatever else. I feel like you shouldn't bother testing what you can already prove. None of this is esoteric. It can be spun up in TS, but mass culture is lagging behind technology.
- bazoom42 2y agoStatic typing is great for other reasons, but it doesn’t replace any tests. If you write a square root function, static typing can just ensure that it always returns a number which doesn’t tell you much. Unit tests can ensure it returns the correct number at least for some inputs. This is a lot more useful for ensuring correctness. The counter argument is that tests donsnt ensure the function always returns a number. Maybe it returns a date or a string for some particular input which is not covered by the test? While this is true, in my experience this is not a common problem.
- mmcromp 2y agostatic typing doesn't cover all (or most) testing but it absolutely replaces type testing. webdev isn't square root functions it's working with complex data APIs. In fact, the argument was about ruby/rails not needing types/interfaces was because "the tests are the interface" that's how baked in tdd was
- kazinator 2y agoIf I have a dynamically typed add function and I test that add(2, 2) produces 4, then the type testing is rolled right into it trivially. 4 has a type. If an object comes out of add(2, 2) that isn't an integer, then it won't match 4. Oftentimes, type is just a less specific match. It would be a waste of time to write a separate test case that add(2, 2) produces an integer, rather than just testing that it produces 4. I've hardly ever seen Lisp test cases that look only at the type of an output, rather than a specific value. Even a function whose job is to operate on types actually has inputs and outputs that are values representing types; e.g. the symbol string representing string types or whatever. However, there are tests which show that a function signals an error when given wrong types. Those would not be necessary in a static language; the function called with wrong types would not compile, and it goes without saying that all tests must compile, unless we are testing the compiler.
- fs_software 2y agoI don't practice TDD often but I've found that TDD can be particularly useful in rare cases when there is a long feedback loop for manually testing the logic I'm working on. In these situations, TDD is perfect for quickly validating a code change.
- matsemann 2y agoI also don't practice TDD directly, but I think trying to for a while made me a better developer. It gave me a better feeling for what a "unit" is, how to structure things etc that I feel is applicable even if not doing TDD.
- rvz 2y agoGreat article cutting through the cultishness and mistakes many experienced engineers make when using TDD on everything to the point where it becomes nonsensical and it is instead used religiously. It bring lots of benefits into large scale refactoring / maintenance in existing projects and totally agree with its less usefulness in most greenfield projects, with the exception of extremely safety critical systems that invest lots of money into the project. Otherwise without the above, you would not attempt to consider using it on a new project with a very hard release deadline attached to it.
- lowbloodsugar 2y agoRe: greenfield development. I approach this in two stages. First, write code that works regardless of its quality. Then go and rewrite the important or fussy bits using TDD where possible.
- shermantanktop 2y ago> Here’s a rule of thumb I follow. If I comment out a line of code in your module (assuming it still compiles and runs), and ALL your tests still pass, something’s wrong. So they rigorously test all those logging calls?
- plorkyeran 2y agoIt's certainly low on the list of priorities to test, but yeah, testing all your logging calls is useful. It's how you avoid having something break in production in a non-reproducible way, checking your logs, and discovering that you've been logging your language's equivalent of [object Object] ever since someone refactored that code last year.
- shermantanktop 2y agoI once refactored a server-side codebase due to poor performance, and a significant time suck was - I kid you not - this line (in pseudocode): logger.info(“Request:” + JSON.stringify(httpRequest)); Terrible! I removed it, deployed it, and…alarms went off. It turned out that the line in question was being used to populate a critical metrics feed. In that terrible, terrible case, I agree that the load-bearing log line should be tested. In any other scenario I’d personally avoid it, outside of mission critical medical or other codebases. But that’s me, I understand the impulse to test it.
- nunez 2y agoI agree with the author that TDD can become problematic for greenfield projects. I've spun my own wheels many times on tests for new things; it is very easy to spend more time on tests than on implementation. HOWEVER, IMO, having a set of behavioral tests (BDD) and a roadmap in the README helps a lot in staying on track. It's the best of both worlds: you don't get stuck in the quagmire of writing tests for things that don't exist yet, but you have just enough tests to keep yourself honest.
- eikenberry 2y agoI've always found TDD to be best when used as a learning tool. Not learning how to write better tests, but in learning to write better code. Looser coupling, better designed APIs and signatures were some key areas where I found improvement along with finding that having tests as I wrote meant that I could use a rapid-prototyping style for every part of the code, iterating on the code+tests together so I run it whenever I want with whatever inputs I want in an easily repeatable way. TLDR; TDD makes you a better programmer, even if you don't use it long term.
- kazinator 2y agoTDD teaches people who aren't good at writing tests about the need for falsiability: that the test doesn't pass anyway without the code change. That goes out the window after refactoring though. Tests created before refactoring continue to pass, but they are connected to changes that no longer exist and therefore could not be backed out to show that the tests fail. (Not without rolling back the refactoring). Or, tests that were connected to independent changes now cannot be easily made to fail independently. Refactoring tied them together. Thus they are redundant.