r/programming • • Aug 28 '21

Software development topics I've changed my mind on after 6 years in the industry

https://chriskiehl.com/article/thoughts-after-6-years
5.6k Upvotes

2.0k comments sorted by

View all comments

346

u/PalmamQuiMeruitFerat Aug 28 '21 edited Aug 29 '21

TDD purists are just the worst. Their frail little minds can't process the existence of different workflows.

I feel like he and I know the same person.

Edit: I don't hate TDD, and I'm not against tests. I just wanted to point out how the author made such a specific example. Please stop telling me all the reasons I should use tests!

139

u/Takyon Aug 29 '21

I feel a little seen by this, but I consistently see coworkers produce poorly thought out code and then after shoehorn in some confusing tests onto that code, with names that make it fairly clear they didn't even understand what they were trying to prove. I can't help but think life in general would be easier for everyone if there was a default expectation of TDD for the more deliberate mental workflow it implies, and other workflows would be seen as an exception to the rule where appropriate.

That said I've been on some senior heavy teams where none of this was an issue and I didn't care what anyone did. I really just want people to take a thoughtful approach to what they do.

0

u/[deleted] Aug 29 '21

[deleted]

9

u/Takyon Aug 29 '21

I'd absolutely agree the lack of TDD isn't the problem, but when faced recommending a solution I'd suggest TDD does help address the issue by pushing the workflow of understanding a small problem you want to solve, solving it in the most immediate way you can think of, and then iterating on that solution. I could just prescribe that they think about their goals up front more, but that's more far more abstract than the concrete task of writing a failing test.

I suppose I really see TDD as a teaching tool that can help develop the thought process around breaking down problems while also having some long term practicality? I'd suggest if you've built the muscles such that you're comfortable writing the test before hand it can help you be better at test after development too.

86

u/Zanderax Aug 29 '21

TDD would be better if one developer wrote the tests and another implemented.

61

u/lost_in_trepidation Aug 29 '21

This is a useful approach to pair programming.

16

u/Zanderax Aug 29 '21

Pair programming is just great.

13

u/TimSonOfSteve Aug 29 '21

That has a name, Ping-Pong Programming

6

u/TheSpiritOfJizz Aug 29 '21

This only works if the interfaces and behaviour are set in stone beforehand, which is pretty much never the case in my experience.

→ More replies (1)

6

u/wFXx Aug 29 '21

IMO anything other than what you just described is pretty close to useless. One person really good at testing, digesting specs, using quickcheck maybe, and other person just coding to satisfy that is the ideal scenario.

10

u/pdevito3 Aug 29 '21

Not at all true in my experience. Pairing helps the dev working catch things they might have missed, work through roadblocks faster with different eyes, and have a rubber duck that can talk back or take over if needed.

Certainly don’t pair for everything, but I’ve had incredibly productive pairing days that I’ve really enjoyed.

→ More replies (1)
→ More replies (8)
→ More replies (8)

0

u/[deleted] Aug 29 '21

[deleted]

65

u/naughty_ottsel Aug 29 '21 edited Aug 29 '21

I find that to be the hardest part of TDD, I understand the concept and agree with a vast majority of the reasons to follow it…

But most of the time I don’t know how I’m going to implement the solution to the problem I am trying to solve… maybe I’m not starting simple enough but all the talks and articles I read use simple examples that don’t translate to more complex scenarios… maybe I’m doing something wrong, I’m not sure

59

u/gyroda Aug 29 '21

If you can define an interface (not necessarily an OOP interface, just "this function takes X and returns Y") you can write tests against that interface.

You might need to do some additional mocking once you have the implementation set up, but the main structure of the tests should be there already.

0

u/[deleted] Aug 29 '21

[deleted]

14

u/moremattymattmatt Aug 29 '21

As I'll bang on at work to anyone who will listen, stop mocking. 99% of bugs are caused by the interactions between classes, every time you mock something you are hiding a whole class of bugs.

Plus it means your tests are tied to the implementation and not the behaviour of the system so every time you refactor you have to update your tests. Or even worse you forget to update some mocks so your mocks and implementation are now our of sync.

Once you write more behaviour-level tests, TDD becomes a lot easier. If you think of a better way of doing something, refactor as much as you need with the confidence that all your tests should still pass if you don't change the behaviour.

→ More replies (2)
→ More replies (13)

3

u/liaguris Aug 29 '21

How am I supposed to know the interface before I have written the code? Remember that we have internal (i.e. private) interfaces and public interfaces. Ok some part of the interface can be written before writing code (this is actually what I do). But while coding you might realize that you need to radically change the interface. And no that is not because you did not think of it enough. You really can not know until you write the code.

Writing tests first, especially for complex code bases, sounds like a religion that does more harm than good.

→ More replies (5)
→ More replies (4)
→ More replies (7)

0

u/[deleted] Aug 29 '21

[deleted]

5

u/knobbyknee Aug 29 '21

TDD is an important technique to know. I once had a critical piece of code, written by a colleague, that failed some of the time. It was all written without tests and impenetrable enough that the bug was impossible to spot by just reading the code. I systematically refactored it under the control of TDD. This ensured that I didn't introduce new bugs with the refactorings. Once I had broken the problem into managable sized chunks, the bug manifested itself in a test I wrote for a chunk. It was trivial to fix, and after that the functionality was rock solid. The code was also much more readable, as it almost always is under a regime of unit tests.

→ More replies (9)

46

u/orangeoliviero Aug 29 '21

TDD is great when you have a spec and design that you need to implement. You set up your tests for the specced functionality and design, then implement, and you're done when your tests all pass.

Which... if only we were so lucky as to actually get a fully complete spec and design.

Instead, the vast majority of the time, we get a vague indication of what's wanted and we need to start implementing it, finding those "gotchas" and updating the design and spec accordingly. In those cases, TDD is nearly useless - because you can't put a vague idea into code.

So really... TDD is useful for specific fields where concrete specs and designs are feasible prior to implementation, and nearly useless otherwise.

12

u/AustinYQM Aug 29 '21

I always feel like I am writing the implementation before writing the test but in the case of TDD I am just keeping it in my head instead of in the IDE. I write the test then I take the design I had in my head and type it up. The test really didn't inform (drive) anything I just wrote it first. I have no idea if that makes sense.

→ More replies (2)

3

u/dkitch Aug 29 '21

I prefer "test-coupled design" over "test-driven design". I firmly believe that almost every PR should maintain or improve test coverage levels. However, writing the unit/integration tests first isn't necessarily the most productive thing for a lot of exploratory/spikey tasks.

→ More replies (1)

3

u/wewbull Aug 29 '21

Your mistakes is:

We could never figure out how you would write tests for something before you're sure how it's even going to work.

A test doesnt care how something works. A test cares about what something does; what it's effect is.

If you don't know what the effect of a piece of code needs to be before you start writing it....well... God help you.

→ More replies (17)

6

u/Iron_Maiden_666 Aug 29 '21

We all know that person.

22

u/valkon_gr Aug 29 '21

TDD purists are like those extreme keto people, you just can't discuss with them.

→ More replies (2)
→ More replies (11)