 Hi, thanks for coming. I just want to get a quick gauge. I'm gonna do my test first. How many people here do TDD? How many people here do it successfully like really believe in it and really love it? Okay How many people here how many people are here to Get that confidence or get that to be sold on TDD Who want to do it? Okay, so what I would like at the end is for those same people to have their hands raised and say well At least I want to give it a try So With no further ado, I want to kind of get through these slides as fast as possible But I did want to do at least a quick intro of what TDD is The number one thing I think of when TDD is I think there's you don't write any code at all without a test first and The the next quick step who here is sort of a red green refactor There's there's something pretty important about this red green refactor There's there's a concept of the red bar you want you want to test to fail Which gives you that red bar and then you want it to pass which gives you the green and then you want to want a refactor Well, why is that refactor part important? Instead of saying test Sorry, red green refactor. I like to say red green refactor Give it a little bit of time because now it's green you can spend your time and make the code a little bit better TDD isn't just for unit testing It's used in For example, I just used it in this presentation. Hopefully the test passes at the end It's used in acceptance testing and And the other important piece here is that it I've been doing it for a little while and I still learn every day I've I've been on a team where I've helped mentor several people and they get it After some time, but even they are continually learning so it's not something that you're gonna learn in Three hours or something like that is something that you got to be you got to be sold on first And then you got to want to do it once you start doing it you start learning the the trade. I This is my own personal opinion. I don't think I this is one of the practices an agile one of the mini practices Which aren't easily reproducible. It takes practice So the sales pitch look ma no hands I Kind of want to go back to this other one for the sales pitch as well where you see the Pitcher of the guy. He's got that Kind of umbilical cord. He's got that cord right hanging off him and he knows that even though he's gonna fall he's got something to hold back on and That's kind of something I'd like to have you guys experience today So the things there have been several research papers several studies on TDD And it's pretty hard to talk about them all for the sales pitch So what I like to do is is that as far as I'm concerned I'm gonna kind of do a little selfish spin on this Why I as a developer would want to do TDD not why my manager wants to do it or why be good for the company Why is it me? So you might ask the question. Well, then what do I care about 50% reduction in bucks? Well, let's go over these really quick. So one thing that I found By looking at these these different Experience reports and whatnot is they all seem to talk about a reduction in bugs and While I say I'm trying to sell you individually. This is also nice for Executive level to say hey, this does maybe make make some sense and So the first two are bug density the last one which I like is Confidence This is what it's all about for me. I know that my code works I find that I do the acceptance level testing a little less When I when I'm writing my code because I know that it's already tested at the lower level So I don't for web application development. For example, I don't spend a lot of time Deploying the application and testing login and testing all these little perspectives. I do do that Don't get me wrong. I just don't spend near as much time on it because my unit tests tell me so Show of head. I kind of want to do a quick collab thing going on here. What is the cost of a bug? So I started out and I said well, these are some things that happens when a bug is found by QA So can anyone think of any more? There are several on here. In fact, I was recommended to take some off So that you guys could come up with some ideas So when a bug's a found you have to find the bugs to reproduce the bug test You have to find the steps to reproduce the bug, right? Then you have to yes Okay, that's good. Thank you. You actually got to the next slide Okay, so there's a there's that two or three things Three and so for the developer to acknowledge the bug is that just the developer looking at it or does it also sometimes need to get prioritized Oh, so he just got he just got in his queue and he's acknowledging it trying to build context How much time do you think that would take? 15 minutes. Okay, and then what was the other one? fixing it Okay, so I want to I want to skip fixing it because that's kind of a hard one Three minutes four years. I hopefully not four years, but a very long time to a very short time So what about what about in so resolve it going in and then two minutes. Okay, so far. That's a 12 minutes then Okay, so yes Okay, the bugs released could affect customer loyalty Thank you Yes QA has to validate the fix. How long does that? Pardon me They have to retest the code and do it again. So how long does that take? Just a quick number. That's all I'm interested in 10 15 minutes. Okay Okay, so that's right, so and how many people does that include? Six and so let's say that you're going over one bug Five minutes for each person in the room. So that's 30 minutes Okay, so now we're at about an hour and we haven't talked about finding the steps to reproduce the bug I know in many cases that could take 30 minutes an hour sometimes I found a bug now. How do I make it so development can consume this and reproduce it? So now we're looking at about an hour and a half there What about just Dev and QA sitting down and discussing it five minutes ten minutes Okay, are there any more? Yes Okay, good the argument where Dev cast blame so how much time do you think that's that costs? Half hour, let's so let's be modest and say a half hour so now we're about two hours and 20 minutes or something. Yes Okay Good so another 15 20 minutes So let's just cap it off and say about three hours is what a single bug cost and it depends For example, what if the customer found the bug? Now do you need it now? Do you need to include more people in that process? Does it need to get prioritized differently? and That this was brought up. What what if it introduces new problems in production? so Quick vote really quick. How long then do you think this all might add up to be what's a pretty modest? A amount of time that that bug costs without even fixing it six hours going once going twice Okay So what does this mean to me? Well, how many bugs are in your current project? Any any takers? 40 It's only one per line of code one per line of code So a reason yes, okay a thousand all right So now we go back to the numbers and say that there's between a 40 to 90 percent reduction in bugs So let's just take it. Let's just say 50 because that's I'm a simple person So that's you just reduced 500 bugs and we said about six hours. So six times five hundreds. How many hours? 3,000 hours So as a developer what that means to you is I get to spend more time coding Less time fixing bugs so I don't know what this duck's doing here, but I Like to do a demo on the Fibonacci sequence But I want to take a couple minutes and just explain what the Fibonacci sequence is Would anyone here like to explain it? Yes It's a series of which you take the first number and the second Okay, good, so it's like n minus one plus n minus two So if you're if you're looking at the Fibonacci sequence for five It would be five the fifth Fibonacci sequence for five minus one Which is four plus the Fibonacci sequence of five minus two, which is three Okay, good, and and there are the numbers up here. I have a cheat sheet which is Right here So I'm actually gonna I'm using IntelliJ and there's a couple things I want to demo I don't have a lot of time to talk about them specifically, but I'm gonna be using infinity tests Which is something that runs on the background while you build your code It just it's running the test in the background and it only tests the code It only runs tests for the code that's been changed Which is nice when you start getting many many tests and it's also nice thinking that I only need to run this one unit test When I changed this code and you find out there's actually lots of other unit tests that need to be run And to it saves time. So I'm gonna IntelliJ. I'm gonna create a project Just call it TDD demo Maven I'm gonna make it a Maven module So to start out I'm gonna go in and I'm gonna Because infinity test requires Java 1.5 for now. Hopefully that gets changed In the future. I have to switch that to 1.5 and then I'm gonna add infinity test as a module to the application So for unit testing the first thing I like to do Well, this is gonna I'm gonna write my test first, which is just gonna be well not really write it, but agileroutes.tdd and then notice I created my package in my test directory first Test package first. All right. So the first thing I'm gonna do is I'm just gonna create a little hookup test What's a good name for this class but say it has lots of algorithms? Okay, I'll call it algorithms. Thanks for the feedback So let me start up IntelliJ again really quickly I while I'm doing this. I kind of have a funny story This morning or last night before I went to bed. I saw that OS 10 had some updates to it and I thought to myself I probably don't want to do this because it might cause problems on my presentation tomorrow morning And so I woke up and I saw that it all restarted and I thought you know what? I need to test to make sure that my presentation is going to work. So I started it up and this very same thing happened and When I did that I found that sometimes I had to restart IntelliJ So another in this case test afterwards, but I thought of the test before I did the presentation at least So, um, I'm using J unit I'm just going to call this a hookup And I'm just going to fail the test And believe it or not failing means it worked. I am using maven. I kind of wanted to show some of you the the uh The benefits of fully working in your IDE. So I'm gonna I'm going to add um J unit as a maven module so now it can be built with maven as I'm running along as a dependency And here I'm just going to go ahead and and include the static import for fail and um The the infinity test should be running so now I can build it And you say I get a failure Yes, that means my tests are all hooked up my build works And I don't have to worry about all that now. I'm going to write my first test. Um What I'm going to do is when I do tests, I like to first test the exceptions. So Since there aren't any requirements on this I'm going to I'm going to make a new requirement And that is I'm not going to worry about negative numbers. Um So in order to do that I'm just going to simply look for a Illegal argument exception because I don't really want to create a new exception And I'm going to do a So what's the what's the first Fibonacci sequence? What's what's zero for Fibonacci? Okay, so sequence number Oops zero I'm ahead of myself algorithms dot Fibonacci I'm going to pass it zero All right, so now I have to worry about some things here. Yeah, go ahead and import my assert It uh, it says it's no it doesn't know about a class called algorithms. Let's go ahead and create that And now it says it doesn't know about a method Called Fibonacci. Let's go ahead and create that for now. I'm going to have it return an instance well Hopefully I don't have to explain that uh negative one. So now I'm checking what? The first one is zero. So we talked about red green refactor. So what am I doing here? I'm getting the red So let's build it uh Oh, whoops, sorry Wow, I'm way ahead of myself So I'm looking for negative one Please correct me when I'm going. I need your participation here So and and obviously the name probably isn't very good anymore. So Fibonacci Negatives So it does fail. It says it is expecting an illegal argument exception. It doesn't exist. So how do I make that pass? By throwing the exception. So what if I just do this? Will that make it pass? Okay Now I don't have to worry about that. It's running in the background. It'll just go ahead and pass. I hope But now to really do it. I should probably do if the num is less than what? Yeah, the that is supposed to be green If it weren't for the the wonderful hue of whatever the pink So num minus if num is less than Zero thank you then throw an exception. Otherwise I want to return You know what for now, I'm just going to leave this throwing an exception. I'm going to write a new test So what's another test I could write? Okay, so I'm just going to call this the first few So assert equals Um sequence So when I do my unit test, I really really like to put down the comment Of what it is I'm testing because after a while you start getting things the test fails And you're like, well, why did that fail and you got to go look at the code to figure out why it failed This little message tells you where it failed and why it failed. You don't have to go back and figure it out So I'm looking for zero algorithms Not Fibonacci of zero and that should return zero. Is it going to is it going to am I going to get a Well, maybe it's the same color bar no matter what Okay, it actually shows red. So maybe we'll say red and not red So it doesn't work the way expected. So I have to do a little bit of refactor here. I can't just return zero Right, so I now I need to do now do my check if by the way, there's no science behind this I could have done it completely differently. It doesn't matter in many cases In fact, the way I I practiced this a little bit before and I never did it this way This is the very first time I'm doing it this particular way Um, so we talked to you guys all answered the question check if it's less than less than numb Then I want to return I wanted the test to fail first To make sure that my test is valid. Does everybody understand that principle If you have a failing test and it shows red first And then it shows green how much more confident are you that the test actually does something correctly that is checking something Right versus if it's not red if you just write a test and it always passes Are you sure that that test is actually testing something? I have actually written tests that pass no matter what And I didn't find that out until I had a bug and I went to to say no that test shows that it works And then I went and fixed the back end and it didn't change It's still it still passes Okay, time to change the test when when is it going to fail here? Uh, so it's really important to get the red green Part of this here. Okay. So now it's red now. How do I make this pass in the shortest time possible? Return zero. Thank you All right, so now I'm going to introduce um, I'm kind of going to jump around a little bit What what's another problem with this particular algorithm? It's returning an int At what point might it blow up? When it returns an into that's larger than Pardon me the size of the integer the size of the integer Okay, so I'm going to still throw an illegal argument exception and except this time I'm going to I'm going to test something that's too big I happen to know uh That 47 Is larger than the max which is this So I'm going to go ahead and I'm going to write a test for that and therefore I still expect an illegal argument exception when I pass 47 Does that does that make sense to everybody? Any questions right now? silence Okay, so I got the failure. So now how do I change this to to make the test pass? I need your help Yes Okay, so I can say if num is uh greater than 46 Is that good well pass It'll oh it'll pass, but it's not good. Why is it not good? Thank you, jonathan. No negatives Is that descriptive enough? No um And our nor Nums wow if I could only type greater than 46 How's that jonathan? That's what I thought All right green bar at least it is over here All right, so now I'm going to introduce the next test What am I going to test first sequence? What's the first sequence or what's the sequence number one? One Now I'm introducing a code smell here or a test smell. What's the test smell? copy paste so Pardon me Okay, multiple search for a test Um the fact that I did copy paste here, which I'm going to fix in a minute But before I fix this refactor I want to make sure that my test actually passes So how do I make it pass? Can I say return num? Now I'll do a quick refactor here. Um So how do I change the sequence number zero? What do I need to do here? Key value sub what? So here I've got the Right here. I'm saying that the zero one I expect it to be zero And the sequence number one I expect to be one So I'm going to say that sequence key of key value equals key sub Of uh zero And this should be key sub key value sub One or two or zero One, thank you And that's for zero. So uh Make sure my test still pass after the refactor. Maybe get rid of this extra test Now I've got one assert Sorry, bad show anyways So now what do we need to test? It'll get more interesting in a second here The second one maybe sequence number two and what's it one? Failure, uh How do I make this pass? Returning num no no longer works. What do I need to do? I need to do a refactor here I I need to actually what? I need to actually do the sequence so There are different ways we could do the sequence. What's a good way? iterative or uh recursive Should we do a cursive? Okay, so if num equals zero then do what? By the way, my code's ugly for a reason. I just want to get this done Return Zero otherwise what? Well in this case, I'll have to worry about one Is that right and my test passes? I hope Okay, let's get to that point. That's good. So now now we can worry about the bigger numbers So I'm so good Method names. I'm going to call it bigger nums And now I just copied and paste which is another problem, do you think? So I'm going to use a little refactor here Create a method called a fib test test fib whatever Boom now Okay, so now I'm doing bigger numbers. So the bigger number would be three and what's the fibonacci of three? two, thank you If num is less than or equal to two then return one Is that going to work? Is that going to make it pass? otherwise return two okay We're going to be here all day. Thank you for that. So now let's go to the next uh Now let's go to the next number, which is four and what's the fibonacci sequence of four three? um, so so Now this this isn't really going to work anymore so, uh You'd brought up that we do what fibonacci of what? n minus one Can we can we just say plus one and will that pass? Little weary it passes Oh, thank you fibonacci of num minus two how confident are you that this test? Now that I did this refactor how confident are you that the test is still going to pass or fail? Or that if it passes how confident will you be in at least in the tests we've written so far that it still works? Raise the hands confidence level. That's not very many two people Okay It passes Huh now your confidence has gone up now how many people are confident that this works Thank you. Um, so but does anyone have any concerns about the algorithm that i'm doing here Let's let's put what what's what's five? What's the fibonacci of five again five? So confidence level did it go up a little bit? Uh, I'm going to say mine hasn't the the reason is is that I'm going to Five should be three zero zero one with yeah three is four five is five So, uh, does anyone know the the 42nd fibonacci sequence? There it is So if this passes how confident will you be that that we just came up with a good fibonacci sequence algorithm How many people would raise their hand if this passes that it's good? Okay That that's yellow still running Okay, so after some time it ran. Are you happy with the time that it takes to run it? No, well, let's just let's just throw another number in there just to make us really unhappy So while that's running I'm going to declare that this method that we came up with via tdd isn't the most efficient method We're using recursion recursion can have problems with memory and Slowness as we can see so I'm going to take a mathematical approach at this and Unfortunately, you're going to have to watch me for a minute. Not a lot of feedback on this one Okay, that would work Okay, thanks. Okay So, uh, what I'm going to do is I'm going to I'm going to create this little algorithm square root of five All divided by two that's five I'm going to comment this out Um, and then I'm going to return math dot floor math dot pow of five to the num and I'm going to divide that by the square root the five and Just add in the half just seems like a good thing. How many people are confident of this algorithm is going to work No one why not? Less code What's it going to take for you to get confident that this algorithm works? So let's say we didn't have any tests. How would you know that this algorithm worked? Okay, and even after that would you still be confident or would you still want to run math? You know software tests against that whether it's manual or whatever So so just for grins you saw how long that last one took. Oh look, it's already done and it passes So now how many people are confident that this algorithm works? You're still not confident There's some people that didn't raise their hand. I'm just wondering why they're not confident Too tired Um Yes Two large numbers 42 and 46 Okay Yeah, right here 42 and 46 So now you're more confident You could we could add more and you can gain confidence All right, so this this ends the demo um, how many people in here That originally raised their hands that came here for a little bit of Oomph that I want to try to do tdd. How many people in here are now more interested in doing tdd So that's about half of the people that originally said that they were interested So that's 50 success rate. Um, does anyone have any questions? Yes Yeah, sorry I My eyes don't work exactly right so I just wonder if you have to write a test before you can write any code After I had a test before you can write a test So so writing a test for the test But that but that test code isn't going into production And that and that test code is also very very simple of saying this is my expectation So so so what the test is saying is the test is saying This is what I expect the software to do Um, and therefore I'm going to write the software to behave as it would expect As you would expect it to happen. Does that answer it? Any other questions What about those people that didn't raise their hand who still aren't who still don't want to give tdd a try Do you have any questions or any any concerns? Yes So I'm actually not a developer I'm a joy manager and One of the things that has been difficult is getting a team of engineers who are not Have really never had any experience with tdd to to Build confidence in their own skills and to actually learn how to do it and do it well I don't think they have that. I mean as far as understanding that concept. It's really hard for them to grasp right? Yes So is there information that I can you know, give them that would kind of Help guide them So there's a This is something Kay knows that I happens to be dear to my heart Um, I have I've worked with this specific. I've mentored teams under this specific scenario Um, a few times and have been successful at at test infecting the whole team and um How that's done is by showing as much support as you can show and you'd asked about well, what type of training do I provide them? Um, I wish I should have prepared that and had a I have a blog post up that talks about how to convert a team over It's called test infecting a development team Oh, did you And um, so the the important thing is that there's users groups There there are you can bring people in um Which local companies here are doing I've helped in that And um, you can you can have them read books You can you can just have successes of I want everybody to try I heard of a certain organization that gave away monitors lcd monitors This is the time when there was a transition between the two gave away lcd monitors For the first was a 10 unit test or something So after somebody wrote a certain number of unit tests, they got their lcd monitor Um, so again, that's not necessarily tdd, but there's there's two hurdles to jump There's unit testing and there's tdd in my opinion. I think you should just go tdd Um, but in the middle of a project that's might be hard because stuff code's already been written It's already been written a certain way Which I think john's presentation after k's will be really good his his tutorial will be really good at that one Thank you