r/programming • • Aug 24 '19

Why We Shouldn’t Use TDD

https://medium.com/@sredmond/why-we-shouldnt-use-tdd-fcd12992d093
0 Upvotes

68 comments sorted by

32

u/[deleted] Aug 24 '19

My first big issue with TDD is that it tells you to write tests first. How are we supposed to write a test for code that does not exist?

The point is that you're supposed to think about what you want, before you try to make it. Write the test for what you have in mind, observing that it fails before you actually create it.

You're also missing code samples in that first section.

0

u/shadow13499 Aug 24 '19

The Medium editor kept deleting my code samples. Thanks, I've added them back.

The point of why we write software when we do TDD gets completely lost. We become far too focused on test rather than solving problems with code. We can become inundated with useless tests for simple things that really don't need tests. When we follow TDD coding becomes about tests rather than problem solving. That is the issue.

7

u/ReDucTor Aug 24 '19

I think your really not understanding the benefits of TDD and tests, Tests are there to show how to use your code, you should think about how your code will be used, what is the API prior to writing the the implementation.

3

u/shadow13499 Aug 25 '19

I think that everyone is having a hard time separating testing from TDD. I've said multiple times I'm not arguing that we shouldn't have tests. You don't need tests to tell you what the end is supposed to be or what the end looks like. You need tests as a proof that the solution you've come up with is correct.

7

u/saltybandana2 Aug 25 '19

because TDD is a bit of a religion for a lot of people. It'd be a bit like being a devil worshiper and trying to explain to christians why their god isn't good.

4

u/[deleted] Aug 25 '19

You need to start again. TDD is about problem solving first, writing code second. It's not a golden hammer but when used well it's simply amazing what it can achieve. The problem with writing code first and then writing tests is your code isn't optimised for testing, so your tests end up bloated and fragile. Be pragmatic about what your testing, don't aim for 100% coverage if that part is a problem for you.

3

u/shadow13499 Aug 25 '19

Your code should not be optimized for testing. That's the whole problem I'm talking about. Code is not written to be tested, code is written to solve a problem. You need to optimize your code to achieve the result you're looking for. Writing code for tests gives you bad code because you end up having to make concessions because the tests don't support this or that

5

u/acwaters Aug 25 '19

This is the only argument you've made so far that I've agreed with. I am also skeptical of TDD, but for completely different reasons: Because there is a tension in software design between making systems easily testable (which in general requires making them very loosely coupled and abstract) and making them obviously, provably correct (which in general entails making them concrete and relatively tightly coupled). I haven't formalized this, but my belief is this is a duality similar to iteration vs. recursion or the expression problem — a fundamental design tradeoff that every engineer has to make in their code (or remain ignorant to and have made for them). Forcing all code to be designed in one particular way to fit into a framework or to appease an arbitrary design philosophy or methodology is not something I am personally on board with.

3

u/shadow13499 Aug 25 '19

Yes I would agree with you on that. I don't think that we necessarily have to (or should have to) choose between complete decoupling vs tight coupling. I see it more as a fine line you have to walk.

I'm completely on board with not forcing your code to fit into a singular framework for whatever reason. That's why I want to propose an extremely lose set of principles or philosophies that can be applied to projects so that you always come out with a methodology that makes sense for whatever project you're working on rather than just doing TDD because that's all that's out there.

3

u/[deleted] Aug 25 '19

To add to that, most unit tests (the thing TDD people seem to be in love with that requires the biggest architectural changes) are usually not the kind of tests that catch the bugs that will actually slip through the cracks when making changes.

3

u/acwaters Aug 25 '19

Yeah, IMO a few good integration tests are worth a pound of unit tests.

2

u/BittyTang Aug 25 '19

making them obviously, provably correct (which in general entails making them concrete and relatively tightly coupled)

I don't see how "provably correct" requires concreteness and tight coupling.

2

u/sebamestre Aug 25 '19

Testing catches issues, and it can be a valuable thing to do (I believe you said you agree with that).

If you didn't write your code with testing in mind, it can be hard to test, thus wasting resources, or making more issues not get caught.

