Red, Green, … now what ?!

so thanks for going this feels like it’s a conference heavily weighted to change management a girl and waiting and energy type stuff so we thought we can inject a bit of actual programming because this place you can’t be agile less unless you write inhabitable code as you keep your coke clean and leave and it’s not always obvious how to do that so we’re going to do a bit of light coating called Jim’s gonna do it was like I said so I’m Kevin I’m a I’m an XP coach be doing it to pee since 1999 and before that I’m doing not XP and this is Jim I’ve been working with software in a few different domains every boss seven or eight years now working for micro necklace doing network automation with lots of refactoring so what we’re going to do is who he is who here actually writes comb all of this day basis okay good you’ll all probably have heard of pragmatic tapes back to the sham-pow from cancer it’s using interviews all across the northwest i have heard of people turning up for interviews and they say right the checkout counter and they do in it exactly the way we’ve done it XD Manchester or whatever I don’t know the ending part answered questions on it because solid days what other people do we’re we’re going to do that we’re going to do it in a highly reflective way so we can so that we can talk about the refactoring part of the TDD okay so and just before I start everyone hear me okay at the back and I’ve got quite a quiet voice normally so have you can’t hear jeans okay so let’s dive into our coach straight away so again can everyone read this okay from the back yeah good so those who know the check out cats will probably recognize this test so we have a check out we scan something and we check that it’s 50 so forgetting about better name I’m just going to check it’s a comment test scanning 18 so the Assumption with this test at the moment is whatever we scanned the price of is going to be 50 everything has the same price so we go ahead and make our checkout class so that was the that was that was the this is the red part of the red green refactor fright we’re writing a test to and to demonstrate the business logic that we want to code up okay and is currently road so she can go through get to compare to be on a scan method and the current balance method and okay it’s written in it because the moment got as 50 we’ll just set that to zero but now they’re really comfortable with Java it was like a kind of lowest common denominator and then i’m not sure if everyone can read that at the bottom bunk show you can see that the red mama at least so we’re saying is expecting 50 but was syrup so that’s the red part right we’re ready now so with we often we’re on the TDD cycle okay so now we’re get do the simplest thing we can to make this pass so the simplest thing we can do right now is just get our current balance method to return 50 so we run our tests now and we’re green so so that’s red green and the next thing

we’re told to do is refactor and mm-hmm has anybody simply not read k becks te d by example so lonely only a small number of you had already it’s a brilliant introduction to DVD it’s it set the scene for what was to come but I’ve got a massive problem with it and and books of alch which is that Kent and the other experts tend to say okay so we’re going to refactor now look some duplication here why man magic stuff and it’s gone and sometimes I can’t even see the duplication that I don’t I don’t necessarily agree that that’s what should be done next refactoring to these guys is is based on years of instinct and insight and they kind of forget that the rest of us have gold that haven’t been through that to their experiences and they just say hand-wavy hand-wavy some duplication here and sometimes you can see it sometimes you can’t sometimes you agree that yes that’s the most important thing to fix but not always and it’s not always obvious the refactor step feels like it’s very arty very sort of subjective so we want to talk about a kind of an approach to tackling that so Kent end and his pals talk about co spells and they’ve all got funky names and it’s not always obvious what those names mean so you know what’s the difference between feature envy and inappropriate intimacy that genuine code starts genuine names for the codes Mazel of what what how does it help what’s it what does it mean it’s it’s it’s kind of subjective they were given funky name so that you won’t remember them and most people remember the names but actually what those names are intended to me an awful lot of people seem to forget so there’s an alternative which is can I sins which was first described about 20 years ago by my love-hate Jones in a in a couple of really impenetrable computer science books but connexins basically says coupling is what kills you when when when the experts in te d do their hand wavy still talk about duplication code smells and stuff or they talk about is complex but they want us to decoupler code refactoring is about decoupling things and if we need couple things then you can change one of them without needing to change the other so coupling is what slows us down duplication is another is another term for coupling but it’s kind of too wishy-washy so Paige Jones try to try to produce a kind of algebra of coupling and this is it so there are nine kinds of kinases and they go from not very serious to extremely serious and what we’re going to what we’re going to demonstrate this afternoon is that by by recognizing which kinds of cases we’ve got in the code that tells us what sequence to do the refactorings in and what kind of refactoring to do in order to deep deeper below remove duplication so we’re going to use Canadians as our signpost to help us do the refactoring step to help in some ways take away some of that hand wavy can Becky this is duplication will just make it magic it away sort of stuff so we’ll do a quick a quick run through of the night kinds of kinases and don’t expect much of it to stick this is just a very quick overview during the live coding we’re going to visit six seven of them in detail so for

