Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #40966

TDD can be useful even when it doesn't achieve what you want

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject TDD can be useful even when it doesn't achieve what you want
Date 2016-02-15 21:49 +0000
Organization Netfront http://www.netfront.net/
Message-ID <n9th55$slr$1@adenine.netfront.net> (permalink)

Show all headers | View raw


As a contrast to the recent "TDD is harmful" thread, I would like to
recount something I thought of recently in regards to a situation
where TDD doesn't actually achieve what you want, but can still make
your code better regardless.

Suppose you have a problem like this: You have made a small class
that opens a file (for example an image file) and does something with
it. One of the things you want to test is that the class actually ends
up closing the file (at the latest when it's destroyed). How would you
test this?

Since you are TDD-conscious, mock objects come to you naturally.
Therefore the solution seems to be clear: You have to make a mock
object that behaves like a file handle. To do this, you have to change
the class so that it takes a more abstract file handle than eg. a FILE*
or a std::istream. Once this is done, you can easily create a test to
make sure that your file-handling class ends up eventually closing the
file (by creating a mock file handler object that makes this check and
giving it to the class.)

However, there's a problem here: How do we now test that this abstract
file handle (which itself uses eg a FILE* or a std::istream) ends up
closing the file (at the latest when it's destroyed)? We realize that
all we have done is just transfer the problem to another class, and we
have ended up in an ouroboros situation. How could we test this? Create
yet *another* abstract file handle which we can mock? That wouldn't make
much sense since we just did exactly that, and it didn't really help.
We would just be in a vicious cycle.

So TDD didn't actually help us solve the actual problem we were trying
to solve.

*However* TDD *did* help us, in a natural way, to make our original class
better. That's because now it's more abstract and flexible. We have
changed it from being hard-coded to use a standard file handle into
being abstract, which makes it more flexible. (After all, it now supports
other types of input for the data it needs to read. We can now make
all kinds of abstract "file handle" objects that feed it data from
whatever other resource that might not be a file.)

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

TDD can be useful even when it doesn't achieve what you want Juha Nieminen <nospam@thanks.invalid> - 2016-02-15 21:49 +0000
  Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-15 21:56 +0000
    Re: TDD can be useful even when it doesn't achieve what you want Juha Nieminen <nospam@thanks.invalid> - 2016-02-16 09:09 +0000
  Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-15 23:59 +0100
    Re: TDD can be useful even when it doesn't achieve what you want Juha Nieminen <nospam@thanks.invalid> - 2016-02-16 09:12 +0000
      Re: TDD can be useful even when it doesn't achieve what you want Öö Tiib <ootiib@hot.ee> - 2016-02-16 03:08 -0800
      Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-16 17:16 +0100
        Re: TDD can be useful even when it doesn't achieve what you want Juha Nieminen <nospam@thanks.invalid> - 2016-02-17 11:26 +0000
          Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-17 16:55 +0000
            Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 08:00 +1300
          Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-17 18:57 +0100
            Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 08:02 +1300
              Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-17 20:11 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 08:23 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-17 20:32 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 08:54 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Cholo Lennon <chololennon@hotmail.com> - 2016-02-17 17:19 -0300
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 10:28 +1300
                Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-17 21:28 +0000
            Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-17 21:08 +0000
              Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-17 23:08 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 11:19 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 08:33 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 20:42 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 20:45 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 08:58 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 23:24 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 08:58 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 21:17 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 09:22 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 21:28 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 09:39 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 23:22 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 11:57 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-19 09:29 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 22:00 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-19 10:10 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 23:15 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-20 11:22 +1300
                Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-19 22:39 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-20 00:20 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-20 13:16 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-20 09:07 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-20 21:17 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-20 09:53 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Öö Tiib <ootiib@hot.ee> - 2016-02-20 04:24 -0800
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-20 14:08 +0100
                Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-21 01:19 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-21 09:29 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Paul Sinnett <paul.sinnett@gmail.com> - 2016-02-21 08:08 -0800
                Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-22 05:07 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-22 19:44 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-23 08:58 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-22 20:13 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-23 09:19 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-22 20:21 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-22 21:18 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Juha Nieminen <nospam@thanks.invalid> - 2016-02-23 09:14 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-23 22:21 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 17:33 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-23 19:28 +0100
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-24 08:43 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 20:13 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Gareth Owen <gwowen@gmail.com> - 2016-02-23 20:53 +0000
                Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-24 17:12 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 18:05 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-24 09:53 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Gareth Owen <gwowen@gmail.com> - 2016-02-23 21:09 +0000
                Re: TDD can be useful even when it doesn't achieve what you want David Brown <david.brown@hesbynett.no> - 2016-02-23 22:22 +0100
                Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-21 01:21 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Ian Collins <ian-news@hotmail.com> - 2016-02-18 21:24 +1300
                Re: TDD can be useful even when it doesn't achieve what you want Vir Campestris <vir.campestris@invalid.invalid> - 2016-02-18 21:27 +0000
                Re: TDD can be useful even when it doesn't achieve what you want legalize+jeeves@mail.xmission.com (Richard) - 2016-02-18 17:48 +0000
                Re: TDD can be useful even when it doesn't achieve what you want Wouter van Ooijen <wouter@voti.nl> - 2016-02-18 19:04 +0100
          Re: TDD can be useful even when it doesn't achieve what you want Vir Campestris <vir.campestris@invalid.invalid> - 2016-02-18 21:30 +0000

csiph-web