google-site-verification=ELxx8BxZxh0FRvrpsz3X3djO2LXa4N0xnq_ADRpt8wA The Power of Test Driven Development (TDD) - Loving Legacy

Episode 22

The Power of Test Driven Development (TDD)

Before I read the book by Kent Beck I was just thinking - pesky tests what are they good for apart from getting in the way? What’s so good about Test Driven Development?

But writing tests isn’t what test-driven development is about. It's actually about designing your code in a way that matches your expectations. It's a powerful technique that, when understood, will transform the way you write code and design software.

NOTES

Kent Beck - Test Driven Development By Example:

https://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530

Github template repository:

https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-template-repository

QUOTES

00:54 - "Because TDD is not about driving development through testing. It's about designing and architecting your application. Through writing tests." [RB]

02:12 - " I find myself going backwards and forwards, making sure I understood every single step and what it meant in that first section." [RB]

03:26 - "And so you carry on writing some more code. And eventually the whole thing becomes a great big ball of string. Lots of code and no tests." [RB]

04:06 - "It's more of a tool for a structured approach to design in the first place. " [RB]

05:07 - "Writing a test helps show that it works. In fact, the word test is really wrong here. It's more of a proof than it is a test." [RB]

06:06 - "Writing code in TDD can initially feel slow and labored but in only a short time, you'll start to notice the benefit." [RB]

06:36 - "This is almost like the reverse Conway maneuver to Goldratt's theory of constraints." [RB]

08:02 - "Whatever we do, there has to be a low barrier to entry for testing." [RB]

08:42 - "And this is the true power of TDD, rather than agonizing over design and architecture and advance" [RB]

09:12 - "TDD is an absolute no brainer" [RB]

Transcript
Richard Bown:

Hi, this is the software that every club, episode 22.

Richard Bown:

I'm your host Richard bound.

Richard Bown:

And this week, I'm talking all about the power of TDD, the

Richard Bown:

power of test driven development.

Richard Bown:

So what's so good about TDD.

Richard Bown:

I think before I read the book by kent back,

Richard Bown:

I was just thinking pesky tests, what are they good for

Richard Bown:

apart from getting in the way?

Richard Bown:

What are they good for?

Richard Bown:

Perhaps for proving we have control of our software.

Richard Bown:

Often we written it.

Richard Bown:

That we have thought about it and decided that we can automatically

Richard Bown:

prove works and is deployable.

Richard Bown:

perhaps the image problem that I've had with the idea of tests in the past.

Richard Bown:

And that there's something that you have to do after coding in order

Richard Bown:

it was to prove that it works.

Richard Bown:

this really isn't what test driven development is all about.

Richard Bown:

Maybe this is a misnomer of TDD test-driven development does not

Richard Bown:

describe what it's all about at all.

Richard Bown:

Maybe instead it should be called design through testing.

Richard Bown:

Because TDD is not about driving development through testing.

Richard Bown:

It's about designing and architecting your application.

Richard Bown:

Through writing tests.

Richard Bown:

There are lots of pluses to this technique and not many

Richard Bown:

drawbacks that I can think of.

Richard Bown:

Because it improves your design helps you get unstuck when you need help.

Richard Bown:

Plus, you've got your test passing and through refactored

Richard Bown:

and your code is written.

Richard Bown:

So you've done everything.

Richard Bown:

Sounds good.

Richard Bown:

Right?

Richard Bown:

So, what does this look like?

Richard Bown:

Sometimes when you're building an application, especially if it's on and

Richard Bown:

off, you're not doing it full time or perhaps you're distracted by production

Richard Bown:

support, or you have to do other things.

Richard Bown:

Perhaps it's an open source project.

Richard Bown:

It can get to a point where you can't see the way forward.

Richard Bown:

Or perhaps it's a case that when you come back to the code, you've

Richard Bown:

forgotten what it's supposed to do.

Richard Bown:

So how can TDD help with this?

Front Right - RODE NT-USB:

If you've read the book, test driven

Front Right - RODE NT-USB:

