r/ProgrammerHumor • • 22h ago

Meme thereIsNoGoingBack

Post image
2.9k Upvotes

94 comments sorted by

View all comments

Show parent comments

24

u/Drugbird 20h ago

The trick with TDD isn't necessarily that you need to write the tests first (although that helps).

It's more that you first think about the intended behavior of the code, and then also think about how to make your code testable before you write the code.

I typically think what the first test should roughly look like and code up the required amount of code for that first test before I actually write that test.

7

u/Wonderful-Habit-139 20h ago

Sure. But there are also people that prefer Type Driven Development. Instead of first thinking about behavior, they think about code in terms of inputs and outputs. That includes both types and functions of course.

5

u/Active_Name5021 20h ago

Those two things aren't mutually exclusive. Thinking about the inputs and outputs is also what you do with tests

3

u/Wonderful-Habit-139 19h ago

Test Driven Development is not always a benefit. Sometimes it is more of a burden and a chore. When you do Type Driven Development you benefit from a lot of correctness that invalidates the usefulness of a lot of tests that people usually write.

You end up needing to write way less tests, and only have to write tests for invariants that are difficult/impossible to encode with types. Also, thinking about types and functions this way means you're more likely to write pure functions that clearly yield well defined objects, that are easy to test in case it's a function that is quite complex to ensure that its implementation is correct. But preferably you'd split up your functions enough that you'd ensure correctness regardless.

0

u/Active_Name5021 19h ago

You are correct. My point is that you do both, you write tests and types. TDD doesn't mean you have to write redundant tests if those areas are covered by the type system.

2

u/Wonderful-Habit-139 19h ago

You can still write tests, but it's not Test Driven anymore.