News for hackers. Stuff that matters.
Re:Why TDD isn't crap by k__ on Tuesday October 31, 2017 @09:59AM (#15593254)
"having test play some part in how the code takes shape"

That is exactly the point why I don't like TDD.

I mean, there are enough constraints that shape the code, why should something artificial like tests shape it too?

With mocking and everything you end up writing code for tests and not code for your problems.

Re:Why TDD isn't crap by testvox on Tuesday October 31, 2017 @10:06AM (#15593293)
If you use mocks extensively in your tests then you probably aren't getting much of the benefit of TDD. Mocks indicate high coupling so using them is just powering though testing your bad design rather then driving good design via testing.
Re:Why TDD isn't crap by efdee on Tuesday October 31, 2017 @11:15AM (#15593821)
How do you handle dependencies of the class under test then?
Re:Why TDD isn't crap by rasmus4200 on Tuesday October 31, 2017 @12:44PM (#15594590)
This is the biggest challenge. What I find works is separating your orchestrations from your data processing.

For example, instead of creating one class that does the network call, and processes the JSON response, break these out.

One class does the network call (keep it extremely thin). Dependency inject stuff in if you want. But view this purely as an orchestration.

if (connectionValid) json = makeNetworkCall anotherObject.processJson(json) <- test separately end

Then, in the other plain old data process class, do you heavy off-by-one edge case testing there.

If the first case, the orchestration, I can write tests like "One make the network call if the connection is valid". But that's all that test would do.

I am at the point now where I actually don't even test that. I assume that is going to work, because it is so simple. That and I generally hate mocking. Way too much coupling. Way too hard to refactor.

Where I do test heavy is on the plain old object side, that has no outside ports or connectors. These are just plain old objects.

I agree with some other comments I have ready here. Mocking is a bitch and I generally don't like it. But it's there if I needed it, so I do use it occasionally.

But it has so many downsides I try not to. Instead I separate the orchestrations from the processing. Unit test the processing (in an integration test sort of way), and try to stick to testing the public APIs of my class, so I am free to change the internals without things breaking.

My 2c. Hope that helps.

Re:Why TDD isn't crap by scruple on Tuesday October 31, 2017 @12:31PM (#15594462)
Would love an answer to this question, as well. I've only come up with DI as a long-term mostly win for this problem and it's something that I grapple with in every code base at work, since we have some microservices and other HTTP API layers that we need to interface with and I want to write testable code.
Re:Why TDD isn't crap by humanrebar on Tuesday October 31, 2017 @10:07AM (#15593306)
Well, you write code for design flexibility. Testing just forces the issue and helps you explore possible design needs. For example, what if the database query you're using isn't suitable any more? Instead of designing around a User table, you start passing around User records. Then your design is more future proof in case you start getting Users from a service or in-memory cache instead of a database.

> ...not code for your problems...

Right. It's not your problem now. It may be your (or your successors') problem in the future.

Is it always worth the effort? Nope. But experience, communication, and good teamwork will help with balancing short-term and long-term goals.

Re:Why TDD isn't crap by discreteevent on Tuesday October 31, 2017 @10:26AM (#15593457)
This is exactly the problem. In general you should not be writing code for design flexibility. Your code should contain the minimum number of abstractions to satisfy its requirements. If flexibility is a requirement right now then that's ok. Otherwise refactor in the flexibility later. But don't make the code flexible for the sake of tests. It vastly over-complicates it for no benefit. Instead write tests as close to the requirements level as possible (ideally most of your tests should be just below the ui)
Re:Why TDD isn't crap by humanrebar on Tuesday October 31, 2017 @12:26PM (#15594428)
> Otherwise refactor in the flexibility later.

I'm saying, with experience, teamwork, communication (including through tests), you'll know when this proposition makes sense or not. It's not universally true that "we'll worry about it later" make sense.

Re:Why TDD isn't crap by MartinCron on Tuesday October 31, 2017 @12:44PM (#15594589)
I don't consider taking steps to make sure the code actually as you expect it to as "artificial" feels pretty essential, to me.
Re:Why TDD isn't crap by sidlls on Tuesday October 31, 2017 @10:32AM (#15593495)
If you're writing lots of mocks, odds are you're writing a lot of unit tests. In my experience these are rarely useful. Most often they are either really testing a compiler's output (indirectly), supplying an ad-hoc and incomplete type checking system to a dynamic language, or more appropriately covered by asserts that will scream when integration tests fail.
Re:Why TDD isn't crap by ISL on Tuesday October 31, 2017 @10:06AM (#15593297)
Because tests tend to make your code do what you originally intended, and keep your attention on the quality of the result.
Re:Why TDD isn't crap by vinaybn on Tuesday October 31, 2017 @10:32AM (#15593493)
Tests are not artificial constraints. They are the guarantees which (should) matter most to your users and stakeholders.
Re:Why TDD isn't crap by efdee on Tuesday October 31, 2017 @11:16AM (#15593830)
I am quite convinced stakeholders couldn't really care less about the guarantees being made by unit tests. Higher level integration tests, maybe.
Re:Why TDD isn't crap by tieTYT on Tuesday October 31, 2017 @04:56PM (#15596482)
I'm not hearing anything negative here. Why is any of this innately bad?

Tests shaping your code is bad, because? Because you already have "enough constraints"? Sorry, I don't get it.