the first of all places of name that means that two areas of your code are composed because they both know the name of something so for example in order to call a method you have to know its name if the name of them changes the code that’s calling it won’t be able to if the code that’s calling it changes the name of the method it’s calling that it won’t call the method it was calling yeah so that’s coupling between two things they both have to agree that that’s the name of the day that they’re both going to use variables class names constant names yeah it’s kind of inescapable we have to know the names of things in order to be able to talk to them but it’s worth knowing that we’re putting that kind of coupling in the code so Canadians of name is is all-pervasive it’s it’s not particularly serious but it’s worth remembering that it is coupling yeah so the next kind is Kinesis of type 2 pieces of code uncoupled because they both have to agree on the type of something so when I’m calling a method I have to know what what the return type is when I’m instantiate an object I have to know what what class I want to instantiate two parts of the code are therefore coupled because they have to agree on a type again it’s all pervasive we didn’t have kinases of tight nothing would ever get done it’s it’s it’s more visible in languages like Java that it is in dynamically type language is like Ruby but even in those languages is there all the time it’s just that it’s harder to see it’s slightly worse than places of name but it still is there all the time so we have to recognize that we’ve got that the next more serious cases of meaning where to two chunks of code have to agree on what something means so for example if you’ve got if you’ve got a string it might be that two objects or two functions in the code both have to agree that that string contains a postcode and they both have to agree on what the representation of postcode is in that strip so so so they’re both interpreting that string and they actually cooked it in the same way so for example if you’ve got you’ve got a function the returns null as a special case when something goes wrong that function and all of its clients have to agree what no leaves they have to agree that no means I couldn’t do what you asked me to do or whatever it was so they’re they’re agreeing on the meaning of that special case this is worse than either the other two because he’s harder to spot it’s harder to work around and it causes all sorts of subtle books and going up the scale we’ve got closest algorithm so this is where this is where you’ve got two pieces of code that have to agree on how something is done so for example you might write a test for a fingerprinting algorithm and the test has to use the same hashing algorithm in order to in order to pass so you’ve got the same algorithm implemented in two places and if either of them changes something will break remember this is about couplings there’s two or more places in the code that are joined together by some assumption so going going on the scale again can I sense of position is where you’ve got a whole bunch of things and you have to know by magic which of them if which of the means which so maybe you maybe you’ve got a message both of us that you have to know that the first byte is the message length the second bite is message type and the third bite is the number of fields in the message or something like that and both the sender and the receiver of that message have to agree on all of that and agree where

each of the big bits of that message are so Canadian supposition is really hard to detect causes all sorts of interesting books and basically you’re saying we all have to agree on that this being the correct agree sequence of things so now we’re into things so those first five you can you can and you can detect them by static analysis of code you can you can detect them all by just reading code the remaining four which are all worse you really only be taped at runtime so cases of execution order for example you’ve got em you’ve got an email system and you have to visit the recipient before you can send the email so the client is coupled to look to the way the API has been designed you’re forcing the client to do things in a certain order and everyone has to agree on that in order for anything to work basis of timing wear something only works if the timing is right classic examples of this are selenium tests you’ve got you’ve got UI tests that test your application through the browser and they only work because there’s a sleep of two seconds in at this point classes of value is as it says on the tin to areas of code only worked together because they both agree on the value of something a hard coded value that they both know and finally a kanay sense of identity which is that actually the code only works if you’re talking to actually that that actual thing singleton is a is a classic unless you’re talking to that instance of the thing it’s not gonna work two instances of singleton both with different state whichever one you talk to you get different different view of the world so in a sense of identity basically says global variables are another one your your code is is coupled to specific instances of things so those are the nine kinds of kinases what we’re going to do is yes kinases of value no not not not static values just agreeing on on any magic number or anybody buddy I shall become your looks yes yeah it’s all we’re going to do is we’re going to roll through the checkout counter but paying close attention to which kinds of places we’ve got and what we’re going to do is you’re going to fix things in order of severity so whenever whenever we’ve and whenever we’ve got to the green step what we do is what kinds of connections do we have which one is the worst okay we’ll fix that next we’re going so we’re going to use Canadian says that as our signpost to tell us what to do so we’ve written that first test we’ve made it pass and the question is what kind of places do we have in the code so we’ve clearly got Canadians of tight and kinases name otherwise the thing wouldn’t compile we’re always going to have those they’re always there and that’s kind of vibe we’ve also got Canadians and family you live as well so you’re asking before well what that means so here we’ve got 50 in our check out tests and a 50 in our check out so going to represent this