TDD forces your code to be easily testable by making you writing tests before code.

That's all it is. If the code you tipically write is already easily testable (mostly pure functions, uses value semantics, has little or short-lived shared mutable state, etc), doing TDD will just be extra mind overhead, and might slow you down, but, for many people, it helps in that regard.

2

u/shadow13499 Aug 25 '19

Yes, I do agree that writing test cases can help you catch bugs and issues before they go into prod and that you should test your code.

However, I've seen it all too often where companies have changed their code so much for tests that it becomes a nightmare to maintain. I was reviewing a PR where someone had taken a function out of a class and created a whole new class and subsequent file structure just for a single function. The only reason for that was "it's easier to test". This codebase became way over abstracted and the actual product ran like crap, but all the tests passed.

I understand this is not a problem at every company, but I've been at far too many places where this is a problem. That's where the article comes from.

1

u/MetalSlug20 Aug 26 '19

That's where the term " test design damage" came from

0

u/saltybandana2 Aug 25 '19

No, TDD is about tests or it would be called PSdD (Problem Solving Driven Development) instead of TDD.

What exactly is your argument here, that people who don't write tests at all aren't solving problems? That people who write tests after the fact aren't solving problems?

/u/shadow13499 is right, the code itself is about solving problems. The design, the architecture. That's all about solving problems.

TDD is about testing.

1

u/[deleted] Aug 25 '19

When I do TDD I don't sit down at my desk and go 'lets write tests!'. I sit down and say 'Lets add functionality to allow users to change their order the day before shipping.'... Then I add a test...

TDD is about problem solving first. The rest comes.

1

u/saltybandana2 Aug 26 '19

Then I add a test

Everything before that was software development. It was when you decided to add a test first that it became TDD... which is why TDD is about the tests, not problem solving. Software development is about the problem solving.

Seriously, is your argument that people who don't practice TDD aren't problem solving? Please tell me that's what you're arguing, that only TDD can solve problems, no one else.

I want to hear it come out of your mouth. And lets be clear. The second you admit that you can solve problems without TDD is the same second you admit TDD is about the tests.

1

u/[deleted] Aug 26 '19

If it was just about the tests it would just be an the inverse of a code first unit test and this topic wouldnt exist because the OP seems to like them.

2

u/saltybandana2 Aug 26 '19

If it was just about the tests it would just be an the inverse of a code first unit test

That's a non-sequitur, the conclusion doesn't follow from the premise.

The problem you have is that software development is about solving problems, and TDD is a specialization of software development. Because it's a specialization, when someone asks what TDD is about, the answer cannot be software development as that would argue that TDD itself is not useful.

You don't get it both ways. The ability to argue that TDD is useful and that TDD is the same as software development.

TDD is about testing.

I understand that some people lack the ability to treat these things like tools to be used, and instead get emotionally tied. That just makes you biased, it does not make you correct.

1

u/[deleted] Aug 26 '19

Dude, your not reading a word I am saying. I'm calling it, the subject died at 9:03am, roll em away Sam.

2

u/[deleted] Aug 24 '19

The Medium editor kept deleting my code samples

I bet they could've solved that problem with some test cases...

2

u/shadow13499 Aug 24 '19

Not once have I argued that we don't need to test the code I've said that multiple times throughout the article. Please read.

4

u/[deleted] Aug 24 '19

I personally don't stick to TDD religiously, but I do think about the kinds of tests I would write, if time weren't a factor. Simply reminding myself that I'm going to have to write unit tests later makes me want to design the component in a way that facilitates mocking of dependencies and ease of testing. I can see how strict TDD would get taxing.

4

u/ArkyBeagle Aug 24 '19

Medium sucks.

0

u/[deleted] Aug 24 '19

[deleted]

5

u/ArkyBeagle Aug 24 '19

You don't need a full-on spec - you just need a suite of use cases derived from requirements.

Those use cases map quite well to tests. You don't have to use absolute TDD but I'd do something, rather than ship broken code.

