Live data from Hacker News

Ask HN: Seriously, how do you TDD?

news.ycombinator.com

71–80 of 86 posts

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

#71

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…

"The said we had a new client, they had a weird name."

This is the material piece of information which lead to the fix. Your TDD simply made it faster to locate the offending line.

Seems like a really poor justification for TDD imo.

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

#72
post #55
post #48

Earlier quoted context omitted.

I have never worked with that sort of time luxury.

It's frequently faster. Half-assed designs will cost you time forever.

Let's not start throwing pejoratives around, there is nothing half-assed about having an idea how your design will work before you blindly start coding probably-wrong stuff.

I've also found having apparent time-luxury to be a rather paradoxical thing, the more of it you have, the more people start spending it by creating bureaucracy and busy-work. It never gets spent how you expect it to be spent.

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

#73

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…

"The said we had a new client, they had a weird name." This is the material piece of information which lead to the fix. Your TDD simply made it faster to locate the offending line. Seems like a really poor justification for TDD imo.

I agree, this sounds like a completely normal process of finding and fixing bugs.

The stack trace tells you where the error is occurring, your intuition leads you to the relevant caller function that you should test, stack trace or logs should lead you to the offending parameters (i.e. the weird name).

With these three pieces of information you can write a failing test and implement a patch.

I don't consider this approach to be "TDD", just "software development with good test coverage".

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

#74
I used to work with a lot of ex-Pivotal folks. Their workflow was something like:

1. Talk through the requirements and write down some key bullets

2. Whiteboard some design options. Make sure you have at least 2 interesting ones.

3. Prototype them out. Decide on your favorite.

4. Delete the prototype.

5. Start writing tests now that you know where you want to be, and TDD from there.

The key point is that TDD has to start from a relatively defined sense of what you want. If you don’t have that it’s totally fine explore. Just don’t ship that code.

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

#75
post #66
post #38

Earlier quoted context omitted.

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 aro…

> Tests make refactoring slower, because you might have to also update the tests

No, tests can make refactoring slower, because they often test implementation detail instead of interface. Being able to distinguish the two is not as easy as it sounds, typically it requires deep understanding of the domain, which people writing tests might not have yet.

> in a complex enough code base, refactoring is impossible without sufficient test coverage

Sure, and in my experience complex code bases are typically complex due to the amount of accidental complexity, not because they actually solve complex problems. Like layers of overengineered classes intertwined through dependency injection for the sake of being more... testable. See the irony?

And indeed, refactoring becomes quite hard. You have two options to make it easier:

1. Identify and remove accidental complexity.

2. Cover it with tests and set the complexity in stone.

It's like fighting cancer with painkillers. It might look like it's working, but you going to die pretty soon.

> 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

Oh, this is a great example. I saw the exact same thing implemented couple years ago. The whole team hated it because it caused tests to break constantly. And yet nobody on the team had the balls to remove it, and they were even following the pattern for new tests, because past tests had it.

If startup time matters for your app, there will be plenty of signals apart from tests that it's broken. If startup time doesn't matter, why are you testing it?

By the way, you haven't even noticed it, but you conflated startup time with number of queries. You're testing the wrong thing. What if hundreds of queries are still blazingly fast (due to being simple and/or cacheable) and have negligible effect on startup time? What if existing startup time is already extremely high (e.g. you're loading ML models in memory)?

If startup time truly matters for you, you shouldn't be testing it, you should be monitoring it. And I'm not even talking about some complex monitoring systems. When someone on your team says their developer experience got worse, that's the type of alert you need to listen to.

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

#76
TDD is hard when you don't know what your goal is. But it's easy to "just start executing code". I leverage it to get into an exploratory mode in order to think through my designs.

How I TDD 1. create a file with a test. 2. Get it running continuously (dotnet watch test or mocha --watch) 3. Write the code in the same file (just to start the design) as the test so I don't have to bounce around 4. Iterate at step 3 for a while to explore the core domain/model/data (in your example, a Transaction has an amount, a payee, and time for instance), just to reach "escape velocity" 5. Ruthlessly push out dependencies that cross boundaries (file system, network, etc), protecting the core 6. Practice Red-Green-Refactor technique (write a failing/not compiling test, make it pass, refactor) 7. Red-Green phase, I just make things work 8. Refactor phase, I design, reflect on object/function communication patterns

Designing architecture is not easy. And keeping it simple, even less. So I've spent time learning about design patterns, boundaries, and communication patterns and implications to improve my design skills.

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

#77
Quite frankly very few people can do TDD well. Hence why most developers hate it. They've never been taught by a master.

The majority of developers have less than 5 years experience, and they're teaching new devs. So the skills that we're developed by people who embraced TDD in the early 2000s have been swamped out by new people.

I think the best way learn if you don't have access to a master is probably videos. See if can find kent beck doing some examples. There's subtle knowledge & flows that hard to teach through just text alone.

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

#78

You can't "drive the design" with TDD. To write a failing test, you have to have a requirement which is encoded by the test. That requirement doesn't come from TDD, and TDD can't tell you how to procure it. To go from a high level requirement like "command line application to track transactions and show expenses by category", you need to go through several levels of detailed design before you have anything that is im…

That's a good thing, TDD is a prompt to say you don't really understand this requirement. Requirements aren't a design.

TDD makes you define what your input is, and what your output is. What's in the middle your free to choose.

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

#79
I think you are doing it correctly, you just need to break that iceberg into smaller chunks. You’re getting caught up in the ideological nonsense that surrounds TDD and Scrum. “It can categorize a single transaction correctly” should be translated to a problem statement, “I want to be able to categorize a transaction.” You must then brainstorm feasible solutions for this or different aspects of the problem, develop the spec for implementing that, in the form of tests, “able to do this, able to do that,” and build the solution. Then afterwards you can test it against some examples and ask yourself, does this solve the problem? How to improve? Come up with a change or completely different solution, rinse, repeat. I would say a general statement like categorize a transaction is difficult because you don’t have any specific examples. Come up with a list of a dozen transactions that need categorizing and your TDD experience will become much less frustrating.

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

#80
I think part of it, by reading your question, is that:

a) TDD primarily refers to unit tests (it does not address other types of testing that you need to take into account when designing a program)

b) you have the wrong idea of what constitutes a 'unit' test versus other kinds of testing. Specifically, as per M. Feathers, anything that touches the filesystem, e.g. to read a file or database (as you do here), is not a unit test.

I would highly recommend Chapter 2 of M. Feathers' "Working effectively with Legacy Code" on this topic.

Post reply on HN