in air so here we can see we’ve got kinesins and value which is a worse sorts of kinesins and then also Canadians of execution order because right now our scan method doesn’t do anything so I think once get as many things as you like it was still so the client only works because it knows that the client only works because it knows that the total is is is returned here and the scan method does nothing that makes sense so so that the checkout has implemented an algorithm which is do nothing and the test only passes because the test knows that as well just like balloons girl but then the test wouldn’t be expressing the business rule that we want so so the so the the kinesin saw meter says we’ve got places of value which is level eight so we need to fix that first so simplest way for us to do this would be for us to pass in our 50 inter somewhere in our tests so we could either ascend through our current balance method which I don’t think really makes any sense I’ll scan method which makes slightly more sense but let’s go with in our constructors so what we’re doing here is we’re invoking the dependency inversion principle from solid which says don’t depend on details depend on abstractions so right now the test depends on a detail look inside the check out and there you know Joe rains burgers work he always says to avert dependencies pass details of the call stack move details up into the caller so we’re taking a detail from the check out and we’re moving it up into the test so that the test passes it back in so I’ve just passed this in as a constructor parameter at it as a field and now returning it in our current balance budget so run our tests now to make sure we’ve got verde mistakes still green so we’ve got rid of our nation’s a fallacy also what we’ve done is we’ve taken these two ends of this of this connection we boot this end into here yeah so it’s like it’s like physical coupling with tape as a buzzer there’s a piece of string joining these two things and we’ve taken this one I moved it over here so the piece of string is a lot shorter right now right now it’s still there in the test it’s just that we’ve shrunk it we’ve weakens it yeah so so we now we’ve now freed this from the coupling so it’s so it’s not it doesn’t have clay sand sibel you with anything anymore but inside the test we’ve got a little bit of Canadians of value because we’ve because we’ve moved this end into here duplication duplication yeah yeah so we’ve still got that in here and now we can weaken this even more by refactoring this into a local variable which just gives us Canadians of me so refax listen to a very cool item price and make sure we use item price down here and we run our tests which still green so there’s TV the TDD books just just waybads so are fifty-fifty we need to fix that Mason says that’s places the value at level eight on the scale that’s serious you need to sort out before we do anything else and here’s how you do it that’s me yes was that a question so what you end up with what do you end up with is is is a general-purpose algorithm that you feed with specific data yeah yes so so this you rises this is all the 10 back hand wavy stuff you’ve got duplication we’ll fix it this is telling us how to fix it why we’re fixing it what then what the outcomes

are so now we’ve got rid of our connections and value but we’ve introduced by platinum tradition but highlighted conations Amelia now it’s however has some connections for execution so what all of these int’s actually represent whether true values and and because of that I’ve gotten a sense of meaning so the test and the check out both have to agree that we’re representing money using its Andy and it wasn’t obvious before you couldn’t really can really see that before because there were no it’s in the in the in the method signatures for not very many but now it’s now it’s much worse however I think since ometer of the rest of it is telling us that patients of execution who is more serious than that so for now we can their non-canadians of meaning in competitor of it so we don’t have a reason in our tests to implement anything in our government that right now so we’re going to introduce a new test so it has a check out with an empty basket just quickly cut finish so whenever whenever I’ve got a nice and execution order the first thing I do is write another test that fails because I’m calling things in wrong so I’m I’m making that I’m making that particular kind of coupling visible I’ve written I’m writing a test that deliberately does things in the wrong order force it to fail and by making it pass I’m going to I’m going to fix that kinases so that’s that’s my answer to kinases for execution order all the time so we run our tests now we can see we’ve got got failure so our simplest way of fixing this is to introduce the concept of balance for the check out so we say for each time the item is scanned then we increase it by the item price and then not from balance method we just return the balance and then we’re back to green so now what funds and Canadians do we have so we’ve got still got our Canadians of meeting we also got Canadians of algorithm now because we need to know how to create a new check out and give it nice and price in both of our two tests so again our connection so meter is telling us that Canadians of algorithm is worse have the two of these arguably it’s not because it’s it’s it’s within the same class so the coupling is contained whereas what is the claessens of meaning there’s more of it and it’s spread more throughout the system so this is where we where we make trade off and and say for the purposes of this we’re going to we’re going to fix kinases of algorithm first but you would probably in real life with probably fix places of meaning first because it’s more diverse it’s it’s affecting a broader spread of the code so the simplest way we can fix this is refactor this enter a setup or a before block and unit so we start by doing this by introducing fields but with the item price and the checkout and then wait I can’t quite remember the keyboard chalk of the extract method a man adds the forward to it so this will be run before every test so run that now we should still be green so we just need to remove this from our the test and it’s made this bit clearer and