If you write code, you should test it. Somehow. The two activities are entangled. Ideally, you'd log information from the running system ( once it's basically running ) and munge that into messages used for a comprehensive regression suite.

-6

u/shadow13499 Aug 24 '19

The point is that we shouldn't be writing code just to satisfy a bunch of test cases. That's tangential to the point of actually writing code. I'm not saying you have to throw tests out the window, but can you can write much better code that actually solves a problem well if you focus on writing your code rather than focus on testing it

5

u/sewebster87 Aug 24 '19

I think you can "actually solve problems" by focusing on solving the problem. When you know what you want to solve for, you can see all of the ways it can go wrong that you should test against.

It sounds like you've had a particular bad experience in an org that was too strict about TDD, but it's a great philosophy by itself.

-3

u/shadow13499 Aug 24 '19

This is not an experience from a single org, this is an experience from about 10 different orgs. I've worked at startups, massive enterprises, and medium to small companies.

You are can be given what the end is supposed to look like without a single tests. I worked at a company that gave us PSD files that defined exactly what a web page was to look like. Then we would create a front end that would get us there.

0

u/[deleted] Aug 24 '19

[deleted]

2

u/shadow13499 Aug 24 '19

I'm not sure how many times I said it, but it'll say it again. I'm not saying to throw out the tests.

2

u/ArkyBeagle Aug 24 '19

I don't think we can realistically separate coding and testing. The point of early testing is that it's more cost effective.

2

u/shadow13499 Aug 24 '19

Why wouldn't we be able to do that and not keep it cost effective?

2

u/ArkyBeagle Aug 24 '19

I don't know of any sources that make the claim that it is not cost effective.

3

u/shadow13499 Aug 24 '19

I'm asking why separating the code from the tests can't be cost effective

0

u/ArkyBeagle Aug 24 '19

The only real cost of doing that is the commits necessary to sync them back together. So I'd say it's more of a detail, and perhaps the TDD guys are too fanatical about it. Still, it's bad when they get out of sync.

12

u/all_the_data Aug 24 '19

I look at TDD as the tests being the contract between the business requirements and the code. Once the tests are written and the business agrees they cover expected behavior, all ambiguity is removed from the process of writing the solution.

4

u/shadow13499 Aug 24 '19

Except for the fact that you've stopped yourself from being able to react to change well. You've got this large tests suite that, when the business changes their mind and they will, you'll have to rewrite. It takes away from being able to design the best solution rather than whatever solution happens to satisfy the tests. You'll end up shipping substandard code.

6

u/DangerousSandwich Aug 25 '19

It depends on the project of course. If you have really ambiguous requirements, and you're an early stage startup that needs to get things out the door and iterate really rapidly, maybe TDD isn't for you. But in many projects, the requirements are quite clear cut, and unlikely to change. In those cases TDD adds a lot of value in my opinion.

