Why TDD leads to cleaner code

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Why TDD leads to cleaner code

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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*
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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. ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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 ;).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Nice one Jcart.. that got a real laugh.

Image
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

I probably didn't get the joke as fast as the others here, but it is funny :)
Post Reply