 All right, so how many people here have done test driven development before? Okay, how many people here have done? testing at all Okay, so we're still not got everybody's hands yet All right, so who enjoys Who enjoys writing code or at least at one time is enjoyed writing code even if they were like ten years old Okay, remember back everybody's hands. I want to see up you enjoyed writing code Okay, you never enjoyed writing code. Okay. All right. All right. So on the other hand who enjoys Playing games and my wife's like I don't like video games. Well, she's good at Scrabble. So if you like Scrabble video games Whatever Football I want to see that hand up. Okay for me test driven development is like my first coding experience and like Playing games. That's why I like it. You don't have to like it for the same reasons, but that's why I like it All right, I am John Johansson I'm John Johansson. This is Jeff Grover and this is Richard Thompson we are presenting and They get I get to do the talking and they get to do the coding All right XP Utah last two weeks ago Or no, sorry SL Agile two weeks ago and I asked the question Well, I didn't ask the question John Major asked the question John Major said to me he said as I'm preparing so I got a TDD course We're giving and John Major said well, let's ask around the room When did you become test infected? So we went around the room ask everybody and Some of the statements one on one that particularly struck me was somebody said when my project When when I delivered on my project and there were no bugs Okay, that one struck me because I've been there done that. I like that. It was a four month project for them Another reason I like it. I said when I when I asked the same thing with Jeff Jeff said When I can be proud of my code I mean how many people have pushed out code that they're actually proud of? Yeah, that's what I'm talking about That's what I'm talking about. How many people are confident there aren't bugs who attended Christian session We went Christian session Who actually believed that his refactor would work? Okay, so three of you and you guys do tests first, right? Yeah, okay Yeah, it's amazing. It was like I've got the test. I refactored it. It worked All right, so that's some of the reasons and I don't want this court. This session is not about Convincing you. It's it's got it's got me excited. I want to get to the coding as fast as possible All right, so let's talk about a little bit about What the metaphor is red green refactor you've heard that before yeah, what does that mean? Exactly it's exactly like the duct tape TV show Richard was like trying to get me to put a slide in there and I keep fighting him luckily Yeah, duct tape red green the reason I put this slide up here is I'm gonna bang on a table So the rhythm of red green we always forget it. He goes red green refactor That sounds like red green that goes red green Okay, red green Refactor Okay, what are you catching there? The time yeah, what how when do we how much time do we spend red and how much time do we spend green red? Red green red green red green refactor so we do all our refactoring in Green exactly so One thing that happened to me. I I had the blessed experience of not having read the test-driven development book when I started I had the blessed I Started XP before there was an XP book. I was reading wards wiki That's how I got started. So I gotta must make every mistake in the book And then one of the biggest mistakes that bothers me is I coded so much code in the red So I wrote my test first nice big complicated test And then I was writing trying to solve it and then I'm getting sign to solve it run the test So red runs test rail red and then finally It's green they go quickly write another test Turn it back red again, and at the end of the day. I'm like I feel awful then Kent Beck's book came out. You've seen it I'm gonna need it. It's That box out there In front of the wheelchair So Kent Beck's book came out and if you haven't read that that for me was eye-opener because he said Red green fast every factor and then I changed it and suddenly I am here. I'm happy So so for my psyche It was better to read it was just changed that little bit It wasn't much that I was doing and there's another one too The one that was on it or are behind it Okay, there's also the toys All right, so this is Kent's book This is the test given development book. All right, that's enough. I'm done talking about it Ask me later if you want to know more details all right, so we're gonna do some exercises and Since not everybody in the room still codes okay, I'm looking at one of you and I've organized it so that When I do this not everybody actually can not everybody actually codes and and we go really fast So as you start to fall apart, we've got some roles that'll help those that are behind so we're gonna talk about a driver So if you've done if you know about extreme programming You know pairing is often divided up into two people one's called the driver or in my case I call it the dog the person that sits next to him is called the Navigator excellent the navigator and I don't like this navigator I don't like to be yelled at when I'm programming so I chose a a little calmer navigator So our navigator is gonna be the baby so we got the dog and a baby So one of you is gonna be driving programming another one is going to be Sitting there keeping the high-level picture talking to your pair helping them out when they need it. Okay as we code We've got two more roles in your pairing That are for this just for this conference geared for the people that Don't want really want to code they want to sit in front of the monitor and pretend to code so this is the Questioner is that you said that? That question the questioner. Yeah, excellent. So we got a questioner, which I call the arm and our final person is Who knows who that is? Hot Rod Hundley and what does he do? He's an answer or a commentator Okay, so so our four roles are the the dog the baby the arm and the old man Or the hot rod however you want to look at it the hot rod is going to talk The hot rod is going to talk so I'm going to be standing back here While this guy's programming and I'm going to talk and I'm going to talk to The arm and we're going to talk about his coding They're going to try to ignore me Hopefully they will and they're just going to code along and I'm going to point out little things I notice the questioner is going to go but what is that and the commentator is going to go well That's what that's the that's a test. That's a setup. That's this that's that that's why Your success depends on those people that are standing behind you At your table. All right, so that's it. We're going to get started Jeff has a presentation and Richard and I and We need a volunteer Jeff needs a pair Jeff needs a baby. I Need a baby and I need a baby out of the audience. Thank you very much a Java. We're programming in his job of the language To Java Okay on the back all the way back. All right, so we've got we've got the dog. We've got the baby Richard is going to I'm going to be the arm and Richard is going to be The commentator you're going to be the old man All right, so Jeff if you can Settle up Nick to Jeff and help him out with the Java right here. We can see his laptop so Richard. I don't Yeah, no you're actually start talking your hot rod We can look we can look up the big screen. So what are we going to be doing? What are we gonna be seeing? Well, I know we're gonna see first. So is that a is that can we all see the code? Okay, that's cool. What's that? What's that at what's that at test thing looks like an attribute saying that this method So why did he write fail me first? So tell me about assert true He's the using a test framework that provides a So is that red bar down there that's the that was the failure that's the red okay. Oh wow that was fast He went from red to green pretty fast But it doesn't look like he's doing anything he hasn't done anything So now he's off in the production code bouncing back and forth to the test and the production code To get the Production code to be implemented far enough to get this test to compile and go red still green. Well, he went from He's trying to Not sure what this test is supposed to be saying because it says fail me So I don't know what we're trying to do with this test yet This is what the baby supposed to catch so this test is going to tell us how a protocol can be parsed from a URL And that's what URL parser's job. That's going to be So now he's getting into the actual usage of the class more than just making one and not having to do anything He's saying that when we call get protocol on the parser with that URL. We ought to get back HTTP so then he just writes That's had the method that he was going to implement in the test first. Yeah. Oh, yeah, that's the way you do it Really? That's interesting. You tell yourself the story of what the ideal code would look like then you go and write the code to satisfy that story That's how you get perfect optics every time That's so bizarre. Okay. Oh I'm still confused, but I'm stay on trying to stay with we're making the object Do what we want because we're seeing what we want first So now he's off implementing his constructor that That's right, but we need our test read before we can get it to go green now our test is red. Oh, okay I got that now. We're red Yeah, really that we want by specifying one test case at a time and we incrementally build a behavior We don't go and build the final implementation just to satisfy this one simple case What if we never needed to do any protocol parsing other than HTTP that would be good enough We would not have over engineered it Really, but we always build just enough code for what we need. I don't want to build more Write more tests. I can do that right more value for your customer. It's nothing wrong with making more It's making that more useful to the customer Oh, he's already got the next test why I was looking why I was looking away There's an average on the test that the host can be parsed from the url He doesn't have a get host name yet. Is this right? Can you do this? Okay, it satisfies all the tests we've written so far The question is do you have enough tests to cover everything that this object needs to do if this object never needs to do anything More than parse HTTP protocol urls from Google.com and this is just a right amount of code Do you know what that's called when he's doing that? When he's doing one when he is fighting tests of force The force is triangulation triangulation is called. Yeah, that's where you you're pinning down the behavior of the objects That you want and locking it all in here is your safety net This is what's going to give us the confidence that when we make changes next year on this class that we didn't break all The stuff we wrote this year Yeah, so why is this important? I heard that How many people here have worked on code other people wrote then I was impacted Gary and our test here We see with that intelligent Are you using Intel J Jeff? We're just using me all right. We can't talk about one of the rules is you don't talk to the coder I Okay, so he's got another red test Now we're getting into more than just hard coding it because you can't return the hard-coded HTTP and have it work for both FTP and HTTP so now he's got to go in and incrementally enhance the Implementation to satisfy the existing tests and because we wrote that earlier test for HTTP We know that the code that we're writing now that's handling a more realistic case is going to work for both You have to store a little bit of state now because you can't just fake it by returning static strings You've got actually remember the stuff that was passed into the constructor and do something with it Now he's got the HTTP case and the FTP case working and the host name part it didn't get affected. So it's still working as well Now he's getting ready to raise next test case Or he's going to try to do some refactoring He's green so now he can make changes You know with confidence knowing that he's not breaking any of the behavior as long as the code is As long as all the tests are still green after the change so it looks like he's getting ready to Factor out some duplication between these test cases each one of them is creating a parser object in the same way a little helper method in his test class that the duplication is Extracted out into the helper and then he's going to use the helper in the test cases Why would he want to do that? Duplication duplicated code is always a source of errors because you change one and then you got to go change all the Other instances of it. So if we can extract out the duplication We can reduce the likelihood that we are introducing mistakes when we make a change Yeah, how many of us have worked on code that other people wrote How many of us have worked on code where somebody's idea of code reuse was copy and paste You felt the pain of duplication. Yes, okay, so he's got that test case using his Extractive method, so they're just laughing. I just want red So he's trying to figure that out. He did a he did a refactor inside his test case That caused it to go red I'm a little baffled by that and it looks like it looks like he's baffled by that too It means he's made a change that you know has now caused the test case to fail. So You could either back out and go back to green or you can make you know Adjust this to get this code to be working again Where does it show which test? it down in the moustache It's underneath you have to scroll down Yeah, the different test frameworks are different so this one just have a good job on test frame It looks like he's a little bit lost right now. Yeah, that navigator maybe we need to change that baby into screaming woman Really flailing around he's wandered off of the production car I Got you little corrections he wants to be listening to his pair. He's asleep As repairing you want to catch little things you'll learn what repair wants you to catch a good navigator We'll say you want to make the next left-hand turn as well as we're going to Cincinnati You want to change this here? We haven't learned our tests in a while. I'm not even sure if I'm pretty okay We're pretty now. It's like he's adding new functionality, but we're not red He's refactoring under the green blog That looks like an enhancement to me new behavior that isn't covered by tests Actually, it looks like it was close name. It's covered by one test He's just random doesn't look like he's re-implaining. See now he changed the implementation and it's red. Oh Maybe without was that intended? No, I think he just type mode No, I think he's trying to figure out the right incantation to get the test to be green again We were turning that static string. So for the tests we have The static string was covering all the behavior for the test that we got We had written a new test asserting on the host name to be something different than we would Is that a test as you write? Probably have Written a test that would force me to go and do that implementation in response to the test rather than anticipating The implementation and before the test strictly called for it. Okay, but you're seeing is Particular styles. So as you do we see we're seeing Jeff style You're gonna hear what my style is and you'll get to see Richard style a little later, and you'll get to see your each other's styles Okay, you want to learn from that and when you see a new style think about it and say which one of those do I want to try and try it? Was actually adding new functionality because there wasn't a test that would cover that somebody could come in and change that to be Google.com to return to that string and there's no test to cover that case But somebody could be using that for a different URL and you don't know hence you introduce a bug So now it looks like he's adding he's got a red test for getting the port number out of the URL string and He's trying to get that to your green So I saw right there. Do you see it red? I'm seeing red up there. Yeah, it's red He's trying to get to green and you put that in to your string in there No, I couldn't they couldn't he could have faked it out with a 443 string But having done that rhythm of faking it out with a static string and now he's already storing the URL He's going straight towards the implementation that will satisfy the test to give green So this is different from trying to get triangulation Yes You wanted to do a triangulation style You would write just returning 443 for that first test and then you'd write another's test And then that would try and you lay down into the arbitrary Port number. He's got the port number functionality locked in. He looks like he's going to Factoring on his production class He's noticed that he's got these splits with the bracket, you know bracket, you know littering his code So he's gonna do some pre-processing work in the constructor My guess is gonna do looks like you can do some pre-processing work in the constructor And then just return them the results of that for those getters Wow, that's cool. Do you see that? He's got this repeated code URL dot split colon So he's taking care of that in the constructor and then reusing that to eliminate the duplication And now he's gonna run his tests and if they're all green then that refactoring did not change any of the behavior So he has confidence that the change he made did not break any of the functionality that he's worked so hard to create To actually start two things that were hard All right Programming in front of an audience that's one then programming in front of Richard and Us talking about it. You get to experience this yourselves and You get to experience yourself this soon But not quite yet, but while But while we We're about to program and I don't care if you do this in pairs or if you do this one We're gonna actually step you through it All right, we're gonna do one of Bob Martin's caught us and We're gonna take you right through it and we want you to follow along. All right. I've got in this box I've got these red and green stickers All right, so the table that finishes first that hasn't got their red and green stickers for their badge Gets red and green stickers Okay, so it's your responsible for your table. That means everybody who's coding at your table Is it why the success or failure depends on your table is? Dependent on every single person having that step done at your table. All right, so Just so you know All right, so thank you. This is what Jeff did. He managed to take an obelisk and Break it into its component parts. Oh an astrolabe. Thank you All right, so this part Says get your computer out and get it ready to go. That means if you don't have your test framework up and ready to go You might be behind. All right, so what we're gonna work on we are going to do Prime numbers all right, we're gonna do we're gonna find all the prime factors and You pass it a particular number and all the prime for going to return an array of all the prime factors Now you'll notice that there's some non primes in here. What are they? One is not prime eight is not prime and you notice one is a weird one is a weird color and eight is Knocked down or is that infinity is infinity prime. All right, so everybody's got your laptop out and your IDE up and I want test one written. I want a failing test to be written see that little red dot up in the right hand corner So again, this is go. This is go time stickers are on the line stickers for the badger on the line This is written in pseudocode Having Richard at your table is like like cheating So instead so instead I'm gonna make him walk around So call out So let me know as soon as your ID is up. I want to see a class. Who's got the class written? class prime factors You got the class prime factors? Can you call it that? Yeah, please that'd be great. Just refactor the name. You've got one person coding here. I See two laptops. That's all right. What what's with the two languages? That's good. It's better if you do it As many people if you do this one the next exercise is we won't have to do but try to do this one together Or separate as much as possible. Are you not doing it? All right. Can I push the button? I'm pushing the button Okay, so we should now have one class and one test inside that class that test should be called One has no factors or something like that Okay, who's got that because we're stickers on the line the first table to get that done I want to see those hands up You guys done that table is done And you've got and It's been stubbed with an assert fault and it's ran. It's failing and the test is failing Okay, that's what I want to see You guys ready? They guys were close. I'm walking stickers over there unless somebody stops me These guys are close They're very close. Has everybody got it wait Wait, who are you pairing with? Can I? Kick you on to a table. That's not programming or pairing Sorry guys Here we go. All right stickers for your badge These guys are first Red and green both you want red and green you want both red and green on your badge That'd be cool. That's it Okay, so how are we gonna what are we gonna do next? What does this tell us? What is this just told us? What do you now know are you guys done now? Anybody not done we'll send to helpers over Jeff. Can you make them done? What does that tell us our testing framework works? That's all this tells us and why do I do this? Last week. Okay. Just last week I was programming along writing my tests writing my code and I didn't see the red first I'm writing code and it's green and then it's green again and then it's green again and then it's green again And about three tests into it. I realize I'm testing the wrong framework Go and and then I then I hook it up right and actually run it and all three of my tests are busted That is not a pleasant thing to have happened You learn from my mistake. Don't do that Okay, so run the test make sure your test framework is working against the right thing All right, so that's cool. Jeff. Are we are they done? Well, what we're gonna do is take that test We don't care about that test anymore and we're gonna make that test real So replace the assert false with assert the empty list so this is an empty list and Assert that the prime factors dot generate is one. Yeah, we've made it a static class here Okay, so we're about to make your test your failing test work Okay, so tell me somebody tell me what that next slide is gonna have on it when I push the button and what is What is the implementation going to be Not return true B that's it. It's so simple Be really easy Come on his first both of them are done it Oh, come on I'm gonna I'm moving on Hopefully you can catch those who are behind a little bit behind can catch up What test? All right, so somebody tell me what this test is gonna look like. No, it's not gonna fail. It's gonna fail. Yes You're right about that But what is it gonna look like what is the failing test gonna look like we're gonna say assert something We're gonna assert something. What's what are we gonna assert? We're gonna the length of the zero. Well, I'm not gonna write that I'm going to return. What am I gonna write? I'm gonna write To how many factors does to have and what is it? To so what are you gonna see at list containing two? That's what you're gonna see Let's see if we were right. Oh, that's what we're right. And what's it what's gonna happen when I run it? It will fail Okay. All right stickers on the line. Let's go. Let's get this one done This should be fast this should be very fast because this is the second time through again. It's your It's your team. It's your team First and then help the others that's how we win Okay, so I'm moving. We're about to make a pass What's the pass gonna predict what the pass is gonna be um is to Bada bing bada boom did anybody predict that? All right stickers are on the line again Come on go go go, but your team. It's your team. It's not just you our next test is a duck my bad All right, so test three has one factor What's the assert gonna be somebody tell me the search not the same guy? It's been back there telling me all the answers and not Richard cuz he knows Somebody what's the assert gonna look like you want Richard to answer? Come on silence Assert go ahead Richard. That's actually the array is going to contain. Oh Come on, but you know what I'm doing that was pretty easy to predict Okay, I want you to predict the next one. All right when we do the next one. Is that gonna pass or fail? It better fail All right, have we got so stickers we've got one team some but one team left and they're gonna be first No, we got two to one team Okay, there's two teams That makes this makes a good exercise because you actually do have to think All right. Are we done? I want to get to the next. I want to make a green. We've been red way too long Yes, thank you. There's your implementation Alright, there here's a chance to get your stickers. There's two tables without them That's a quick implementation That's very quick quick quick quick. How hard is that? Oh nuts. They gay beat you and these guys Who turned who turned my laptop around? That's beautiful. Okay. We ready to go on So tell me what the next test is gonna be if we've done one has we've done one two We've done three. What's the fourth one gonna be? What's our fourth test gonna be? We're not gonna throw an exception at all four Four Four has how many factors two factors two factors Two and two two and two All right, so what's the assert gonna look like? Somebody's gonna say it. I know one guy already knows I know what two guys already know But here we're gonna say it somebody's gonna say it. I'm done with one assert Stickers on the line. Let's see if you guys can get this There's two groups left. I hate Microsoft bashing Yeah, you can I would actually write they can write your own I would assert Then like like steal them and help those guys get their their stickers. All right, let's go to the next one How are we gonna make this pass? Making this pass is hard But it's a one-word change Yeah, but it's not a one-word change right with the mod see by the being we got somebody predicting the mods in there all right, so All right, you guys are going you guys are trying to get the stickers. It's just an implementation Try to beat that try to beat what these other tables These other tables are gonna come help you. That's one of the stories. I'm gonna tell if we get time So are we done? I am So has anybody had trouble putting this in Okay, so I got a question What we just had a question over here. Can you say it again? Can I can I get everybody's attention? That was your question We all do the first solution the algorithm wasn't gonna be our end-all solution, right? We knew that we're gonna work at what point they're trying right the end algorithm first and Then that then we will convince like so I responded with what's my next test somebody tell me my next test Five is five gonna pass or is it gonna fail? Yeah five looks just like two. It's so obvious But it took some thinking if you don't know then you write test five It turns out Sorry turns out. It's pretty obvious that if I wrote five it would pass So I wouldn't write that And then When this pops up here, I think there might be some people like I don't know how you My point is if you have some time, I know we don't have too much time to but this stuff will just kind of Appear it'll emerge and it's totally cool and it wouldn't have happened if I would if I wouldn't have done this Okay Exactly that what he did was triangulation What Bob Martin's doing here since Bob Martin knows primes he writes a prime thing every two or three weeks Since Bob Martin those primes He's just doing the simple the obvious implementation the obvious next step and that's what we're doing so the question is Five does everybody convinced that if I ran if I wrote test five that it would pass So I'm gonna skip test five So test I'm gonna skip five and go straight to six is Six gonna pass You think that algorithm that we just implemented works for six and what's the assert obviously you've seen it before Five works six work guess what we are starting to there's something happening here. That was a surprise We thought that wasn't gonna work Five works six work is seven gonna work was to so we're gonna skip it that was too obvious It's same as two and five so we're gonna skip it. So our next test is going to be eight is eight gonna pass If you can't if you can't immediately answer we need to write that test Okay, those two teams stickers on the line for this one. Let's get that implemented Somebody help them out. I want those guys to get their stickers So somebody somebody help them You know what the assert looks like I'm helping my father Now I go sit over there with them All right, so Predict this test for me. Oh, this is the pass. So they got a chance. Why don't you help them? Make your table pass get your table of stickers Jonathan if you get your tables of stickers you're getting sticker I Help your table out chess over there. These guys just have to be done. They're facing the wrong way. So they're sort of Know I can't believe you're sitting. All right, we're moving on Richard you're gonna have to you're gonna have to get them their sticker. All right, so this next test You know what it looks like? Does this test pass or fail? It fails good call and what's the algorithm change? Richard are we getting their stickers? Oh You're good that see that look at that it takes an outside a little bit of outside help and they're gonna have their stickers in no time at all See that's perfect outside help sometimes motivates the team to get working Change it themselves. All right First time this first table to get done. We're moving on to the next one every factors after lunch. We do the factor I think we've run out Okay, so that's done. All right, so what's the what what makes this pass? That's a tough one. There's a big change so there's a There's a mistake almost everybody makes because there's no curly braces notice This is outside of that while loop. Don't accidentally slip it in there You'll get an endless loop. You've probably already noticed that Okay, I made that exact mistake Did Jonathan help Okay, no stick no stickers for you. Okay, so break time Come back