4 ms·
> "you think is correct". > `add` with `pass` or `return 9` is somehow deeply irritating Similar to others, I understand this feeling. And I would urge you t
by mpweiher 8y ago
> "you think is correct".
> `add` with `pass` or `return 9` is somehow deeply irritating
Similar to others, I understand this feeling. And I would urge you to get over it.
First, technically returning 9 for a test of add(4,5) is correct. Your tests define what the function does, not the name. If you are thinking "a correct add adds all numbers", then that isn't wrong, but it is bringing your tacit world knowledge into the code without that knowledge being expressed in the code. And that's Not a Good Thing™.
It also seems to me that what you meant here was not "correct" (because it is correct), but "general". And that's one of the points of XP/TDD: as programmers we have a terrible tendency to over-generalise. Really horrible. "Write only the code to make the test pass" works against that tendency (and also has the nice gamification effect mentioned elsewhere).
The other point is when to generalise. The first answer is "not", the second is "not yet". Generalise only when you have several instances, otherwise your generalisation will, what's the technical term, suck.
Last not least is how to generalise. TDD/XP essentially say that you generalise only as a refactoring, that is a behavior-preserving transformation.
So when you add the add(10,2) test case, the really pedantic way of doing it would be to handle the extra case with an if-check, then once the additional test passes refactor the add method to handle all those cases with less code, by removing the case-handling and doing arithmetic.
What that means is that you separate the coding that gets the tests to pass from the more challenging bits of coding where you need to generalise, with the latter always under full test coverage.
It's a really nice way to make progress.
And of course: use your own judgement. If things get to tedious, it's OK to leave out steps. At some point you will probably find that you are running into problems, at that point you want to slow down and re-introduce more of the intermediate steps.
- ramblerman 8y ago> otherwise your generalisation will, what's the technical term, suck. Linus Torvalds didn't write the linux kernel or Git in this manner and ended up pretty well. I get the idea behind TDD, and have used it successfully in areas where indeed the business case was very unclear. But to drink the Uncle Bob Koolaid and pretend this is now the only way to do things is lacking in creativity, and wisdom imo.
- mpweiher 8y ago> Linus Torvalds I am not Linus. Maybe you are. The reason I am such a fan of this approach is that I wrote software without it for a long time (Objective-C pre-processor/runtime, Postscript interpreter + various RIPs + other pre-press stuff, content mgt. system when that was still a thing...), and the difference is...hmm..let's say "significant". Without this sort of approach, you get that rush of making super progress quickly, of holding the entire state in your head and being just the coolest cat on earth. It just doesn't last. Things start deteriorating, you fix a bug only to have another one pop up, after a while of this you get a sense of deja-vu and notice you've been going around in circles. Pretty soon you are stuck. When I was put in charge of the CMS, management wanted to know when we would ship the next version. I couldn't honestly tell. With a TDD approach, you get less of that rush, less of the "I am the greatest". What you do get is steady progress without backtracking. > drink the Uncle Bob Koolaid You shouldn't drink anyone's Koolaid, not mine and certainly not Uncle Bob's. For your reference, I was doing this long before Uncle Bob. At least I remember sparring with him on comp.object, when he was still claiming that "OO is just function tables" and not a peep about TDD from him.
- ocharles 8y ago> First, technically returning 9 for a test of add(4,5) is correct. Your tests define what the function does, not the name. If you are thinking "a correct add adds all numbers", then that isn't wrong, but it is bringing your tacit world knowledge into the code without that knowledge being expressed in the code. And that's Not a Good Thing™. How do you feel about just starting with tests that are actually more of a specification? For example, I'm starting to feel that unit tests aren't something we should be writing, but are something we should be generating. We should be writing things like QuickCheck specifications. So the first test I'd probably write is: given x. y. add(x, y) == x + y This is a specification that is essentially tested against a "model" (native addition, in this case).
- mpweiher 8y ago> How do you feel about just starting with tests that are actually more of a specification? Tempted and conflicted. Yes, it's tempting, because "yay, more general and comprehensive", the mathematician inside me says. On the other hand, the beauty of these sorts of tests is often precisely their concreteness/specificity. Tests should be so simple that their correctness is obvious by inspection. (One of my problems with specifications was always that to me it was no clearer that the specification captured what I wanted than that the code did). The other side of the equation is that although theoretically such concrete tests are seriously flawed, because they only test one set of values, in practice that is actually usually quite sufficient. > we should be generating Again, sounds very reasonable, but now you have to trust (a) your generator and (b) your specification of those tests. I'd rather have a concrete test. Now I do think there is some use for this sort of thing, for example in ferreting out edge cases that you might otherwise miss. I wouldn't want to rely on it.
- kazinator 8y agoGeneralizing and naming is the purpose of functions. An add that only handles 4 and 5 need not exist; add(4,5) gets replaced with 9 and that's that. If I'm looking at a complex expression, it's easier to convince myself that it's right if it contains 9 than if it contains add(4,5). Why would I write an add(4,5) that fails and has to be repaired, if I can write a 9 that doesn't fail? It could only be because I'm anticipating a future generalization. Oh, and can write a 9 that doesn't fail? That goes against TD; shouldn't I first break the lexical analyzer of my language so that the 9 constant goes red? Then repair things, so the 9 test goes green? How can you call it TDD if you're relying on non-TDD pieces that work without being covered by tests that previously failed?
- sorokod 8y agoAt what point do you generalize to doing arithmetic and what makes that point better then any point beforehand? What is it that you learned from (4,6) and (10,2) that you didn't know before?
- mpweiher 8y agoThere starts to be duplication in the implementation that you can eliminate by refactoring.
- sorokod 8y agoRefactoring is not (necessarily) generalization. I am interested in how you decide you have seen enough examples to generalize (to actually doing arithmetic in this case) and what makes you trust your judgment at that point while rejecting your judgment initially.
- mpweiher 8y agoOf course, arithmetic is a somewhat silly example, because we generally tend to have arithmetic built in, and we all know how arithmetic works. "Refactor to remove duplication" is absolutely a standard step of TDD. How you do it is up to you. This is not an automated process, it requires (design) skill and good judgement just like any other programming. I recall once seeing Beck look at two loops that were quite dissimilar: they had different for structures, and different contents, which is pretty much nothing duplicated except the word "for", and the fact that they were looping - differently - over the same collection. He changed the second loop to loop the same way the first one did. This required changing the body of the loop to skip over the items toward the end of the collection, since the previous version only did the front of the collection. Now the for statements were the same. "Well, gotta eliminate that duplication, he said, and moved the second body into the first loop and deleted the second loop entirely. Now he had two kinds of similar processing going on in the one loop. He found some kind of duplication in there, extracted a method, did a couple of other things, and voila! the code was much better. http://wiki.c2.com/?OnceAndOnlyOnce http://wiki.c2.com/?OnceAndOnlyOnce What this approach does is move most of this type of tricky design work to a phase of development when you are not also trying to figure out what the program is trying to do or how to do it. Instead, while you are doing this design work, the behaviour of the program remains fixed and you have tests to make sure that's actually the case. Having the code remain functionally identical (in terms of the tests) is a lovely safety net to have while you are doing fancy coding acrobatics. If you find you're in a dead-end, revert, check that the tests are still green and try again, with the knowledge you've just gained. I guess this is something that older developers are more attuned to, because they have been humbled by code enough to appreciate the limitations of their cognitive faculties. > rejecting your judgment I trust my judgement, always, and am wary of my judgement, always. But I also listen to the code, and I just don't "remove duplication" until there actually is duplication. http://wiki.c2.com/?YouArentGonnaNeedIt http://wiki.c2.com/?YouArentGonnaNeedIt https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming) https://en.wikipedia.org/wiki/Rule_of_three_(computer_progra... https://blog.codinghorror.com/rule-of-three/ https://blog.codinghorror.com/rule-of-three/
- nimblegorilla 8y ago> First, technically returning 9 for a test of add(4,5) is correct. Your tests define what the function does, not the name. You are technically correct that testing "add(4,5) == 9" is valid. However, everyone already knows that an "add(a,b)" function which always returns 9 is broken. > So when you add the add(10,2) test case, the really pedantic way of doing it would be to handle the extra case with an if-check Saying an if-check is the correct way to refactor does a disservice to TDD practitioners everywhere. Implementing a trivial add function via TDD is just bad. It would have been far better to choose something slightly more complicated such as sorting or fizzbuzz.