2

I'm a long-time Java developer who's trying to make up his own mind about TDD.

I've used JUnit since I first discovered it in 1998. I use it regularly, but I don't subscribe to "write the test first". My coverage will range from 60-90%; I'm not compulsive about driving it to 100% in every case.

I read Extreme Programming when it first came out. I liked the ideas, but I've always been troubled by the dogma that accompanied it. "If it doesn't work for you, you're doing it wrong" has never sat well with me.

I found Martin Fowler's Is TDD Dead? It's good to see people like that debating the question.

I went over to Linus Torvald's github repo for Linux. Browsing quickly through the source tree revealed no evidence of anything like TDD. I saw no test code of any kind.

Can I conclude that TDD is not as common for functional and procedural languages as it is for object-oriented code?

duffymo
  • 3,032
  • 16
  • 23
  • 8
    No. it means with Linux, YOU are the test. – Euphoric Aug 18 '16 at 12:30
  • Thanks, but I don't know what that means. Is "you" the developer contributing to the kernel? Or is it the end user trying out the installed distro? – duffymo Aug 18 '16 at 12:31
  • 1
    Sounds like the answer is that TDD is impossible unless you have all possible hardware combinations available. Thanks for the link. – duffymo Aug 18 '16 at 12:35
  • Whether you write the test first or after isn't important. TDD is a good approach for some, as it encourages test creation. If you have the discipline to write tests after though, then it may not work for you. I use it, but would never claim it's the only valid approach to creating unit tests. – David Arno Aug 18 '16 at 12:37
  • 4
    As for whether TDD is as popular for functional programming: it's not. The functional approach of pure and higher order functions and immutability by default tends to create very loosely decoupled code. So changing one aspect of the code is unlikely to affect other parts. Instead, [property-based testing](http://blog.jessitron.com/2013/04/property-based-testing-what-is-it.html) is popular for testing such code. – David Arno Aug 18 '16 at 12:46
  • Thank you, @DavidArno. I appreciate your insight. I'm not a functional programmer myself. – duffymo Aug 18 '16 at 12:53
  • I realised that with TDD you have to - in order to write your test - design the API to your code _up front_. This is invaluable! – Thorbjørn Ravn Andersen Aug 18 '16 at 13:23
  • Let's not confuse TDD with unit testing. Unit testing is demonstrably and provably a colossal waste of time and money. Unit testing is responsible for more failed projects than you would care to count. It also rarely results in higher quality applications and services. What you want is fully automated integration (functional) tests, ideally mocking only remote (out of process) services. – Rick O'Shea Feb 25 '19 at 07:37
  • Does linux kernel have no pure (or with at least common dependencies) functions anywhere? Surely these could be unit tested. I understand how triggered system programmers get with the phrase Dependency Injection, but the fact that they even bring up hardware combinations makes me think there is misunderstanding on what a unit is. Also, not advocating for 100 or even high test coverage, agree that is a fools errand – Shanteva Aug 05 '21 at 14:07

2 Answers2

2

Can I conclude that TDD is not as common for functional and procedural languages as it is for object-oriented code?

No, you cannot conclude that by looking at a select (very large and legacy) codebase.

Browsing quickly through the source tree revealed no evidence of anything like TDD. I saw no test code of any kind.

These look like test code to me:

https://github.com/torvalds/linux/tree/9256d5a308c95a50c6e85d682492ae1f86a70f9b/tools/testing/selftests

Den
  • 4,827
  • 2
  • 32
  • 48
  • Didn't look deeply enough. Thank you for the link. I'll look at it more carefully. – duffymo Aug 18 '16 at 13:32
  • 1
    consider [edit]ing the answer to show a snippet of code or docs from the referred link at github, in order to make it easier for readers to see what you mean when you say that it looks like test code – gnat Aug 18 '16 at 13:42
2

First of all, TDD was invented a decade after Linux. Even non-TDD unit testing was not common before that, except on safety-critical projects. Second, it's not so much the language that's problematic to unit test as the domain. You basically have to mock out the computer hardware. A lot of the units are at a higher level, so it is doable, it's just not as easy as testing most desktop applications.

Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479
  • I agree on first point: XP was published in 2000, Linux started in 1991. Certainly testing has been done as long as there's been programming. I'm wondering about what form it takes for non-OO languages and what typical practices are. – duffymo Aug 18 '16 at 13:55
  • Automated testing was done before TDD, but it was nowhere near as common as today. Like I said, mostly only safety critical applications. – Karl Bielefeldt Aug 18 '16 at 14:50
  • I used to be a mechanical engineer, so I know what you mean. There's a difference when it comes to avionics, controls, etc. – duffymo Aug 18 '16 at 14:55