we’ve this verb are the test okay so now we fixed our kinesins algorithm I still got Canadians of meaning so we had we had the same ultimately to places we just extracted something so it was only one place simples so how Canadians are meaning so that’s where we’ve got a monetary value being represented by 50 now I went to a software system once where we represented money by floats in hand doubles so you can imagine Mike it doesn’t mean 50 pence or 50 pounds or something else so was that the spacecraft blew up on the launchpad wasn’t there because one team had worked in inches another p.m. team worked in centimeters and they never actually integration testing this stuff five billion dollars integration tests on the Lopes with live rocket fuel so that’s Canadians of meaning I also worked on a financial rack where where we had money represented by all sorts of things and when we finally bit the bullet and said okay what we’re going to do now is get out of money class we had to change seven thousand places and code kinases the meaning is a really one probably the most interesting anything is kind of it’s in the chem back world it’s primitive obsession and it gets everywhere you think me we only got a few lines of code here but the number of places where that into crops up is alarming already and kinesins allows us to see it allows us to give gives us a tool for saying but we’ve got this we need to sort it out if we don’t sort it out now it will kill us later so to get rid of this i’m going to introduce your money class for convenience i actually got one I prepared earlier and waving that way so the reason we use it the reason we use a a factory method like that is again documenting the assumption we’re making well what the meaning of that magic value is you know you might have from pounds method owner also smaller factory methods you have your documenting the special meaning of that that point so just going through updating all of our references to item price to use our money class instead which means i’m also having to do this for balance which i need to now initialize 20 and change the return type of our current balance so this still won’t quite work need to change our empty check out test so also use it so we run that now at thai green bar and we’ve removed moved equations meaning so now the to the checkout and it’s tests are communicating using an object that we’ve defined so we’ve got rid of the primitive obsession in the in the API of check there’s no mention now of of any type that we haven’t designed defined ourselves class with cases of armor you’re asking the servlet that’s going to use to hide hmm yes I know what you mean this is a tutorial so we’re just kind of using as a as an example of that I quite like to organize my tests around context so each test class war will create a context inject some sort of stimulus and then test for them expected outcomes of that so you could see that in this way that’s insane but yeah we wanted to shoot on place itself or them in here somewhere so covering