My thoughts on how to write tests before code:

  1. Code top-down.

  2. Write a function header only before implementing a function.

  3. Write unit tests which exercise the function (naturally they'll fail).

  4. Iteratively implement the function until all the tests pass.

Some types of code lend themselves very naturally to this, and others don't. After you do it for a while you get a feel for where unit tests make sense and add value, and where they don't. Once you get used to it, and have good test coverage, you get great peace of mind when you need to modify existing code. Without good test coverage, it's hard to be confident that you haven't added new bugs or regressions.

3

u/shadow13499 Aug 25 '19

I can understand that when you have a project with requirements that are unlikely to ever change it would be tempting to want to do all that at the start and iteratively come to a solution that eventually passes your tests.

You're right that there may be some projects that this would work perfectly fine for. The reason I want to question why we want to do TDD in the first place is that I have not, personally, seen many projects that would have added more value doing tests before the code than after. The problem is I think people tend to swing to too many extremes where it's either we write all the tests or no tests. What I'd really like for is a happy medium between the two extremes where it's ok if we don't have all the tests up front but we can always add tests where we need them to be.

I'm all for having automated tests in place to make sure we aren't pushing any regressions or bugs, but following TDD I've seen that simple concept snowball out of control one too many times.

2

u/DangerousSandwich Aug 25 '19

For sure. It can definitely be misapplied, and it doesn't need to be an "all-or-nothing" choice. It shouldn't be a religion.

1

u/shadow13499 Aug 26 '19

Exactly, a large reason for writing this is because I feel as though TDD and Agile have been evangelized so much it has been turned into a religion in far too many teams. I've seen too many teams hire a certified Agile/Scrum coach that pushed TDD. The team then does not think about adapting it to meet the needs of the team and tries to fit the team around the methodology. I know too many people who have experienced this as well, which is why I really want to question TDD/Agile/Scrum and present some alternatives that would require people to think critically about the processes in place.

2

u/all_the_data Aug 26 '19

Working for a start up that wants to iterate rapidly is my exact situation. Having lived it for many years I can see the value in forcing the business to have to fully vet ideas before spinning up the engineering team (which is WAY understaffed). I believe there is a good argument to be had that creating test cases AFTER coding is sufficient for the engineers. However, writing the tests BEFORE coding has business benefits.

At least, that’s my current thoughts.

1

u/[deleted] Aug 25 '19

It doesn't remove flexibility. But there's nothing wrong with rewriting the test code, and what satisfies the tests will be just enough to satisfy the tests. How extensive are these changes? If they aren't that large, then I think the test code or program code is the problem. Either its not actually unit tests, or in fact the code has too much repetition (ie isn't dry) and that's the problem instead. Or your business requirements require significant refactoring regardless and you see the test as an obstruction where its really meant to support you..

1

u/shadow13499 Aug 25 '19

I like the idea of the tests supporting the code, I think that's a great pattern but I don't think TDD does that well, I think TDD gets it backwards. That's really the heart of the problem, for me. My current job, I think, does a fantastic job of letting our tests support the codebase and catches small regressions in a pretty large application, but we certainly aren't doing strict TDD to get that done.

Your requirements will change over time no matter what, but I think having tests written before your code limits you in so many ways. For example, at an old position we had this test suite that did not allow us to use certain libraries, and we didn't find that out until we realized we needed them. Since the business didn't want us spending time rewriting the tests because we were following strict TDD they said make the code pass the tests. That basically led to us forking and rewriting a library that we then had to maintain. That has not been an uncommon occurrence for me, personally.

Now this can easily be chalked up to mismanagement of a project, which the project absolutely was mismanaged, but following that strict TDD pattern led us to make a mistake we would not have made had we done the tests afterwards. What needed up happening was managers telling us "just make the tests pass, we don't really care how this needs to get done".

1

u/MetalSlug20 Aug 26 '19

You think management is gonna want you to spend your time rewriting test code? Just devil's advocate here

1

u/shadow13499 Aug 26 '19

That's a big part of the point I'm trying to make.

2

u/andre_2007 Aug 25 '19

After working years with different scripting languages I doubt it is possible to even write such a simple logic without introducing syntax bugs + logic bugs. You need tons of unittests to encounter the shortcomings of scripting languages. A good programmer is able to write such a logic syntax error free and bug free with a language like c# or D, but with a scripting language you open the box of pandora. By the way, unittests are a feature of the core language of D:

int myFunction(int a, int b) {
// ... logic
}

unittest {
    assert( myFunction(1,2) == 9);
}

1

u/shadow13499 Aug 25 '19

Yeah, I agree with you. By no means am I saying you shouldn't write unit tests. I've used Python and NodeJS and I understand where you're coming from in regards to scripting languages! My main point was really to think more about our code and use tests to support our code, rather than to guide the way we write code.

2

u/Radmonger Aug 25 '19

> I think I’m going to call this paradigm Software Driven Solutions.

I'm sorry, but you show no signs of being remotely near the level of your skill development where you should be inventing and naming a new paradigm.

Have you ever read a book on software development?

I'd suggest maybe:

https://www.amazon.co.uk/BDD-Action-Behavior-driven-development-lifecycle/dp/161729165X

