Live data from Hacker News

Ask HN: Seriously, how do you TDD?

news.ycombinator.com

61–70 of 86 posts

Re: Ask HN: Seriously, how do you TDD?

#61
I've found that strict write-the-tests-first TDD doesn't work for me for most problems. The exception is things like "parse specific components out of this string" - really straight-forward this input should result in this output code, where sometimes I'll write the tests first.

What DOES work for me is having a rule that any implementation should be accompanied by tests that prove the implementation works, bundled in the same commit.

I've been doing this habitually for a few years now and it has had a dramatic positive impact on my productivity.

I wrote a bit about how I do this here: https://simonwillison.net/2020/Feb/11/cheating-at-unit-tests...

Re: Ask HN: Seriously, how do you TDD?

#64
A lot of people giving their opinion about TDD, but not many actually answering your question.

The answer depends on your approach. You can go outside-in (top-down) or inside-out (bottom-up). The one you use is a matter of taste. A lot of people find outside-in easier, but inside-out can be a bit faster if you have a good sense of how your system will fit together.

To take an outside-in approach, start at the top level of your application. Possible just under the UI, because UIs can be hard to test. (If the UI isn’t tested, make it do as little work as possible, and have it just forward to the tested code to do real work.)

Test-drive a module, class, or method in that top layer. The smaller the better. If there’s something complicated that should be handled by another module/class/function/method, make the module/function/etc., but just hardcode one answer. Make a note of the things you hardcode.

When you’ve finished TDDing your first thing, look at your notes and choose something that you hardcoded. TDD that in the same way. Repeat recursively until the application works. Focus on getting to a working but incomplete application as soon as possible, by working depth-first instead of breadth-first.

Once you have a working but incomplete application—a “walking skeleton”—think of a feature you’re missing and add it in the same way. Continue until the application is done. Remember to look for opportunities to improve the design and refactor as you go.

Inside-out is very similar, except that you do the depth-first recursion in your head, mentally, and don’t write any code until you get to the leaves of the recursion tree. You test-drive the leaf, then test-drive its caller, etc., until you get to the top. It takes more experience, more thinking about design during TDD, and more willingness to refactor.

I have videos on my site if you’d like more. Start with this series:

https://www.jamesshore.com/v2/projects/lunch-and-learn

Re: Ask HN: Seriously, how do you TDD?

#65

I am not trolling today, TDD doesn't make any sense to me, testing to fail and correcting it until it passes doesn't work with my flow. In a DAW(Digital Audio Workstation), this is like adjusting the knobs, adding effects when you haven't even added any samples, it just doesn't make any sense, stop forcing yourself to do things that doesn't align with your flow, you'll likely get annoyed for no reason. The way I work…

Is there a specific tool you use to figure out your design graph, or is it more a 'mind' thing? I know there's some vscode plugins to kinda visualize how code fits together, etc.

It's more of like doing system designing (pre-planning), the system design stage is way of looking at the bigger picture by connecting the dots, and yh, it could be a mind thing but I mostly use my pen and paper or if it's something that's really difficult to nail, I use the C4 modeling (you can use whiteboard, paper or any C4 modeling tool).

C4 modeling would give you the abstract design or system architecture graph, you don't have to do all 4 levels diagram, just do the one that makes sense to you.

If you are doing something simple, you might not need to do any of that but as soon as you are building something of scale with optimal security not doing system designing is like building a skyscraper with no design, well, you know what might go wrong :)

Re: Ask HN: Seriously, how do you TDD?

#66
post #38

Earlier quoted context omitted.

In my experience tests also slow refactoring down , despite the common mantra claiming the opposite. Want to extract some logic into a new interface or introduce a new parameter to a method? Well, now instead of just doing that, you will also have to update all tests dealing with said logic. At my previous job I spent much more time updating tests testing trivial shit than actually writing useful code.