development, by example, by Kent Beck.

Front Right - RODE NT-USB:

Then at first it might all seem a bit obvious or perhaps a bit daunting.

Front Right - RODE NT-USB:

It's written in a chatty style, which may not make it the most obvious or

Front Right - RODE NT-USB:

easiest to follow in the first instance.

Front Right - RODE NT-USB:

It does require some work.

Front Right - RODE NT-USB:

You should get invested in the story a little bit.

Front Right - RODE NT-USB:

Once you do that and take your time with the exercises, particularly in

Front Right - RODE NT-USB:

the first parts of the book, then it will pay you back for your persistence.

Front Right - RODE NT-USB:

I find myself going backwards and forwards, making sure I understood

Front Right - RODE NT-USB:

every single step and what it meant in that first section.

Front Right - RODE NT-USB:

After a while it clicked for me.

Front Right - RODE NT-USB:

What I saw that this was a really clever way of designing code.

Front Right - RODE NT-USB:

And not just a way of making sure we have good code coverage.

Front Right - RODE NT-USB:

The first part of the book will tell you everything you need to know

Front Right - RODE NT-USB:

about TDD and why it's so powerful.

Front Right - RODE NT-USB:

The second part extends this knowledge without other paradigms.

Front Right - RODE NT-USB:

And the third gives you plenty of great examples.

Front Right - RODE NT-USB:

So, what does TDD do that is so different?

Front Right - RODE NT-USB:

If you're a programmer already, then you'll already be familiar

Front Right - RODE NT-USB:

of being in a coding zone.

Front Right - RODE NT-USB:

Perhaps when you put on your favorite least distracting music and you climb into

Front Right - RODE NT-USB:

your editor and your code excitedly, and you write and you write and write, and

Front Right - RODE NT-USB:

perhaps you test manually as you go along.

Front Right - RODE NT-USB:

But you don't formalize any of those tests, at least not straight away.

Front Right - RODE NT-USB:

You might grudgingly add some tests afterwards, but perhaps they're

Front Right - RODE NT-USB:

quite high level and abstracted.

Front Right - RODE NT-USB:

Perhaps you're kind of trying to scam the testing gods.

Front Right - RODE NT-USB:

And you're not too close to the actual core business or the domain language

Front Right - RODE NT-USB:

at the heart of your application.

Front Right - RODE NT-USB:

Then perhaps you come back to it later.

Front Right - RODE NT-USB:

And you want to get that coding feeling again.

Front Right - RODE NT-USB:

So you jumped straight back in.

Front Right - RODE NT-USB:

So the coding you want that coding high.

Front Right - RODE NT-USB:

And the coding high comes from writing the code, not the test.

Front Right - RODE NT-USB:

And so you carry on writing some more code.

Front Right - RODE NT-USB:

And eventually the whole thing becomes a great big ball of string.

Front Right - RODE NT-USB:

Lots of code and no tests.

Front Right - RODE NT-USB:

And you're not even sure what the original intention was anymore.

Front Right - RODE NT-USB:

So we're coding happily.

Front Right - RODE NT-USB:

And perhaps we end up in a blind alley and have to unpick our work,

Front Right - RODE NT-USB:

or sometimes even start again.

Front Right - RODE NT-USB:

This can feel like we're making progress, but not all the time.

Front Right - RODE NT-USB:

For solo coding, this can definitely happen.

Front Right - RODE NT-USB:

coding as part of a team, then this is less likely to happen

Front Right - RODE NT-USB:

because you do need to justify your code and your design decisions to

Front Right - RODE NT-USB:

othersthat you're working with.

Front Right - RODE NT-USB:

So TDD is therefore perhaps only useful for solo coders you may think.

Front Right - RODE NT-USB:

No, because it's more than it's all for just not getting stuck with a design.

Front Right - RODE NT-USB:

It's more of a tool for a structured approach to design in the first place.

Front Right - RODE NT-USB:

By taking your time with testing and design as you go you make

Front Right - RODE NT-USB:

