Live data from Hacker News

Ask HN: Seriously, how do you TDD?

news.ycombinator.com

21–30 of 86 posts

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

#21
There is a lot of really good advices here about when to do TDD and how to do it, so i would only reiterate earlier comments.

My personal experience is that i start TDD once my project is "mature" enough that the next upgrade/addition can be put at two different place easily enough and i don't know where is the best place to do it. Basically, on my latest project, i asked myself something like: "is this part of this class method, or should the main loop take care of it?". That's where i started completing tests to 90% coverage (main loop + pagination mocks were skipped, ideally i could've reach 95% but laziness caught up) and started TDD. In the end, i still made the wrong choice and added my change to the class method, only to change it back two weeks ago (or roughly six month later). Here, TDD didn't help me to make the best software architecture choice, but having to write test first for this part of code made me decouple it from the method i originally put it in, and allowed me to made a architecture change in less than an hour.

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

#22

> I know I need to, at least, read a CSV, parse a CSV row into some data structure and then categorise it. So I TDDed my way through to complete these “sub problems”. However, when I was about to apply the categorisation rules I realised that the data structure I created didn’t help, was just bad and didn’t work at all TDD is kind of a forcing function for modular design (whether OOP or functional programming). In yo…

It is indeed a forcing function for modular design but what you get with that if you aren’t careful is heavily over engineered code. Take that first module, the CSV loader… if you build it in a truly modular way, it can be hard to stop with the features that just support your end user application.

> it can be hard to stop with the features that just support your end user application.

I mean, it's not hard, you just choose to stop.

All it really takes is an awareness that you're programming to solve a specific limited defined task, not programming just for fun.

Of course, if you're doing it for fun, then fine, don't stop.

But nothing about stopping is hard. It's just a choice you have.

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

#23
post #20

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 very false in my experience, and I did 100% TDD for years (have a more nuanced approach currently in TypeScript/React) As a programmer with a problem, you first instinct is to start at the solution. TDD pulls you back, and you first have to write the api and decide how you verify it. If you're doing it well, you make both of those things as simple as possible, first. Reduce dependencies and inputs, etc. That's…

A very good explanation. Yet what I think is confusing a lot of (very?) good programmers (not me) is that they already do that intuitively by not letting methods become longer than 3 or 4 lines, and whenever implementing a new method, by writing the return statement first (i.e. returning a mock object or similar) and working backwards, i.e. only writing enough code to be able to return the desired value.

If one then goes and tries to write tests for these kind of methods, it feels like superfluous busywork.

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

#24
post #5

Most software development these days consists of applying glue code to myriad of frameworks and libraries as opposed to writing algorithmic code. This kind of work does not lend itself well to TDD.

> This kind of work does not lend itself well to TDD.

I couldn't disagree more. Indeed, it's precisely because framework and library black box behavior and upgrades tend to introduce random unforeseeable bugs that TDD is essential.

It's super important to take some known inputs, and after a series of framework/library operations, to check that the outputs are what they're supposed to be, for normal operation and for all the wacko edge cases (zero-length strings, etc.).

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

#25
A year into a novel project, a teammate expressed their concern about a lack of unit tests. They'd even spent time creating a bunch early on. I asked if they still ran or were even relevant any longer and the answer was no.

Particularly with exploratory projects, I'd recommend focusing on rapidly iterating to reach the simplest solution (businesswise and implementation-wise).

If in the end non-trivial logic must be specified, write some tests. Your definition of 'non-trivial' will change with experience.

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

#26
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 I do the architecture design, I write the code, then I write my test by following the design graph.

If I don't know what the design would look like, I do what I call "code freestyling" to get a basic idea of what the overall design would look like then I repeat the above step.

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

#27
post #12

Don't let TDD destroy your architecture. TDD encourages a form of programming where you build from the outside-in, building layer after layer of abstraction, putting off solving the actual problem until you get to the messy gooey centre where it ends up being a kludge. It's also very easy to make flawed assumptions in your original TDD spec which you don't realize until you get to that gooey centre, having wasted N d…

Seconded. Only ever add tests after you have a working version. Starting a greenfield project with TDD is a waste of time. Just get something out then start testing your assumptions with unit tests, even then, don't test the whole codebase, test your fundamental assumptions and core features (like user auth, roles, permissions and their invocations, esp if multi-tenanted).

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

#28

A year into a novel project, a teammate expressed their concern about a lack of unit tests. They'd even spent time creating a bunch early on. I asked if they still ran or were even relevant any longer and the answer was no. Particularly with exploratory projects, I'd recommend focusing on rapidly iterating to reach the simplest solution (businesswise and implementation-wise). If in the end non-trivial logic must be s…

These old test are the fundament. Test_application_window_rendering may sound simple and useless now, but if this test fails something is really wrong. How else would you test something like dependency upgrades?

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

#29
Don't think in terms of tests -- think in terms of examples.

Write examples of how you would like to be able to solve common cases and handle edge cases if you had the ideal interface based on what you know at the time, then use unit tests with stubbed out interfaces as a tool while trying to develop the implementation.

Refine and even rewrite your examples as you learn about what works and what doesn't work.

Recursively break down complex pieces into smaller modules as you learn what can be handled independently and composed with the other pieces. Break off independent examples depicting how you expect those modules to work and be used as new unit test suites for those new modules.

Don't sweat testing the implementation details, especially when prototyping. What you want is to use tests as a way to speed up your development process and improve the quality of your design and architecture.

Try to write tests that show off how nice your code is to use and how few edge cases you need to handle when calling it. It'll raise the bar for how your code interacts with the rest of the application or system and give you smaller pieces to work and focus on.

Finally, for really green field stuff with a lot of unknown unknowns, you often have to write a lot of standalone, bottom-up throw-away prototypes for things. That doesn't invalidate the usefulness of tests, so don't be afraid to do that pretty regularly.

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

#30
I don’t know if this is pure or orthodox but: Start with one or two high-level interfaces that should meet the functional requirements.

It defines the information you need to send in and/or receive.

Non functional. Then capture some basic behavior of one of the methods/properties in the tests, then implement the solution. Then start capturing edge-cases the same way.

If you’re asking yourself “how” or “should” I test this, then you’re doing great.

Apply SOLID principles to separate meaningful concerns into separate classes; and iterate there.

Take my viewpoint with a large grain of salt.

Post reply on HN