Exactly my experience. Numerous times I found ways to reduce accidental complexity in software. But guess what, my change breaks 50 tests, because accidental complexity happened to be under test. What I anticipated to be an enjoyable 2-hour refactoring turns into 3-day chore. I guess I'll just leave the complexity in there. Sure, those were "bad" tests. Accidental complexity is not supposed to be tested. And yet I ha…

Tests make refactoring slower, because you might have to also update the tests. On the other hand, in a complex enough code base, refactoring is impossible without sufficient test coverage. You can rely on the tests to catch edge cases that you weren't aware of in areas of products built on top of architecture you are maintaining.

Sometimes, all 50 of those stupidly over-complex tests that people have copy-pasted around are garbage that get in the way of your refactor, but sometimes one of them is protecting some important functional requirement that you didn't think about like "Test that this code called from a tight loop doesn't query the db hundreds of time which would lead to the startup time taking minutes rather than seconds".

Re: Ask HN: Seriously, how do you TDD?

#67
In all my years of coding, I only knew one developer who did TDD properly. He taught me, as I was struggling trying to track down an intermittent bug and nothing was working. Not going to lie -learning to do TDD properly is difficult. Since learning it, I've not met another dev who does it, and most devs barely write tests. Stick with it, practice. You'll eventually hit that ahah! moment.

A story: I used to work with some devs, most of them would finish coding something in 2 weeks and then spend a week debugging it. I'd take 4 weeks to TDD something. My boss was frustrated at my speed until one day a production system which I'd written, stopped working. Several million dollars was stuck in limbo, the backend team responsible for the paperwork unable to do anything. We had a new dev and the bandwagon was "TDD is crap!" was fully rolling that morning. I asked the backend processing person what had changed? The said we had a new client, they had a weird name. Looked into the client log, sure enough. Added a test with the client's name, and ran the tests. It spat out "1 failing test in module X at line Y because 'this'". Modified the code to handle that test, re-ran the tests, 100% pass. Emailed the results output to the team for a manual upload, processing done, committed and pushed to the production branch. The server would update after business hours. Diagnosed and fixed in 5 minutes. I turned to my colleague who I knew was still stepping through his code for 2 days and said (within earshot of the new dev) "How's that bug you've been hunting? Still not found it? This is the power of TDD. I might be slow at the start, but I fix bugs quickly". The dev manager had everyone take a day of TDD training after that, but nobody really understood. The trainer said my approach to TDD is too strict, with my team doing 110 test runs against the next team of 63 test runs. Our solution had only a few lines of code verses the paragraphs of the others though.

Re: Ask HN: Seriously, how do you TDD?

#68
In short: You don't. At least, not in quite the way that the orthodox expansion of TDD implies.

I've found that TDD with a reading of "Testability-Driven Design" was useful for driving adoption of certain architectural patterns that made my software more easily testable. That, in turn, informed how I authored my APIs. And so on.

Re: Ask HN: Seriously, how do you TDD?

#69

A lot of people giving their opinion about TDD, but not many actually answering your question. The answer depends on your approach. You can go outside-in (top-down) or inside-out (bottom-up). The one you use is a matter of taste. A lot of people find outside-in easier, but inside-out can be a bit faster if you have a good sense of how your system will fit together. To take an outside-in approach, start at the top lev…

Thank you, this is a very useful answer. I will check your videos out for sure.

Re: Ask HN: Seriously, how do you TDD?

#70
If you do ‘red, green, refactor’ TDD and write clean ‘arrange, act, assert’ tests then you’ll likely create nice single responsibility classes that are a pleasure to test and extend.

The tests usually run lightning quick because the units will be small.

When bugs occur in your codebase it will be at the right level of abstraction to write tests for the bug.

I almost never do strict TDD, I hack away to get something going and use the tests to refactor the working code into clean srp code. The end result is the same. However where I would use strict TDD is when there is massive complexity (like creating a video encoder) or if you are following a published specification.

So ‘just barely working, green, refactor, else red, refactor, green’ is probably closer to my actual day-to-day. The working code is clean, the tests are clean, and the coverage is good. The order doesn't matter.

Post reply on HN