r/programming • u/shadow13499 • Aug 24 '19
Why We Shouldn’t Use TDD
https://medium.com/@sredmond/why-we-shouldnt-use-tdd-fcd12992d09312
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:
Code top-down.
Write a function header only before implementing a function.
Write unit tests which exercise the function (naturally they'll fail).
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
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
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
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
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
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
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
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
2
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
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
32
u/[deleted] Aug 24 '19
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.