I haven't shared this on HN but I saw this on another forum and thought this was a really fantastic example of what you can end up with, in the real world , when you are driven by tests. Real-world example: https://i.redd.it/lwin56fisdsz.png It doesn't look like a joke to me. It only works over integers so the code is absolutely correct. It also strikes me as the kind of convoluted logic that someone took a really, r…
Are there any proponents of serious testing or tdd that don't also promote code review? Why would tests make this code more likely? If anything, I am more confident that I can change it to be better if I have a test suite.
Code like this is basically only possible through testing, because the programmer doesn't understand why it is working: it says so in the comment, which I believe.
So if it weren't written against testing (manual or automatic) it simply couldn't be written like this.
There are a lot of really broken designs that "work". Testing gets you "working" code. Often you could do much better starting with correct code and then adding testing afterward - which I believe is not the essence of TDD. TDD is about driving the writing of code by tests, and I believe the example I shared is one of these bad outcomes. (There are others.)