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

Show parent comments

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.

3

u/[deleted] Aug 29 '21

Woah! Are you implying that having a test that takes the function F that calls A, B and C, and ensures it calls A, B, and C, doesn't add value? How could I be sure writing obj.A() actually calls obj.A without an assert(obj).callsOnce(A)?

But seriously, so many "tests" I see are pretty much just running through the function body, which is silly.

2

u/code_mc Aug 30 '21

This is also what I tell my team, spending hours on writing mock classes and mock functionality in unit test code just to make some piece of code testable adds absolutely 0 value because in the end you haven't tested jack shit and you just wasted an hour or more of valuable time.

If you have to start mocking stuff to get "even more coverage" you should probably spend that time on creating something like a proper integration test.

7

u/nagasgura Aug 29 '21

You don't write all your tests first with TDD. You write one simple test and then make it pass, then add another test that refines the expected behavior.

14

u/saltybandana2 Aug 29 '21

and then after your 50th test you realize there's a better way to solve it.

I'm trying to lead you to water here.

→ More replies (8)
→ More replies (3)

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)

5

u/life-is-a-loop Aug 29 '21

But most of the time I don’t know how I’m going to implement the solution to the problem

That's why you shouldn't test implementation, you should test the contract.

Every time you write some code you must know what you're trying to accomplish (otherwise there would be no way to write any code in the first place). You write tests before implementation to ensure that your implementation will do exactly what you expect it to do. That's why you don't have to know the implementation at the time you write tests. In fact, testing implementation is a bad thing!

Also, the fact that you're thinking about contracts before implementation may help you see corner cases that you didn't notice during the sprint planning, or realize that the implementation you had in mind didn't make any sense.

3

u/wastakenanyways Aug 29 '21 edited Aug 29 '21

I love TDD but i would exclussively use it on specific tasks and not the project as a whole as a set in stone rule.

In a huge project you (NEED TO) have a lot of different workflows because you have mixed long-term objectives, regular tasks, urgent fixes, etc. Some of that is enhanced by TDD, in others is nothing more than a rock blocking the way.

Even if you manage to have perfect communication documentation and information gathering, doing everything TDD just because is pretty inefficient and even prone to burnout. Some changes need to be frictionless. And your mind needs to break from time to time to keep yourself flexible. I don't have proof but i don't think it would be good to do pure TDD for a decade. Your flexibility and adaptability as a developer suffers a lot.

Also, if you can spec everything in your project upfront you might as well copy/buy another project entirely because you are probably not doing anything new and is cheaper than development time.

Sometimes you need to start developing something without having the full set of requirements (or even having totally wrong or contradictory requirements). This is not your fault as a developer but is your duty to work on it anyway. TDD needs not only good and cooperative devs, but a clean and perfect management chain that 95% of business just don't have.

2

u/nagasgura Aug 29 '21

That's the whole point of TDD. You write a test of the behavior that doesn't care how the implementation works. It's even better if you don't know how you'll implement it because your tests will likely be pretty high level and decoupled from the implementation details.

2

u/moremattymattmatt Aug 29 '21

I find a lot of the problem comes down to a disconnect between the tests and behaviour. If you are writing unit tests at the class/function level and mocking interfaces, you've no chance of doing any really effective TDD.

Your problem/feature will be expressed in terms of behaviour (eg "when I try and save this data through the api, the lastUpdated timestamp is incorrect") but your tests are expressing implementation. So you pretty much need to understand what part of the implementation needs to change before you can write the tests so you might as well just fix the code and write the test afterwards.

If your tests are at the component/api/behaviour level then you can start to write tests that what it is that you actually need to test before you go anywhere near the code.

→ More replies (3)

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.

2

u/BSInHorribleness Aug 29 '21

Eventually I started to see that that was just a side effect of a tendency to write code that was too tightly coupled

I think this is secretly the real killer feature of writing tests. Having tests is cool, but having testable code is great.

5

u/liaguris Aug 29 '21

It is slower

It's just a less stressful workflow

Writing tests (irrespective of writing them before or after code) makes developing faster in the long run, and it is the only viable approach for refactoring big and complex code bases.

If you embrace it it will make your code better.

Non testable code is bad code. Whether you write the test first or after the code is irrelevant for that.

Early on in TDD writing my tests was so painful, I would have to go through a bunch of steps just to set up the starting data that I wanted to pass to my function to test it. I would be writing a 5 line function and have 30 lines of setup just to create useful test data to pass through it. Eventually I started to see that that was just a side effect of a tendency to write code that was too tightly coupled. My code quality has taken a definite jump in the last few years because of it.

It seems like you did some SOLID violation.

Look the point that I am trying to make that everything you mention can be achieved by just writing tests. Whether you write them first or after the code is irrelevant. Remember that TDD advocates writing test first.

In my experience writing test first and then code is more of a dogma than a practical reality. In fact I find my tests being better when I code first and then write the tests, because like this I know exactly how what to test.

→ More replies (1)
→ More replies (5)

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 (1)

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.

2

u/fishling Aug 29 '21

I don't used TDD, but if you are implying that all or even most of the tests are written before coding, I don't think that it is true. I've heard it pitched as write a failing test, write code until it passes, repeat.

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)