News for hackers. Stuff that matters.
Re:I was saved by Test Driven Development by loveparade on Saturday February 11, 2023 @09:30PM (#34758995)
TDD can be great if you know exactly what you're building and what you must test for, but it gets in the way when you're rapidly prototyping and exploring.

Just don't be dogmatic about practices like TDD. It's always a tradeoff. Being a good engineer means understanding when the tradeoff is worth it and when it isn't. Be wary of development teams, or people, that force you to practice certain methodologies regardless of context.

Re:I was saved by Test Driven Development by wpietri on Saturday February 11, 2023 @09:46PM (#34759132)
I think it depends on what you're exploring, honestly.

TDD comes out of the same circles as Extreme Programming, aka XP. In XP, there's something known as a spike, which is basically a short-lived technical prototype. If you really don't know how to tackle something, you build a quick one to throw away.

In that case, yes, definitely don't do TDD, because a goal of TDD is to build up a good test suite for production code. It's not a good match for throwaway code (assuming the team really has the discipline to throw it away.)

But if I'm exploring in a way that feels less disposable to me, which happens often, then I'm happy to use TDD. E.g., if I want to try out a new way of solving a problem, or I have an idea for an architectural improvement. Then I'll use TDD to force myself to think about it from the outside in, to keep design considerations to the forefront.

Re:I was saved by Test Driven Development by LAC-Tech on Saturday February 11, 2023 @09:57PM (#34759212)
TDD can be great if you know exactly what you're building and what you must test for

But you should know this from your functional requirements, right?

(Slightly sarcastic comment, I know in a lot of places people just start coding without a plan, but I think TDD and functional requirements go together well).

Re:I was saved by Test Driven Development by devoutsalsa on Saturday February 11, 2023 @10:21PM (#34759370)
TDD is pretty useless when the requirements are shit. It just takes longer to realize you need better directions.
Re:I was saved by Test Driven Development by scott_w on Sunday February 12, 2023 @01:32AM (#34760455)
Or it tells you straight away that you have no idea what you’re supposed to be doing instead of spending 3 days building the wrong thing.
Re:I was saved by Test Driven Development by chrisandchris on Sunday February 12, 2023 @02:24AM (#34760699)
> ... start coding without a plan ...

Well, I don't do that. But i usually don't know all the names of my (internal) interfaces and models and properties right at the start. I might move things around and rename stuff along the way. And I think that's quite normal if you don't live in a world where the spec for a small task is multiple books thick.

Doesn't TDD get in the way with? Wouldn't I keep refactoring the tests all the time then?

Re:I was saved by Test Driven Development by missingdays on Sunday February 12, 2023 @05:40AM (#34761783)
When you rename an interface or a method, IDE renames them in tests too
Re:I was saved by Test Driven Development by dmitriid on Sunday February 12, 2023 @01:39PM (#34765480)
> But you should know this from your functional requirements, right?

Functional requirements describe observable behaviour. You can't write any meaningful tests for that behaviour until a lot of your code is written.

Yet somehow in all the discussions there's this unexplaned leap from "you have the requirements" -> "well you just write a unit test for your function first, and then code it".

My functional requirements come in the form of "Given publisher A in Country B and contract requirements C the service must return D for a subset of products E", for multiple combinations of A, B, C, D, and E. And where each of those may and will require lookups in external services and databases.

Re:I was saved by Test Driven Development by cratermoon on Saturday February 11, 2023 @10:31PM (#34759417)
Here's the thing. When I'm rapidly prototyping and exploring, I'm constantly asking questions and making hypotheses about what the thing I'm exploring will do when I turn various knobs and pull different levers. Part of any good process of making guesses and comparing the expected and actual results is making notes about the test and results. Those notes could be written in natural language, and many notes should start there, but why not formalize the guesses and discoveries and write them as executable, reproducible, tests?
Re:I was saved by Test Driven Development by LouisSayers on Saturday February 11, 2023 @11:01PM (#34759582)
Agreed.

Essentially if you don't have an existing code structure in place, TDD will be very confusing and you'll end up tangling yourself into a coding mess.

On the contrary, if you have a structure in place and need to ensure the correctness of the code you're about to write, it is incredibly useful.

Building my own stuff, I hardly ever write any tests tho as in the early stages of a project, testing will slow you down as the tests are essentially locking your code in. For me personally, I prefer to have my code be more fluid so I can rapidly change it without having the overhead of having to write and rewrite the corresponding tests.

When I come across some tricky logic tho I will definitely crank out some tests as it actually helps to create the solution.

As usual context is key to knowing whether TDD (or even testing) is appropriate or not.

Re:I was saved by Test Driven Development by lnxg33k1 on Sunday February 12, 2023 @12:44AM (#34760205)
Not really, TDD helps you to think about what you're writing and even when you're prototyping from scratch gives you a good overview of the complexity of what you are writing, because you can't just finish a method that is few tens of lines long then try to test it, you write small test case, then you evolve the method that is being tested by the unit test and so on, and you can see when it's becoming too complex, I think some people just is not used to small methods whose responsibilities are well thought beforehand and just try to be a cowboy, TDD is about the quality of the code, the clarity of code, mantainability of code, as unit test, not necessarily the correctness of code in production
Re:I was saved by Test Driven Development by lukashrb on Sunday February 12, 2023 @01:44AM (#34760523)
> but it gets in the way when you're rapidly prototyping and exploring.

If you prototype a new features for an existing system, a test let you execute only the code you actually need. This will shorten your feedback loop and allow you to iterate faster. Refactoring a test is fine. Writing a BS test just to explore a solution is fine. In my experience following a test lead practice will help you build a simpler system which will be easier to maintain.

Re:I was saved by Test Driven Development by wmwragg on Sunday February 12, 2023 @02:47AM (#34760801)
I tend to view TDD as a form of coding using a REPL, like in Lisp, where you can easily test chunks of your code as you go along. The main difference is that you leave behind your tests so they can be used in the future.
Re:I was saved by Test Driven Development by onion2k on Sunday February 12, 2023 @02:12AM (#34760642)
TDD can be great if you know exactly what you're building and what you must test for, but it gets in the way when you're rapidly prototyping and exploring.

It sounds like you're making the mistake of thinking that TDD means writing the tests first. That's not quite right but it's understandable that lots of people believe it given the way TDD is usually discussed.

In TDD you should be writing one test first. Then you write enough code to make that test pass. Then add a second test. Get that to pass. And so on. If you realise you missed something you can delete tests that don't make sense any morejuat like you delete code that doesn't make sense. In essence TDD is about writing the tests in parallel to writing the code instead of writing them afterwards.

You can easily prototype and experiment with TDD because you're never much further ahead with your tests.

TDD is faster than writing tests later because your code has to be testable throughout the process. Writing tests later often means you need to refactor or rebuild to make it testable, which is a waste of effort, even in a prototype.

Re:I was saved by Test Driven Development by rolisz on Sunday February 12, 2023 @02:19AM (#34760667)
But what test do I write? What does the poorly documented 3rd party API return? Is the test failing because there's an edge case in the API or because my code is wrong?
Re:I was saved by Test Driven Development by sbysb on Sunday February 12, 2023 @02:21AM (#34760676)
The very first test can be a simple "Did my HTTP request receive a response?". Then you can build on "Does this HTTP response have this value I need".... etc

The way I have always gone about TDD is just that I am testing the code I am writing by running the test, not from the main entrypoint of the application. The things you would log and look for when you run the application you instead validate with an `assert()`. Then once you have finished developing, you do a single pass verification from main and you have both a test and a function written

Re:I was saved by Test Driven Development by lcw on Sunday February 12, 2023 @02:54AM (#34760834)
Do you see how playing this game of writing iterative tests would actually provude slower learnings than prototyping a call to an API and receiving a result that would inform a series of tests that would be much more informed?

It's just as easy to be inefficient writing useless/bad tests than it is writing bad code. I think every developer should do TDD in their career, because it will make you think about the testability of the code. That said once you understand how to write testable code than I think it makes sense to be flexible on whether you write the test first or take a crack at an implementation.

The argument that you should have a good understanding of requirements I think leads to a lot of analysis paralysis, and isn't a practical method for building novel things in a timely manner.

Re:I was saved by Test Driven Development by Jarwain on Sunday February 12, 2023 @03:38AM (#34761112)
I think the key distinction here is "run my new function through the entry point of the application" vs "run my new function from a test". You leave out whatever startup time is involved from the rest of the application to get to that function, & you have a tiny feedback loop to test the function.

You have your test which gets the response, and you can still log the output to see what it looks like. It's a little slower at first since you have to write the test, but I'm not sure I see a big distinction vs "have a small script that sends the request".

Re:I was saved by Test Driven Development by atoav on Sunday February 12, 2023 @04:04AM (#34761262)
> Do you see how playing this game of writing iterative tests would actually provude slower learnings than prototyping a call to an API and receiving a result that would inform a series of tests that would be much more informed?

And do you see that in many examples just coding ahead without conaidering how to test the code later will in many cases leave you with code that is such a pain to test that you start avoiding the tests because "they take too much time".

If you start with the test the question of "how can this be made testable" is unavoidable.

I am not dogmatic here. If you are operating on the level of "I am happy if anything works at all" then starting with writing a test is probably a bad idea, better try things out first, scrap the whole thing once you understood what you wanna do in which way and then start writing the tests.

For a lot of adhoc code test driven development doesn't make too much sense, but if the stuff you write could ruin someones day (or life) maybe it is the way to go.

Re:I was saved by Test Driven Development by Manfred on Sunday February 12, 2023 @02:57AM (#34760853)
You start by allowing external requests from inside the test and automate your exploration of the API. Then you slowly evolve it to mock or stub the elements that interact with the external system so you can run your tests in isolation.
Re:I was saved by Test Driven Development by onion2k on Sunday February 12, 2023 @05:09AM (#34761608)
If you believe the answer to any of those questions is "So I shouldn't write tests, right?" then you must build some very flaky apps.
Re:I was saved by Test Driven Development by maest on Sunday February 12, 2023 @05:50AM (#34761836)
There is a difference between writing tests once you're done with your exploratory phase and know what you have to code up and doing full blown TDD from the beginning.
Re:I was saved by Test Driven Development by dmitriid on Sunday February 12, 2023 @03:03AM (#34760897)
Kent Beck (yes, the Kent Beck): "I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence..." https://stackoverflow.com/questions/153234/how-deep-are-your...
Re:I was saved by Test Driven Development by onion2k on Sunday February 12, 2023 @03:14AM (#34760957)
I think what Kent is saying there is that he knows which tests he can skip writing because they provide little value. If you're as experienced and knowledgable as Kent then that's probably true.

For the rest of us it's better to write too many tests than too few. You might waste some time, but that a smaller problem than accidentally skipping a test that was actually useful and pushed your code in a better direction than you'd have gone without it.

Re:I was saved by Test Driven Development by dmitriid on Sunday February 12, 2023 @04:01AM (#34761241)
> For the rest of us it's better to write too many tests than too few. You might waste some time

That's a very bad attitude that is all but enforced by TDD proponents.

It's very easy to see which tests are useless and which are not. So you end up with people writing thousands of unit tests and little-to-no integration or functional tests because:

- "TDD told me so", and

- Testing frameworks are written by people who adhere to the same philosophy

Whereas there's very little utility in those "too many tests" while they give you a very false sense of security.

Re:I was saved by Test Driven Development by onion2k on Sunday February 12, 2023 @04:56AM (#34761548)
It's very easy to see which tests are useless and which are not.

For trivial things, maybe, but then people start applying the 'rule' to non-trivial things "because it's easy" and they get it wrong. All I'm saying is that until you are a Kent Beck level expert it's safer to err on the side of caution. When you have a ton of experience and knowledge you can do what you like.

Also note that I said its better to write too many tests than too few - that still doesn't mean you should test everything, or that you have to test trivial things. It just means when you're not 100% sure it's safer to write a test.

Re:I was saved by Test Driven Development by dmitriid on Sunday February 12, 2023 @01:29PM (#34765360)
> For trivial things, maybe, but then people start applying the 'rule' to non-trivial things "because it's easy" and they get it wrong. All I'm saying is that until you are a Kent Beck level expert it's safer

Most of the stuff we do is trivial [1]. Unfortunately no one really teaches testing or shows what needs ot be tested. Hence the prevalent "write hundreds of tests perhaps some of them will actually be useful". And most available examples, and most available advice is that: write many many useless tests.

I have a perfect example from the Java + Spring world. It's common to have a Controller + Service + Facade + external service client definition.

So I've seen countless times when tests are written as follows:

- external service client is mocked, multiple unit tests for the Facade to make sure it returns data

- Facade is mocked, multiple unit tests for Service, to make sure data is returned

- Service is mocked, mutiple unit tests for Controller, to make sure ata is returned

They are all the same tests, and can be easily deleted and replaced by a single suite of:

- external service is mocked, including invalid responses and timeouts. The actual rest service provided by the app is tested for all the scenarious that were quadruplicated in the unit tests above

You don't need to be a "kent Beck level expert" to do that. However, almost literally nothing teaches you to do that or helps you write those tests. Almost literally everything is hardwired to write small useless unit tests.

[1] Except UIs. I have no idea how to test UIs, and I don't think anyone does :D

Re:I was saved by Test Driven Development by heretoo on Sunday February 12, 2023 @03:46AM (#34761146)
> It sounds like you're making the mistake of thinking that TDD means writing the tests first.

Actually, Kent Beck, who coined the term TDD, has the following to say in his book "Test Driven Development":

"In Test-Driven Development, we Write new code only if an automated test has failed"

This implies that in TDD the test is written first.

Regardless of any definition, test-first and test-last are just two ends of a scale we can use.

Re:I was saved by Test Driven Development by erik_seaberg on Sunday February 12, 2023 @03:52AM (#34761189)
He’s saying to write a test before its code, but don’t write all the tests before any code.
Re:I was saved by Test Driven Development by dmitriid on Sunday February 12, 2023 @01:34PM (#34765422)
He's also saying "I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence..." https://stackoverflow.com/questions/153234/how-deep-are-your...
Re:I was saved by Test Driven Development by dgb23 on Sunday February 12, 2023 @05:14AM (#34761641)
I have to disagree with that last statement.

The code I write initially can often be very dumb, repetitive and procedural. I often have to discover and see what the data structures really look like, what the public API looks like, what I leave in code and what I want to be data driven etc.

At this stage it makes little to no sense to write tests first. All I need is to see the output, which might change dramatically during development. It’s a very fast, direct way of programming, with a lot of copy pasting and long parameter lists.

When the code is laid out like that, then I can see where it’s going. I see the repetitions, the edge cases etc. Now it starts to make sense to design the data structures, give declarative names, find the right abstractions. Now I can move forward with tests.

Re:I was saved by Test Driven Development by mettamage on Sunday February 12, 2023 @05:51AM (#34761843)
That first phase is what I call “playing around”. A necessary phase. When you explore/play one shouldn’t test because one is learning.

Testing (aka evaluation) should happen when one knows the routine + outcome you want to evaluate that routine one.

Whether it’s code or improving your singing or learning in school. This generalizes to all of that, IMO

Re:I was saved by Test Driven Development by pydry on Sunday February 12, 2023 @05:55AM (#34761862)
>The code I write initially can often be very dumb, repetitive and procedural. I often have to discover and see what the data structures really look like, what the public API looks like, what I leave in code and what I want to be data driven etc.

I do this plenty after writing a new (high level integration) test.

Usually I have a fairly clear idea of what the public API I want should look like before I code. A test that calls that makes a good test. I might tweak it when coding the implementation, but probably not by much. The integration test doesn't care what kind of data structures you use.

>At this stage it makes little to no sense to write tests first. All I need is to see the output, which might change dramatically during development.

I frequently write the test first for this type of code. Where I'm expecting the test to change the output I program it to temporarily overwrite the output with the results of the program, eyeball the result and commit it all together if it's correct.

The self rewriting pattern is especially good for API tests or command line tests - where the output is a blob of JSON or a wall of text.

Re:I was saved by Test Driven Development by randomdata on Sunday February 12, 2023 @04:05AM (#34761270)
I disagree. TDD shines most when you don't know what you are building. It allows you to to test hypotheses without needing to over-implement a system to be able to begin to gather data about your design.

In fact, if you know exactly what you are building, how could tests drive your decisions? You have already made up your mind. If you know what you are building you can simply implement it straight away without the data gathering phase.

Re:I was saved by Test Driven Development by ChrisMarshallNY on Sunday February 12, 2023 @07:15AM (#34762270)
> if you know exactly what you're building and what you must test for

This is the main issue that I have with TDD. I use a methodology that I call "Evolutionary Design,"[0] and TDD won't work for that.

But I think they have the right idea, in emphasizing the need for extreme Quality. I like to achieve this, by using test harnesses, as opposed to unit tests[1].

[0] https://littlegreenviper.com/various/evolutionary-design-spe...

[1] https://littlegreenviper.com/various/testing-harness-vs-unit...

Re:I was saved by Test Driven Development by deterministic on Sunday February 12, 2023 @09:56PM (#34769642)
Eh? You can’t prototype or explore if you have no idea what you are building. You obviously have some idea (3rd person shooter or accounting system?) but might not be sure how to organize your code.

However well written tests are independent of how you organize your code or any other implementation details. So tests are great even when you are just prototyping or exploring. You can rapidly change your code and have the tests ensure that it still works the same way. And the end result is a fully tested, easy to maintain code base. What’s not to love about that?