fabrications meaning and so now we just worked with Canadians inside informations name which we kind of decided that kind of necessary otherwise which is got things not interacting with other things so so it’s really important observation ability to hammer home here is that the public API of the check out doesn’t mention any classes only types and the only time submissions are ones that we’ve defined ourselves so we’re in control of the meaning of everything going across that interface so now we’ve we’ve got rid of most of these types of cases yes it’s probably a good time for us to have another test so next test is going to be different items of different prices so so connections there is help to know when to stop yeah we there’s a lot less hand waving we can just say we got rid of all the series kinds of coupling so now it’s time to move on okay so here scan actually did nothing and and yet we got 50 out of the current balance we could have scanned two items three items 12 items and still would only ever get 50 out of the current balance so there’s no relationship between the number of items were scanning and the price we have to pay and importantly our tests knowing that and understand that and that’s that’s where the kinesins comes in so our tests know that or knew that scanning this however many times wouldn’t change the change the result so we got rid of it by the redesigning algorithm so that scan did do something so that every time you scanned item it’s incremented the bars we and we test drove that so it’s at lectures so current balance was effectively relying on the fact that the executioner Habiba that 15 precisely won’t scan before yes and that’s the only way the test worked to all these things were coupled together because of that yeah yeah yes so the failing test causes causes to fix that and we just fixed it by you know doing proper program there’s no refactoring magic thing makes sense so if we had our next test that and you scroll up of it or can you move or can you move the new test up yep Kevin all over in the church you’ve got this next tax that you’re going to do instead of doing the empty task that I’d be would you’ve got a different powerful with are you specifically Joe we chose chose to do the test empty basket for example rather than highlight that with just two items yet so so if so instead of test empty basket we could have we could have scanned to ice yeah and it would have exposed the book just say yeah I’m just wondering in order to be the different design decision what we’ve done that no look well that’s an exercise for you to try home and report back using the agile nine hash it out we deliberately sprinkled this with exercises for you to do so I think he gets you to the same point I did when I tried it but then I knew where I was heading so we’re scanning two items now so we call the first I temp a second item B and we’ll also need to update that in a minute so then so the attention of this

test now is that we can have different items with different prices yeah we’re beyond just assuming that they all have the same price so if we say our B item costs 13 p so a plus B will be 18 so before we can run our tests we actually broken this test because we’re not passing anything in here so we we just add in I’d say our first item with an a 20 is our item price so we run our tests we get our our failing test now the simplest way we can get this passing is for us to check whether it’s an a that we’re into scammed before we increase the increase the balance so if we say if the SKU is a and increasing the balance by item price otherwise increase it by increase it by 30 pets which was our price so this is that this is a critical part of the red green refactor cycle the green part you really have to switch your brain off and just type when you when you write the test when you write the failing test you’re designing the expected behavior of the sire of your black box so you’re kind of being business analyst when you write the group of the when you make it green you just want to you you want to switch off all the thinking parts of your brain and just make you pass some just drive a bulldozer through the code just get into green as fast as you possibly can it doesn’t matter what damage you do in achieving that as long as you get to the green bar as quickly as you can that’s the next safe point and annex at that point then we can look at the places on earth and so what damage have we done what you need to fix now and with we’re saving fixing it because we because we’re Greek blood tests hold in the zone I think you describe this quite well the other day when you said when you write your tests you’re designing the outside of your class and once you agree and you’re refactoring is designing the inside of your class which I thought was quite a nice nice way of putting it so now that we’ve got our tests passing with our check in here we can have a look and see what what types of conations we’ve gone so we’ve got kinesin some value because we’ve got an eight in both and also creates value because we’ve got 30 pounds we’ve also got Canadians of algorithm here is where we know how we increment our balance in both places so and Canadians of meaning which I missed that which was for our sku both the tests can recode those understand that this string represents in sku and for those not familiar with the check out cutter sku is stock keeping unit like a barcode so awesome some school yes yes there would be some extent well not necessarily ‘used because it would just be that the price of an a and the price that would be would be 30 not necessarily the relationship between the two at this point so how r kinesin somerset tells us that canadians and value is the worst type conditions that we have a moon so we’ve got to hear i’m going to start with the Canadians of value in the birds and pence so again as we did earlier I’m just going to push this and pop the stack into here so go

we have this in our constructor change our constructor to reflect that so mr. field and then again rather than fifth there we can just use price of B so she can run my tests make sure not many mistakes ok that’s pretty so far so again we’ve taken retaken hard coded value from one side we’ve moved into the other side to inject it back in so this is this is taking that piece of string and moving the ends of it closer together and one outcome of that is that the check out becomes generalized it’s more general than it was before so before we move on just for clarity on cancer update these names so we’ve got price of a rather than night and price ok so we’ve also still got kinesins of value in our in our test here so even though we’ve made it weaker we could make this even weaker still by K pad changing it to listen to me so extract this look variable here call it price and feet and then change this to a local field ok so now if we start using that in here and we’ve reduced that kinesins now we’ve still got item price hair so scare quickly quickly rename this as well so that the price of a arguable there’s some kind of kinases there between what what the variable was called and what it means so it was called item price but it meant price of a there’s there’s something going on there I can’t quite figured out what but we know needs changing so packing in our diagram head we’ve got rid of our kinesins and value but we’ve introduced Canadians and position in fact so Canadians and position is where both e in our case both the tests and the code rely on the order of the constructor arguments so we have to to money objects coming in here so it’d be quite easy for us to get those the wrong way around as so the check and the test both have to agree which one means B which one means Oh it’d be really easy to call that constructor with them in the wrong way around and that will break the test but that’s why it’s that’s why it’s let level 5 on the scale it’s it’s it’s worse than some of the others because you really have to detect it using tests so before we incorporate okay so before we move on to fixing that we’ve still got Canadians value with the a in both tests so like we’ve done with with everything else will press has a into our constructor so again we’re moving this up the stack so we change our constructor to take item a sign that to a parameter then start using that in our scale method now believer our tests they’re still green however so again we’ve taken to hard-coded values in different classes we’ve moved them both so that they’re in the same class so we’ve we’ve taken that piece of string remove the ends together and now that we’ve done that we can eliminate it completely so we can do that by just we here strap this to a local variable and