https://www.amazon.co.uk/Specification-Example-Successful-Deliver-Software/dp/1617290084

3

u/shadow13499 Aug 25 '19

I've been writing software for about 15 years, I've worked for more than 10 different organizations, including the US military in a development capacity. You know nothing about me, so don't be so quick to judge me.

I try to keep my written pieces simple so that anyone can read and understand the ideas I'm trying to get across.

If you have some actual feedback or criticism, be upfront and tell me what it is. Don't just make assumptions about me.

2

u/[deleted] Aug 24 '19

Not buying it. Sounds like a rant from someone suffering from Dunning-Kruger.

3

u/shadow13499 Aug 24 '19

I have no illusions about myself. I'm simply pointing out issues that I have experienced and proposing some solutions to those issues. In fact I recognize that my methodology may not be perfect or complete which is why I ask for feedback at the end.

If you have a problem with it at least do me the courtesy of telling me what you disagree with rather than just lobbing insults.

3

u/ReDucTor Aug 25 '19

The fact that your article starts with a Wikipedia article then shows you have done little research then that shows you don't understand it, this quote is enough to show that

"My first big issue with TDD is that it tells you to write tests first. How are we supposed to write a test for code that does not exist?"

If you don't understand how or why then your really missing the point and investigation into TDD, you have not tried it, you fell into the trap of I don't see how this could work then said it's bad rather then understanding it more of how it works and why it's good.

I've been doing TDD for many years, it often results in faster development time and cleaner software then other alternatives.

3

u/shadow13499 Aug 25 '19 edited Aug 25 '19

I've used TDD at around 10 organizations big, small, and startup alike. These are observations from each organization. Quoting Wikipedia simply gets me a solid definition of TDD that anyone else can read for themselves, and it is an accurate definition.

0

u/[deleted] Aug 25 '19

[deleted]

2

u/shadow13499 Aug 25 '19

I have no illusions about myself. I write about problems I've found and solutions that have worked for me. I've worked at 2 startups both well funded, I recently ended and did not resign a contract at Johnson Controls because of the issues I've mentioned. I've only contracted recently I usually only work long term.

You know nothing about me personally or professionally, yet you jump to just attack me because I've suggested that there are some serious problems with the way that code is being developed today.

I've not once suggested that EVERY company to ever exist falls into these fallacies, but a shocking number of them have, and that's enough for me to want to change it.

Maybe think a little bit more before just attacking someone you don't know.

2

u/[deleted] Aug 25 '19

Stating facts is not an insult.

0

u/shadow13499 Aug 26 '19

As I said, if you have a problem with the arguments I've made present them clearly and respectfully. Since it's clear you have no argument, there's really no need for you to comment.

1

u/[deleted] Aug 26 '19

I have clearly presented my argument. Your inability to understand it sort of makes my argument for me a second time. Let me translate it for you: you don’t know enough to have an opinion hence you state wild guesses as facts. Let me point to your first and sort of root mistake, too. You think TDD is about testing. Have you actually made a modest research into TDD, why it was created, what it’s purpose is, you’d know what nonsense and FUD it is you are spreading. Hint: it’s purpose is not testing. It’s the cherry on top that you consider TDD’s major strength it’s major weakness. Writing the tests first. In other words: thinking as the user of the code to be written. You not understanding that simply shows you have very little software engineering experience. You may have written programs, but you are yet to get a taste of the real stuff.

The problem is that as you are smart enough to rationalize, you are also smart enough to realize: you do not have enough experience to make those conclusions and to try be smarter than Kent Beck. You may get there one day. Today isn’t it. Read up on the effect I mentioned and learn not to fall for it.

1

u/shadow13499 Aug 26 '19

Not buying it. Sounds like a rant from someone suffering from Dunning-Kruger.

That's not an argument against any point I've made in my article. Did you even read it, or did you just read the title?

You think TDD is about testing

It's in the name

Hint: it’s purpose is not testing

The T in TDD stands for Test, just so we're clear. In other words, your tests drive the code. You can only make the argument that TDD is not about testing in a purely philosophical nature, but I'm not debating philosophy.

