Page 1 of 2

Why TDD leads to cleaner code

Posted: Tue Dec 12, 2006 6:53 am
by matthijs
Ok, I just started with TDD and after writing just a few tests I now understand why all the TDD-gurus keep saying that TDD leads to "better" and cleaner code.

It's such a pain in ** to think about and write those tests, that you'll think twice before adding anything which isn't totally necessary :)

Am I right or what?

Posted: Tue Dec 12, 2006 7:17 am
by feyd
Nah, it's not a pain to write the test. It's a pain to figure out what interface I want, but that was always a problem.

The only time I can see making the tests is a pain is when I have no idea what the output should be for any given method.

Posted: Tue Dec 12, 2006 7:28 am
by sweatje
Not really. If a test is hard to write, that should imply something about the code you are testing. Is there a hard coded dependency in your code which you have to do backflips to get around? If this is the case, writing the tests may allow you to make the decision to pass in the dependency instead of hard coding it, leading towards looser coupling in your code.

Having the complete set of unit tests also allows for refactoring, that is, rewriting your code to eliminate duplication and make it easier to read and maintain. You can't do that with any sort of confidence without a good set of tests as a safety net.

Posted: Tue Dec 12, 2006 7:49 am
by matthijs
I do believe you guys, no worries. I was just kidding. To be honest I think in my case it's my inexperience with TDD why the tests are hard to write :wink:

But I don't mind. At this moment, it would - for me - be a lot easier and faster to hack together a script without TDD. But I do believe that in the end that will lead to messy and buggy code, which is hard to refactor. So I will pursue on this path. I have your book laying open on my desk Jason. And I'll take both your remarks with me.

Posted: Tue Dec 12, 2006 8:43 am
by MrPotatoes
feyd wrote:Nah, it's not a pain to write the test. It's a pain to figure out what interface I want, but that was always a problem.

The only time I can see making the tests is a pain is when I have no idea what the output should be for any given method.
i always have problems with the interfaces. which sucks because they are so nice to have. i usually end up writing alot of class pusedo code and figuing my interface from that. *sigh*

Posted: Tue Dec 12, 2006 8:50 am
by neophyte
Learning how to write tests through SimpletTest or other library with no previous test writing experience :: expect pain.

No pain -- no gain...

No guts -- no glory ...

You know the song. ;)

Posted: Tue Dec 12, 2006 9:47 am
by Chris Corbyn
Yeah TDD does encorage you to spend more time thinking about the interface. It also allows you to keep increasing the complexity of things whilst keeping everything clean and knowing that at each stage you already have a set of tests which (if written well) will reveal problems as they occur.

Sometimes figuring out how to put in code exactly what you want to test is the hard part. Writing the test is easy and becomes second nature when you have an idea for what to do next.

One thing TDD hopefully does is encourage you to refactor often so that you can test small units of code without having to hack 50% of it up in the process :)

I seriously believe the project I'm just about to release would be nowhere near as flexible has I chosen not to use TDD since there are some complex things involved which if you get it working one way, may break when you tweak the code to make it work another way and testing each variation manually each time would take a lifetime :) I'm referring to a tree-branch-node composite structure.

Posted: Tue Dec 12, 2006 10:09 am
by matthijs
Sometimes figuring out how to put in code exactly what you want to test is the hard part.
Yeah, I guess this is what I'm referring to. As I'm just starting with Simpletest, I'm constantly asking myself questions like:
"Ok, this method $foo->save(); . Should I just test if it returns a True result? Should I also test if what has been saved is indeed now in the database? Should I also test if what has been saved to the database is in fact equal to what data I had in the first place?"
etc etc

Posted: Tue Dec 12, 2006 10:19 am
by Jenk
Unit testing helps me write cleaner code because it helps me minimise what methods do, and helps me keep track of those methods in some situations.

Instead of saying "I need to get from A to B" I break it down, by testing that A.1 works, then A.2 works etc. etc.

Isolating Functionality and ultimately removing duplication, is what I'm jibbering on about :) Without unit tests I sometimes end up writing blobs.

Posted: Tue Dec 12, 2006 11:38 am
by Maugrim_The_Reaper
Devnet loves TDD. :lol:

Tests are hard to initially get into. After a few attempts you'll start getting the hand of what your Unit Test library is capable of and then, thinking about your classes in terms of the tests you'll need to write and pass, and how you can make classes easier to test, starts becoming second nature. Once you're infected you may lose all your hair in first stage Test-Infection. But that's a small price for where your coding skills sit once you've conquered unit testing and TDD ;).

Posted: Tue Dec 12, 2006 11:55 am
by Chris Corbyn
Maugrim_The_Reaper wrote:Once you're infected you may lose all your hair in first stage Test-Infection. But that's a small price for where your coding skills sit once you've conquered unit testing and TDD ;).
Doh! Is that what's happened? It's become apparent that I'm beginning to bald at 23 years old over the past month or so :( I've started wearing a hat most of the time and keep my hair short :cry: 8O

Maybe if I stop testing it will grow back :?:

(d11 isn't joking about the young baldness)

Posted: Tue Dec 12, 2006 11:57 am
by John Cartwright
d11wtq wrote:
Maugrim_The_Reaper wrote:Once you're infected you may lose all your hair in first stage Test-Infection. But that's a small price for where your coding skills sit once you've conquered unit testing and TDD ;).
Doh! Is that what's happened? It's become apparent that I'm beginning to bald at 23 years old over the past month or so :( I've started wearing a hat most of the time and keep my hair short :cry: 8O

Maybe if I stop testing it will grow back :?:

(d11 isn't joking about the young baldness)
Mock your hair. That way you can keep testing and pretend to have hair.

Posted: Tue Dec 12, 2006 2:40 pm
by Chris Corbyn
:lol:

Posted: Tue Dec 12, 2006 5:20 pm
by feyd
Nice one Jcart.. that got a real laugh.

Image

Posted: Wed Dec 13, 2006 12:46 am
by matthijs
I probably didn't get the joke as fast as the others here, but it is funny :)