then it feels and then we can use it down here and also in our original test now notice that we don’t have the same problem would be because that’s the only place in the Cody to mentioned we can change that to Z and everything will still pass so it’s not just about having magic values of me it’s about having multiple places in the code that interpret those magic values and dependent on them so before we go on up there quickly tightly listen slightly by changing our fields to be initialized up here as I’ve scaly placed there they used good ok so we’ve removed our Canadians of value that leaving us with our connections position so so we still got all the other kinds of places there we’ll get around to those eventually but the place is so much or is telling us this is the old we should fix things in so connections of position is that with relying on both knowing which order their past so simply a simple way for us to get rid of this Canadians is for us to convert its can be Canadians of me so if we have some way of mapping a and item so nice an item and I will get rid of it so if we just flip back to the check out inside the check out constructor here it has no way of knowing what the call are intended here it’s it’s it’s saying I want you to pass price of a first and price of be second but it’s got no way of actually verifying that so classic fix for this if you’ve got a language like Ruby where you can have name parameters you could actually insist that that the caller use named parameters here what we want to do is associate things with these things so that we know which is which sort of so that both so that the caller has to use our names we’re in Java so we haven’t got named parameters so the next best thing is actually to pass something into the check out does that for us associates the sku with its price the obvious thing for that is the dictionary or on that so we’re going to fix this by passing in a map that says item a has this price item B has this price makes sense so those those two parameters to the constructor because they’ve got the same type the compiler part helpless we can only we can only know at runtime with tests whether we’re calling it correctly so if instead we we actually create a data structure that map’s those names to their meanings then fewer opportunities to get it wrong we’re reducing the places of position to places of name and so as we’ve done before we can pass a map into our construction of the check out so I’ve got a simple map set up between string which is heavy I receive and money and I’ve just populated that without later so to import ashman oh yes thank you so again if we passed the inside constructor and a pleasure constructor to match that in the end not red party fowler’s refactoring book you know what anything have you the classic structure of most of his refactoring moves is create something new migraine all the colors of the old thing so that they use the new thing then you can delete the

old thing what we’re doing here is we’re passing in something new then we’re going to make the check out use the new thing bit by bit and then some point the old stuff won’t be used anymore so we can get rid yes yeah yeah that would have that dad’s cognitive to to scan methods doing different things yeah they stopped compiling for about five seconds so yeah in a bigger system we would have gone more carefully so just changed for when the HK use a to increase the balance by the price in our prices map so the tensions still pass at that point yes stage which still green I voted one use of this stuff so that it uses the new stuff instead so we’re gradually migrating code over to use the new thing and then for the from we like we could do the same here we could do prices get sku here because price it be we’re going to get from from the price list as well so the test shoots too at this point yeah so we’re still green so now we’ve got it when we don’t meet so i guess we could argue this is canadians algorithm but we’ll all just get rid of and artists should still pass rate so we’ve got rid of our nation’s of position so we’ve got some stuff we can delete from the checkout we do so we’ve got our prices and our item pick which are you say more so we can safely get rid of those that just to make sure the fields as well and then we can do the same for our method signature so if we remove the item price of a and price of V that leaves us with quite simple we can check out them and we just ran our tests again so we’re tending to find that every every time we fix some Canadians our code generalizes because we’re taking we’re taking something that it was coupled to and moving it out an inch by inch we’re building a general-purpose check out class but we’re doing it in a very controlled manner so another common thing is we’ve been moving between types of collations so we got rid of Canadians of position there but we have introduced Canadians of value which when you didn’t feel right earlier on when we when we had our be here and item a so now we’ve got the opportunity to get rid of that so so the two ends of this one are in the same class so it’s not as strong as Canadians with the checkout itself but it’s still Canadians above you so it’s high of the list so let’s get real good now so so connexins isn’t something you can get rid of all you really do is change its nature is is converting from from one type to another what we want to be doing is converting it from strong serious nasty types to the weaker less nasty types and the whole thing about the Canadians orbiter is it tells us which direction to go so I I’ve I’ve I’ve seen places where a factory goes round in circles you know what will will will fix this feature Embassy will move that into there and now now it’s exposing something to over here so we’ll move