it clear for everyone who works with the code in the future.

Front Right - RODE NT-USB:

So how does TTD differ from the code first or even a design first approach?

Front Right - RODE NT-USB:

Well, you write the test first and then you were tray one minus the time.

Front Right - RODE NT-USB:

To get passing tests.

Front Right - RODE NT-USB:

So you need to keep passing your test.

Front Right - RODE NT-USB:

As your north star.

Front Right - RODE NT-USB:

And without that indication, you just don't move on.

Front Right - RODE NT-USB:

So immediately TDD imposes a discipline on your coding.

Front Right - RODE NT-USB:

You can't any more, just keep writing code and hope that it works.

Front Right - RODE NT-USB:

You need to prove it to yourself at every single step

Front Right - RODE NT-USB:

. How does this help with your design and

Front Right - RODE NT-USB:

It sounds like it might be boring.

Front Right - RODE NT-USB:

It sounds like you might miss something important.

Front Right - RODE NT-USB:

And you're worrying about tests so how can this help?

Front Right - RODE NT-USB:

This is in fact, one of the key benefits.

Front Right - RODE NT-USB:

Proving the what your building will do.

Front Right - RODE NT-USB:

The thing that you're intending.

Front Right - RODE NT-USB:

Writing a test helps show that it works.

Front Right - RODE NT-USB:

In fact, the word test is really wrong here.

Front Right - RODE NT-USB:

It's more of a proof than it is a test.

Front Right - RODE NT-USB:

It's proving that the thing you've created will do the thing that you say it well,

Front Right - RODE NT-USB:

And the cool thing about this.

Front Right - RODE NT-USB:

Is it the test or proof makes you think more deeply about what the code you're

Front Right - RODE NT-USB:

going to write is exactly going to do.

Front Right - RODE NT-USB:

You're creating a specification for your code to fit into.

Front Right - RODE NT-USB:

And that specification means two things.

Front Right - RODE NT-USB:

When you do write the code, it virtually writes itself.

Front Right - RODE NT-USB:

And when you've written the code, you can tell immediately if it's

Front Right - RODE NT-USB:

working by running the test.

Front Right - RODE NT-USB:

And this covers the first two important mantras of TDD, which are right a test.

Front Right - RODE NT-USB:

Then write the code.

Front Right - RODE NT-USB:

The third important mantra.

Front Right - RODE NT-USB:

And the discipline is refactor.

Front Right - RODE NT-USB:

Ensure that the code is not only doing the right thing.

Front Right - RODE NT-USB:

But doing it in the right way for the rest of your code for the rest of your

Front Right - RODE NT-USB:

design for the future of your application.

Front Right - RODE NT-USB:

Writing code in TDD can initially feel slow and labored but in only a short

Front Right - RODE NT-USB:

time, you'll start to notice the benefit.

Front Right - RODE NT-USB:

Not only because you have a lot of tests.

Front Right - RODE NT-USB:

But also because you'll have an emergent design.

Front Right - RODE NT-USB:

The design itself comes from creating tests and thinking about what your

Front Right - RODE NT-USB:

code would do before you write it.

Front Right - RODE NT-USB:

You're creating your expectations as specifications about what the code can do.

Front Right - RODE NT-USB:

By approaching this through constraints, we help our brain focus on what we intend.

Front Right - RODE NT-USB:

This is almost like the reverse Conway maneuver to Goldratt's

Front Right - RODE NT-USB:

theory of constraints.

Front Right - RODE NT-USB:

We add a constraint, then we solve that constraint by fixing the functionality.

Front Right - RODE NT-USB:

Step-by-step we constrain our domain.

Front Right - RODE NT-USB:

We constrain our functional model.

Front Right - RODE NT-USB:

And we constrain our application to be the thing that we intended to, but I've

Front Right - RODE NT-USB:

never actually described to this point.

Front Right - RODE NT-USB:

Okay, before I get too philosophical about this.

Front Right - RODE NT-USB:

How can we do TDD practically?

Front Right - RODE NT-USB:

