What can possibly TDD, BDD or adhering to SOLID have to do with a lady that is funny about her tea? Read on and I promise the answer will not be disappointing to you.
But first let me give you some context about what lead me to write this article. This last weekend I watched the third episode from http://www.cleancoders.com/. It’s about how you should go about creating your functions/methods, how big they should be, how you can actually have a “class hidden inside a method” (if you can watch it, it is very entertaining, it’s not free though).
Monday morning, in The Code Project’s “The Daily Insider” an article named “Readable code – and the long lost secret of how to achieve it” was referenced. That link points to a video that is a rebuttal of what is advocated in Uncle Bob’s video in the cleancoders. It tries to refute the idea that you should “extract ‘till you drop”, which means that you should try to make your methods smaller until you cannot make then any smaller. Her argument is that this actually makes code harder to read.
Usually what people do in a situation like this is to bring their own personal experience and affirm, certain of themselves, that their view is the right one (just read the comments on the readable code video). The discussion that ensues usually leads nowhere.
Now comes the story of the lady that was peculiar about her tea. She liked her tea with milk, but you had to put the milk first and then the tea, or else she said, the tea would be ruined. Your reaction (as mine was) is that it is impossible to tell the difference. But how can you prove it?
Well, if you give the lady one cup of tea with milk added first, she has 50% change of getting it right. If you give her two cups of tea (with milk or tea added first randomly), she now only has 25% chance of getting it right. If you were to give her 5 randomly filled cups of tea with milk, her chance of getting them all right is around 3% (we usually say that a result is significant if the chances of getting it randomly are <5%).
Well, this story is real, and she was really able to tell the difference (I don’t actually remember which one she preferred, milk with tea or tea with milk, but you can look the story up in this amazing book from Andy Field: “How to design and report experiments”).
Apparently she was able to correctly classify 8 cups of tea (the chances of she getting that right by chance are <1%) (check http://en.wikipedia.org/wiki/The_Lady_Tasting_Tea and http://en.wikipedia.org/wiki/Lady_tasting_tea).
That’s how you settle a dispute, try arguing with that. You can’t. Why don’t we apply this same reasoning to the claims that BDD, TDD, etc, lead to better code. I’m not saying that they don’t, I actually believe they do, but if you test them scientifically you can actually measure how much better they are, you can compare them, you can extract other useful information, for example, how much more time one takes in relation to the other and if the increase in immediate quality is worthy of that cost.
It’s a very hard thing to get right (http://blog.jove.com/2012/05/03/studies-show-only-10-of-published-science-articles-are-reproducible-what-is-happening/) but we should really think about doing it more often in our domain.