that over to there now we’ve got a huge class so we need to move these things out but now we’ve got Pedro again so we need to move back in and you just just following the code smelly type refactoring often just take your any circles or never ends with Canadians you you’re saying this is coupled to this for this reason and what we have to do is move that over there or move that over there or move them both over to here and it tells you when to stop so at this point we’re left with just Canadians of meaning for our string which is our sku in our scan method so we know that what we’re passing in here and receiving in here represents an sku and our placements are meaning the map that we know that the it’s a map between the items or the item s can use and their prices so another way to think about that is that the public interface of the checkout is expressed in terms of tights we didn’t define and because of that the checkout and its clients have to agree on what these things mean we’re getting a map of string to money it could mean anything we all have to agree that it actually means this is our priceless we’re getting strings here and strings here that represent product codes or sk use or bar codes everyone has to agree that that that’s what they need and conversely that we’re representing product codes using strings so this is the primitive obsession thing again that I think that it’s really easy to spot the put the public interface mentions types that we didn’t define so in the spirit of leading things for for you to their own that’s an exercise you can have a look at is how you could how you could refactor those out and what kinesins that might introduce further down so so we’ve done quick whistle stop tour of seven of the nine kinds of kinases here so we’re just going to quickly rattle through what we’ve done and then we’ll be time for discussion so we started off by scanning a single item we had our red red failure we got that’s passed by just returning 50 and that left us with four types of Canadians Canadians and value because both in both classes know the price and Canadians of execution order because scanning doesn’t have any effect on the balance so that was where we have that empty scan mirth and then also incarnations of type in a sense of thing which we can leave in there so we fixed that by introducing the constructor parameter and I interviewed that highlighted are Canadians of meaning but still left us with connects Canadians of execution order as well so we fix this by writing a test for an empty check out we got that to pass by moving the adding to the balance in our scan method and then that left us with common common setup of our checkout which is kinesins of album so we removed that by introducing our before book which moved our checkout and the item price of insert fields on our tests next we’ve still got Canadians of meaning which is where both classes knew that we were using it in to represent

money so we got rid of that by introducing our money class that’s just left us with Canadians of type and kinesins of name again so we added another test for different items with different prices got that’s passed by just putting in it in our scale method and by doing that we introduced Canadians of value because everyone knows the everyone knows about the a SKU anyone knows the price would be we’ve got Canadians of algorithmic as we’re doing bounced i add in two places so the two branches of the if statement the work they work they were different but the code only worked because they were both doing balance side then we’ve got Canadians of meaning where we were using strings through represent our sku use so to get rid of our kanisa to value we introduced a parameter for the price of be that introduced kinesin supposition so the case in some position where we had both of the monies being passed into our constructor so we still got Canadians of value which is our worst thing at this point and Mason’s found rhythm in cases of meaning still from the last fight we just beaten the world record to same work Mason’s so we introduced the parameter to get rid of their case sensitive value for the item sku leaving us with Kayson supposition canadians algorithm kill a sense of meaning we introduced a map to get rid of the nuisance of position so that assign names to our prices leaving us witches conations of meeting relationship type so spoiler forever on the scan dealers at home we could introduce a price list to get rid of the Canadians of meaning further in map and a product code for the sk-ii so I was really quick tour license there’s an awful lot to take in and had awful lot of what you’ve seen is still subjective because there’s we’ve got in a down different routes of different types but I hope you get a flavor of what connections can do for you to bypass some of the hand wavy overlook this duplication kind of stuff and so any questions yeah i still find out more about this and some way of Z placed the original originals writing on canadians is about a page and a half in a terribly book about computer science it languished there for about ten years until jim Wyrick did some talks on it at Ruby conferences in 2004 the only other place where it’s been written about very much is on my blog and the last 10 or so articles around Christmastime I did the check out cutter again with different starting tests and therefore different series of cases it really isn’t much written about this which is kind of why we’re doing the talking so these slides are on SlideShare just look for me on SlideShare and you’ll find these that’s it really we’ve got we’ve got got these you can take away their our laptop stickers here as well but yeah unfortunately very few people are writing about this yeah what’s impressive house just to check out of sample size it’s probably there but all