Well, it's important that you have a project in source control.

Front Right - RODE NT-USB:

A build system and a way of running tests against your projects.

Front Right - RODE NT-USB:

So with a source control system.

Front Right - RODE NT-USB:

You can baseline all of your changes.

Front Right - RODE NT-USB:

If you don't have source control, then we can't be sure that we're

Front Right - RODE NT-USB:

doing the change we intend.

Front Right - RODE NT-USB:

Secondly, a build system is needed.

Front Right - RODE NT-USB:

Obviously, we need to be able to compile a code to validate

Front Right - RODE NT-USB:

that it's syntactically correct.

Front Right - RODE NT-USB:

And then finally we need to test framework.

Front Right - RODE NT-USB:

Sometimes it's built into the language or natively supported.

Front Right - RODE NT-USB:

Sometimes you need to find a framework that you can use with your build system.

Front Right - RODE NT-USB:

Once you have these three things.

Front Right - RODE NT-USB:

That it makes sense to invest also in some automation that your tests show, you can

Front Right - RODE NT-USB:

run your test manually, but after every change, do you really want to do this?

Front Right - RODE NT-USB:

Perhaps sets up your CI to run tests every time you push.

Front Right - RODE NT-USB:

Also I'm currently working in a rust and with vs code has got a nice feature

Front Right - RODE NT-USB:

where you can just click on the test in the code and run it from the editor.

Front Right - RODE NT-USB:

Whatever we do, there has to be a low barrier to entry for testing.

Front Right - RODE NT-USB:

You want to be writing tests and running tests continuously as you code.

Front Right - RODE NT-USB:

Therefore it needs to be simple and as automated as possible.

Front Right - RODE NT-USB:

So there is some upfront work here to set up your project with a testing framework

Front Right - RODE NT-USB:

but this will pay you dividends when it comes to creating something new.

Front Right - RODE NT-USB:

When it comes to you creating your design.

Front Right - RODE NT-USB:

So perhaps in your source repository, you can even create a template

Front Right - RODE NT-USB:

project that is ready to go.

Front Right - RODE NT-USB:

Again, the barrier to entry, it needs to be really low.

Front Right - RODE NT-USB:

ideally, so that when you have the idea, when you want to try something out new,

Front Right - RODE NT-USB:

you can immediately create your projects and start designing, start creating.

Front Right - RODE NT-USB:

And this is the true power of TDD.

Front Right - RODE NT-USB:

Rather than agonizing over design and architecture and advance.

Front Right - RODE NT-USB:

You can just see what flows from your mind whilst constraining yourself to

Front Right - RODE NT-USB:

focus on one piece of functionality at a time, one aspect at a time.

Front Right - RODE NT-USB:

Right a test.

Front Right - RODE NT-USB:

Write the code.

Front Right - RODE NT-USB:

Refactor.

Front Right - RODE NT-USB:

Red green refactor and then green again.

Front Right - RODE NT-USB:

Piece by piece.

Front Right - RODE NT-USB:

You will create something that not only more closely resembles

Front Right - RODE NT-USB:

what you intended to build.

Front Right - RODE NT-USB:

But also has a great code coverage already built in.

Front Right - RODE NT-USB:

For me, TDD is an absolute, no brainer.

Front Right - RODE NT-USB:

If you've not tried it yet.

Front Right - RODE NT-USB:

Then what are you waiting for?

Front Right - RODE NT-USB:

That's it for this week.

Front Right - RODE NT-USB:

Nice and short and sharp.

Front Right - RODE NT-USB:

I'm in the middle of writing something fun using TDD.

Front Right - RODE NT-USB:

So I need to get back to it.

Front Right - RODE NT-USB:

I wish you luck check out the book until next time.

Front Right - RODE NT-USB:

Goodbye.

About the Podcast

Show artwork for Loving Legacy
Loving Legacy
How to deliver successful real-world IT and software projects

About your host

Profile picture for Richard Bown

Richard Bown

I’m an independent consultant who helps software businesses maximise their legacy investment.