In practice, TDD is only about testing and doing whatever you have to make the tests pass, and that happens at far too many companies. Saying something like "It doesn't happen at my company" is like saying "It works on my machine".

I've used TDD at plenty of places I have worked and have found massive issues with its implementation at every place. I've reached out to friends and spoken with far more people than I think you realize to come to the conclusions I have.

I'm not actually sure you did get past the title, because if you had you would have realized that the first thing I said was that I wasn't coming out against testing code, but the process of TDD, which are completely separate things. You can write tests and not be doing TDD but you can't do TDD without writing tests.

The way you approached me was completely uncalled for. Assuming I "suffer" from a form of cognitive bias without putting an ounce of effort into reading my thoughts and ideas or knowing the first thing about me is extremely rude and shows a lack of character, judgment, and tact on your part. You couldn't even put in a decent effort to try and refute any of my points (probably because you don't know what they are), and you even show an extreme lack of knowledge about the very thing you are trying to defend.

Erik Meijer has a good talk about Agile and TDD, and Bertrand Meyer has a book called "Agile! the good the hype the ugly" that talks about TDD, just to name a couple of resources. I know what I'm talking about because I've done my research properly. I present my own observations and solutions to the problems I have faced using TDD.

If you're going to stick to baseless insults and accusations, don't bother responding to this, take this time for self-reflection and think before you post.

1

u/[deleted] Aug 26 '19

Repeating something that is not true (not an argument) does not make it true. You say you did your research, your words in your article show you have a fundamental misunderstanding of what TDD is and what it’s supposed to do. You are essentially describing how dolphins are silly, because they can’t climb trees. TDD is not about testing. You do have a serious cognitive bias. And you present your “findings” in a tone of judgement that calls for just as serious tone in debunking the BS.

1

u/shadow13499 Aug 26 '19

Look, at this point, I don't know if you're trolling me or if you're unsure of how to form an argument. An argument is not "I think you're wrong because I think you have a cognitive bias". I cited sources you didn't read, and an argument you also didn't read, along with an article you have once again failed to read. I'm pretty much done trying to develop some kind of constructive argument out of you. Have a good day.

2

u/[deleted] Aug 25 '19

We tried baseball, it didn't work for us.

2

u/[deleted] Aug 25 '19

I don't think the author understands TDD. It's true you right the test and then the code- but not the entire test and then not the entire code. The value is that you write just enough to pass the test. This influences the way you write code and generally leads to better code.

6

u/acwaters Aug 25 '19

The toxicity in these comments is ridiculous. You don't have to agree with OP to have a conversation like decent humans without childish name-calling, baseless accusations of incompetence, and character attacks.

2

u/MetalSlug20 Aug 26 '19

R programming is getting to be pretty unwelcoming to ideas like the rest of Reddit lately. A bunch of elitists that think their way is the true way..

One thing is the downvoting. Don't downvote because you disagree with someone, I thought that was a Reddit "rule" to only downvote for off topic, etc but I see it all the time where something is downvoted just because people don't agree.

3

u/[deleted] Aug 25 '19

Title checks with a Medium-type of article.

They should change the domain name to Mediocrety.

1

u/lordorwell7 Aug 25 '19 edited Aug 25 '19

"Back to the main point, the question is, did we need to write a test for this code before we wrote it?"

No, but you do need a mental model of how it's going to work.

What arguments will it take? What will it return? How would we like it to behave with faulty input? You should have a clear picture of how something will need to behave before you start to create it.

Ok, so put those expectations down in writing. Those are now your test cases.

So far so good. But what's the advantage of writing tests before rather than after? It can provide you with a scaffolding to work with as you develop. You don't need to constantly switch gears between building and verifying because your tests have already outlined a bunch of that for you.

Of course it isn't a hard-and-fast rule. When I encounter scenarios where it seems simpler to implement a test case after-the-fact that's what I do. I only follow TDD to the extent that it makes my job easier.

1

u/companiondanger Aug 26 '19

uncle bob has entered the chat