the resources they need you to do some real action to remove some duplication feeling some interesting curse of duplications yeah linear to commission some executions yep which doesn’t reduce test well that’s the that’s the way I tackle it this is that it is the duplication represents duplication in the center position that’s the problem with the word duplication there’s lots of different kinds of duplication of knowledge of intention of actual text in the code and what you’ve got them with cases of execution order is duplication of knowledge about how the algorithm works how it has to be cold and it seems to me that the simplest way of bringing that out is to rice failing tests so that’s what I always do if anybody comes up with it with alternative that would be grateful to hear about it I personally I’m a fan work and write writing an extra tests kind of captures that knowledge it’s real way they seem to be a kind of complementary of different stages this one’s about refactor stage it is and I prefer this and I I think because the transformation priority stuff feels less scientific process yes I would look I would love for somebody to to take the nine kinds of kinesins and do for each of them a catalogue of the main kinds of refactoring you do I mean there isn’t going to be a one-to-one mapping you know but there are definite there are definite tools and patterns that you can use with kinases the transformation priority stuff I don’t know it doesn’t feel different clears ferrara somehow i mean this is this is still highly subjective yeah this that this run-through through we’ve just done it’s about the seventh we tried you can do things in different orders you can you can and and initially we started with different starting tests and we went off in a totally different direction so we chose these starting test because it allows us to show you seven of the nine kinds of places so and and even then the first few run throughs we missed some of the cases we didn’t spot in till later so risk of learning how to do one thing I’m but what problem that we having a bachelor antique a huge kill bears yes we’ve got a whole bunch of types which are logically the same but whichever to come come by different we’ve got probably half a dozen different represent different classes representing the 3d position in space represented millimeters in so in a sense each little chunk of code has developed their own types with our lady I yeah I’m sort of code I end up writing tends to be talking to this geometric alone get them out of here general into their seem to spend all its time translating between different but incra but functionally equivalent little types right which again feels a different sort of duplication I’d appointment well maybe it is maybe it isn’t I mean if if this over here is one bounded context in domain-driven terms this is another value context in domain-driven terms

they leave got their own definition of appointing in space that works for them that works in that code yeah arguably the only problem is that they’re exposing those to you yes so so you’ve got Canadians of cleaning in the sense that you and each of them have to work you have to agree with this one when you’re talking to this one and you have to agree with this one you don’t this one I need I need to know how to build one of these from one version Gaspar’s so another another response to Canadians of meaning so in every case here we introduced a rapper type another response is to choose a rapper layer to hide the API that mentions specific types behind something that mentions only times that you’ve defined so so you would have you you would write it you would drive for each of the two things you’re talking to you might write an adapter layer that hides the details of them and is written from your point of view in terms that you’ve defined well necessarily because because inside inside the body of your code you would have your own representation of a 3d point and you have to thin adapter layers that convert that to and from the 3d point over here and 3d point over here so in your code you don’t you might have fewer places where you’ve got to do that check so that’s the basis of hexagonal architecture you have anti-corruption layers around your code that protect it from the stuff coming in and going out to other parts of the world that’s another approach to a source of meaning is it is a kind of adapter layer of facade wrapper layer type 3 thanks so much thank you anything anything else yeah sorry really sunny parties well that’s the interesting thing because the bottom the bottom view can be figured out through static analysis but the top few card there’s no way looking at code that you can figure out that this only worked because it’s pointing at the same thing that that’s pointing at so the answer is tests anywhere else stunned into silence okay couple slightly envision it thanks for coming you want to know any more about this tappers up the rest of the conference there are some places on winters here and some laptop stickers the kinesin summonses as well also have a blog addresses on if anyone that’s really