Getting a Handle on Legacy Code

Video thumbnail (Frame 0) Video thumbnail (Frame 1971) Video thumbnail (Frame 3523) Video thumbnail (Frame 10107) Video thumbnail (Frame 11952) Video thumbnail (Frame 13063) Video thumbnail (Frame 14046) Video thumbnail (Frame 20475) Video thumbnail (Frame 23017) Video thumbnail (Frame 26593) Video thumbnail (Frame 29197) Video thumbnail (Frame 30726) Video thumbnail (Frame 32326) Video thumbnail (Frame 33273) Video thumbnail (Frame 34243) Video thumbnail (Frame 35233) Video thumbnail (Frame 36627) Video thumbnail (Frame 38639) Video thumbnail (Frame 41827) Video thumbnail (Frame 45492) Video thumbnail (Frame 47203) Video thumbnail (Frame 48233) Video thumbnail (Frame 49501) Video thumbnail (Frame 53740) Video thumbnail (Frame 58410) Video thumbnail (Frame 59429) Video thumbnail (Frame 61463) Video thumbnail (Frame 62674) Video thumbnail (Frame 63729) Video thumbnail (Frame 64780) Video thumbnail (Frame 67828)
Video in TIB AV-Portal: Getting a Handle on Legacy Code

Formal Metadata

Getting a Handle on Legacy Code
Title of Series
Part Number
Number of Parts
CC Attribution - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
We all run into legacy code. Sometimes, we even write it ourselves. Working with legacy code can be a daunting challenge, but there are ways to tackle it without taking on the risk of a full rewrite. There is a deep satisfaction that comes from conquering a nasty piece of legacy code. The ability to achieve this goal depends on both testing and refactoring. We'll learn how baby-step refactoring techniques lead to a better understanding of the code and a high-quality design while always keeping the code running.
Programmer (hardware) Touchscreen Multiplication sign Machine code
Point (geometry) Scheduling (computing) Presentation of a group Run time (program lifecycle phase) Multiplication sign Direction (geometry) Electronic program guide Device driver Control flow Parameter (computer programming) Mass Rule of inference Computer programming Product (business) Number Mathematics Atomic number Code refactoring Software testing Data structure Descriptive statistics Social class Task (computing) Physical system Covering space Stapeldatei Constraint (mathematics) Demo (music) Inheritance (object-oriented programming) Cellular automaton Mathematical analysis Planning Bit Machine code Cartesian coordinate system System call Planar graph Category of being Type theory Data management Film editing Process (computing) Speech synthesis Right angle Navigation
Point (geometry) Slide rule Algorithm Group action Dependent and independent variables Identifiability Computer file Java applet State of matter Multiplication sign Control flow Machine code Mathematics Word Software testing Moving average Physical system
Reduction of order Software testing Machine code Computer programming
Point (geometry) Standard error Building Blind spot (vehicle) Patch (Unix) Parameter (computer programming) Shape (magazine) Mereology Inequality (mathematics) Formal language Mathematics Goodness of fit Universal product code Atomic number Computer network Software testing Data structure Form (programming) Social class Area Algorithm Touchscreen Weight Sound effect Database Machine code Instance (computer science) Variable (mathematics) Cartesian coordinate system Category of being Computer animation Triangle output
Weight Source code Keyboard shortcut 1 (number) Sheaf (mathematics) Water vapor Machine code Parameter (computer programming) Mereology Complete metric space Number Expected value Mathematics Computer animation Software repository Different (Kate Ryan album) Square number Software testing Right angle Code refactoring Physical system
Covering space Point (geometry) Smoothing Multiplication sign Bit Function (mathematics) Machine code Product (business) Word Personal digital assistant Software testing Pattern language Social class Physical system
Computer file Block (periodic table) Multiplication sign Device driver Computer font Theory Computer animation Ontology Network topology Representation (politics) MiniDisc Right angle Error message
Differenz <Mathematik> Line (geometry) Fiber bundle
Area Group action Arithmetic progression Graph coloring
Different (Kate Ryan album) Cellular automaton Multiplication sign Software testing
Computer animation Multiplication sign Bit Object (grammar)
Interpolation Computer animation String (computer science) Multiplication sign Duality (mathematics) Representation (politics) Bit Software testing Function (mathematics)
Point (geometry) Group action Computer animation Information Multiplication sign Software testing Right angle Mass Machine code Writing 2 (number)
INTEGRAL Cellular automaton Multiplication sign 1 (number) Function (mathematics) Parameter (computer programming) Machine code Directory service Line (geometry) Mereology Food energy Subject indexing Radical (chemistry) Computer animation Personal digital assistant Cylinder (geometry) Network topology Software testing Endliche Modelltheorie Window
Area Point (geometry) Multiplication sign Weight Machine code Line (geometry) Mathematics Process (computing) Loop (music) Order (biology) Software testing Code refactoring Contrast (vision) Row (database)
Computer animation Computer file Root Different (Kate Ryan album) Network topology Software testing Extreme programming Directory service Proper map System call Rule of inference
Area Computer animation Bit Machine code Hydraulic jump
Computer animation Multiplication sign Analogy Machine code Line (geometry)
Intelligent Network Constraint (mathematics) Confidence interval State of matter Multiplication sign Software developer Weight Control flow Set (mathematics) Machine code Line (geometry) Product (business) Twitter Computer animation Personal digital assistant Network topology Order (biology) Statement (computer science) Software testing
Group action Functional (mathematics) Multiplication sign Direction (geometry) Web browser Mereology Rule of inference Computer programming Product (business) Mathematics Goodness of fit Hacker (term) Software testing Code refactoring Series (mathematics) Form (programming) Bit Machine code Cartesian coordinate system System call Process (computing) Factory (trading post) Quicksort Freeware Boiling point
Computer animation Computer file Branch (computer science)
Group action Observational study Equaliser (mathematics) Set (mathematics) Bit Machine code Lattice (order) Radical (chemistry) Mathematics Word Computer animation Core dump Software testing
Point (geometry) Particle system Complex analysis Loop (music) Computer animation Universe (mathematics) Software testing Machine code
Direction (geometry) Expression Variable (mathematics) Graph coloring Formal language Mechanism design Computer animation Order (biology) Self-organization Software testing Iteration Code refactoring Collision
Point (geometry) Noise (electronics) Group action Observational study Bit Insertion loss Machine code Instance (computer science) Line (geometry) Variable (mathematics) Equivalence relation Subject indexing Mathematics Word Loop (music) Computer animation Factory (trading post) Green's function Right angle Software testing Code refactoring Abstraction
Point (geometry) Noise (electronics) Computer animation Inheritance (object-oriented programming) Multiplication sign Selectivity (electronic) Pattern language Line (geometry) Machine code Social class
and and and so on I get afternoon thanks for coming to my workshop today uh hope you enjoy it I hope they can keep you entertained enough to stay in the post lunch food come that I have gone on to them so notice in the room the sides darker decides brighter and so there's more going on the screen on the sides of my bill harder to read the code on the screen uh but if you like light that's great and be sitting for a lot of this and you'll see past the podium which is either a downside a bonus another pretty to look at so that you might wanna sit over there so I could you imagine around if you want those instructions for the the lab up on the screen will need that for a little while so you have some time to get that go on and a for Wi-Fi goes down waffle without sorry and so a sense how many of you consider yourself relatively new Ruby programmers like maybe less than a year if you and everyone else from the experienced as can wanna know how much explanation when you do you answer most of those Russell Turner of tried explain things as we as we go if you have questions stop and ask because right so how many of you had to deal with legacy
code before the a lot of how many of you had to how many of you with legacy code yourself that we need to and we all have at some point so this this workshop is based on a code Carter called the Gilbert rose cut you can look it up online is really cool column that is planar because I'm not gonna get into rails here you know Israel's Conf I know but it's it's really it's an exercise it's like bite-size enough that we can run with the journal trouble but we probably won't get done this is our have workshop of the schedule the poster on the conferences will but unclear what we actually ended 3 20 so just before the last talks a lot and I'm happy to say that after enough questions there's no real summary after us so we can say that if you want but I'm applying to wrap up for 320 just so you get about AusTalk if you're planning to and if you wanna hang around happened that although my colleague Adam copy speaking right after me and I can once you start but I can see some of them factor so so Gilbert rose kind were starting a job
today and working for the gilded grows and and work in inventory management system at the end if you play world warcraft you recognize all lot the references in this code no pro have fun with it so in our system we have a bunch of items that we stock in the cell and they all have a cell value number of days that we have to sell the item and all the atoms have a quality and the quality degrades over time so so valuable the animals and our system runs kind of like a batch process to the end of the day and updates both values for all the items in the inventory then there is the specter you do not need to read this wall of text I just know that there is 1 we have no clue of we can trust documentation lies right you can want a system where the specter written months or years ago the code mayor may not reflect that so this might give us a bit of an idea what the system's about some of the rules in the system but we can't trust it we don't know if it's true we don't know if the code matches at so we can use it as a rough guide but you don't want that too much reliance on because some reasonable of from a new at runtime we have a constraint there's a troll that works on the item class and he does not shirk over ship of this code that actual Qatar description says that if you change this code here 1 I knew that so we cannot test items item class or the items property so that's a constraint there's no collective preneurship honesty and this is not a workshop about how to deal with problem people on a team so we'll cover that today much as we probably wouldn't reality OK now with legacy code if it's running and role working and you don't need add features there's no reason to touch it so we're done we can go now we have a new task is a new kind of item in support called 100 items and Kondor items degrading quality twice as fast as normal so that's what's special about them and that's so we have to support in our system so the way structures workshop would do a little better presentation demo this is probably the most all talk at once and the whole workshops were going to do some mob programming and reduce some small small small repair individual work and you really can play by ear between those 2 of them operands working in July can it will keep doing that if it's not working on it can work on your own at school too if you are just take this character at the code and run with it for well on your own and ask questions I'm cool that it it's a really fun kind work on out of a plane with that so that when a star with more programming and and so I'm operand works I'm not sure if you've heard of a government Woody's well nationalities name right kind of change has been popularized in low bit and the ideas super parameter have a driver role and navigator also the person with the keyword analysis driving person's navigating strategic direction that kind of thing with my program you have 1 driver and everyone else is either a navigator research yourself like you need to look up an API call somebody can have the computer and look it up but there's 1 person actually taking the code and that person is mostly typing what did the people are telling him to type you know all us do this and this and that and so I'm going to be the driver for this is having to get different people appear working on my so this work well and you're gonna tell me which direction to go on and we doing some guidance because I have a few points I like to get across all this but it's going to be pretty organic and really driven by the idea is you come up with I'm going to try to steer things in certain directions and so how do we safely change legacy code any time you have to test legacy code that's your questions like I have to change the stuff but this starts running in production it's meeting somebody's needs maybe making my business money or it's helping people whatever your application is doing for all that legacy code is gross nasty it is working mostly it is doing something that is valuable for somebody you can afford a break them OK so as much as you wanted just on the code when you look at it remember that it's actually providing value there is something there and you have to respect that and so a lot of people just say hey understand rewrite this mess there's a lot of blood and sweat tears when in that code and a lot of learned lessons that are hidden away in the mass and when you do a rewrite we forget about this stuff so we need to respect that this code is actually do something we need to be safe which changing if you work with legacy code you need this
book by Michael Feathers skull working effectively with legacy code it's a few years old now but it is cold is pure roll there's so many good techniques in there so in in that book he gives a legacy code algorithm which will be following the 1st thing you do is you identify change points so you have a feature you need to add need to figure out what can you change in the code word word where the where does this change need to happen and then you need to find places where you can test the way we safely change legacy code we get tests around coming I should mention feathers definition legacy code is code without test the definition I like Morse code iden right but really I think is this a little more politically correct so will go there's these fine test points and then you need to break dependencies because a lot of times the stuff you need to test is too tightly coupled everything else in the system you really can't get out at then you write the tests then you make the changes and refactor the code so I actually um tweeted about Michael the other day because I was curious how he thought this book applied to something like Ruby is Ruby you have things like metaprogramming monkey touching which use right more about C + + and Java stuff which don't have those tools and so his
response was you put more effort into characterization testing then dependency breaking below the latter is trivial so Ruby the dependency breaking is less onerous because you have power tools but you still need to use some of it but that the characterization testing we talking about minute is warm water part so on thank Michael 1st 1 item that was cool and I have a slide up earlier here's the code for the workshop and you can call that if you want if you haven't already a um double the Brabham problems of a couple things action did just push up a change of threat to include breaking the gym file so it's there if you have a problem with that but let's get to looking at the code and before we do that OSHA requires me to show you this warning that's Occupational Health and Safety Administration if you're not from the states have to show this
warning because the code is really bad so the 1st thing we're going to do is reduce mob programming
or and characterization tests for the kids so as Mr. down now and we will get into code and take a look at the has that font size in the
back is a readable yeah that at the end the correlated as good they're made up so forcefully reminded not respond to command plus so I have to do the hard way here
in the form of to do on this to as a I really get some code on the
screen here so maximize lower as about the
so this is the item class that were not allowed to touch and that's shown on which is your OK so we just look at and see what it's about you see an item has a name it's own inequality to standard error accesses so this is essentially a structure not much more than that so like I said were not allowed to touch such as literal in the corner of the argument today but it's there and that's what it does and let's look at the test the that's all because we have to start with this is how the comes so it actually called the method but I don't see any checks and there's no sheds no expects no nothing so great legacy code and here's the code so so there's a skill that rose class it has we have a database hard written unifies method that's awesome and and then there's this update quality method that I will discuss the role and scroll through so don't try to study that that it's nasty I call this would Gecode has the shape of a all the of the wages and triangles where OK so let's were working on today so the 1st thing we need to do according to legacy code algorithms we need to identify changepoints so just kind of skimming through this code what part does it look like we might have to change to build support conjure items at update quality that looks like a pretty good changepoint notice in writers collection there is actually a kinder item already were in a gas of that's probably not being handled correctly yet we don't know that but probably not but there is there is actually kind right in in the Davis less work with which is good but the quality is our change point so now we have to look identified a test point so what can we test about this that this method there are no input parameters Ms. run to the bottom there's not really a return value so what what can we test about this method which the test point atoms property yeah pretty much this whole method operates by side effects on the items collection OK so and there is no data for the atoms collection so hurrying get at that there's basically at least 3 things I can think of so the 1st obvious 1 is what was just dead area reader that just drop that end up to up like that the problem is we just change production code and we have no tests for now sometimes that's what you have to do with legacy code because it just is not another option but this is Ruby and we have power tools so let's not do that but a 1 plus the production code until we have a safety net work yeah instance verbal get a 2nd way we can do it and and the 3rd way so just gonna make a lot for the items to those were and we work with here and will play some ideas so 1 areas was instance rare will get so we can do the Ophiuchus poetry great so we can do that and there's 1 other way we could do this that we will go with but we could actually reopened items and we monkey patch only for the test we can do that so we're being gives us some tools whereas other languages we'd probably be forced to change production code without to test safety net so 4 2 they were in a go with the instance variable get so I think that's probably most impact 1 but now we have the items so there's no what Michael Feathers calls these are our our sense points things that you can sense about the application that you can actually write some assertions about so that's great so what's the
1st thing we should maybe test on this code we on studying the 1 actually we don't know what it does yet it's unfamiliar or try to do is get a safety net so we wanna go like really quick and dirty the kinds of tests for right here are not the kinds of test you've been taught to write your career OK these are going to be ugly and messy and you're going throw them away at Coraline and give it a great talk about what's Ruby and part of her talk talking about some of the tested on legacy code anybody basically right you can write nasty ugly tests and once you get the Cobra factors we tried the test you really ugly ones away they're just they're scaffolding to get to where you need to be so we're probably going to do anti-patterns and some gross stuff with the test and that's OK because it gets us to where we need to be right so what what might we want test 1st some idea that would get exchangeable always hit only go even simpler than that why we even just test that we can actually get the information out of the items in the 1st place so change nothing more and nothing just make sure that we can assert things about the items as they stand today that some OK and so a lot of this talk is going to be an the system we go smaller smaller baby steps because once you know how to go really small then you can handle any situation most Samuel Doe that small but when you need to you really can it's appointed can't that makes a lot but when he talks about parameters sometimes you really need you really small baby steps just to get through a tricky section and then you can come and take bigger supply it's already attractor and expect about the items so
on going actually during the test of their lives complete no-op assuming letter the monitor keyboard today so I'm going to do a using a beta actually specify here so after no updates and using the ugly or spec tests and answer them using our spec I actually have many test in the Repo if you prefer me test have a specific reason for using our respect for this will get in a few minutes but and I'm kind parasites switch between the 2 I have decided from like the best here the so let's just assert something about like the 1st item and that its quality hasn't changed so when the split-screen hero quick so we can both the misery demand because like the refactoring tools in it that use wherever you like and so it's just take the 1st item and make sure that it's after remember the water Selinus 1st and quality so let us make sure it's um of square couple expects the going be ugly and that's OK Catherine a change in a minute anyway I don't Uzbekistan I call that Solon 1st to call the 2nd it the the controversy in the source code and remote spot OK so passes we can actually since the items now now you can imagine with these tests are gonna look like if we keep writing like this and so instead of continuing down this road and spending a whole workshop on and I wanna teach you about a different style of testing that were in that which is really well for this kind legacy code do we actually care about these numbers in this test to the matter at all right now do we care the right not all we do is care that we don't change them and so there's a style of testing it's actually an anti-pattern but for legacy code works great summers but that kinda require 1 2nd don't the so called golden master
testing courts also known as the guru checks output met anti-pattern and so with the idea is that you get some output from a system never human look it over and say up Deloitte scared say that off the next time you run the test to get the output again you compare that to if they're the same great test passed if the different than the test failed and human has to look at the output again none of the reason this is an anti pattern is that the time you most need this test to be right this problem when you're making you change that you need to get into production to fix a critical about at that point the humans involved are going to be the most restaurant a B and so that have been manually checked the output of that time really bad idea it's a recipe for disaster that's why this is anti-pattern but in this case were working with legacy code we don't care the outputs right we just care that we don't change it and so golden master testing is also we don't even have to look at the output to see if it's right we just need to save it off as this is the master if I change it something broke I need to back up and go so were in use for this and that you know 1 religion called approval for this I have to give me he shared between actors I I did that and then run through the Slavic community college class last week and there's just some things with the command line interface that if I could be improved so I'd like fact Friday smooth like for pull request to the GEM this is like last friday and that nite she merged all 4 of them and was can and and I thank you for attention and it's about coming and grinding work stoppages for using in this next week I'll cover release where now so Katrina's awesome thank you to train after watching this and soaring usage and because it's really cool so let's keep my programming a little bit
and we're in a word bring approvals tutorial
John file and so the way it works actually interfaces really nicely with our spec so it's can required and can and I'm going to change this after 0 updates just keep running but mean strong old 1 individual rights new stuff using approvals so we're in a specified after 0 updates do and so approvals has a method called verify this kind of the main main method that you use and it takes a block and already was verified the items and when it's going users can save some representation of the items out to a file on disk and remember that a single master and that's all we have to do to use this song the go-ahead run this test and that failed and so and to all they do not have a driver ontology answer this font is really
small um there was not should be getting in different and to this is warnings against so here's the actual error approval air because I don't actually have a golden master copy at and that's expected the 1st time so Osipova to note that tree theory of
to and so we can do is return bundle exec approvals verify and so it's showing me a diff of the current approved copy and the new updated or she calls are received copy and it's out a bunch of lines and we can say whether we wanna prove that our answer were being the jurisdictional upper but right now and we're saying that looks good yes go yeah but has a better head-to-head note to get a
constructive for progress there as a performer in and we have a an area why document they're called group can
the the better there's colors are greater than
I will be spent much time in the cell so and so however now we accept that 1 so it's good we go back to remind we run our test and it still interesting ran test after accepting the gold Master and it's were still getting approval where it's not match it's affecting girl detection here called the and so let's go back to run approvals
verify again so the difference
is this object ideas in here every time you run this you need a different object and for the items so we actually need to have our golden master 0 could be a little bit less variable and so we see a little bit more work here Simon said no I don't want to prove that an awful back to remind and will just
weaker testis lower and so what I'm gonna
do and seems particularly of interest and stuff here OK so instead of
just getting the items I'm going to actually map that and just produce a a string representation instead and so this be really simple will make it'll be human readable so that when we are actually looking at these and make some sense so I'm just going to be a really simple I will process up to get up so you can pull down we start working on this on on by the way so you have to try to take everything and typing the it x over the scanner dual string interpolation get string representation of each item In array and let approvals work with that said so run out again and again written approval were in Room to verify about
this time with a little bit cleaner output that's not likely to change every time around the test so what to prove that a new golden master and we run
the test while usual road no undefined
masses so information jet the answer
to write revenue tests Muriel want thank you the green bar so now we have a test it's not actually calling the code at all but at least we know we can get the items out and we can run the test repeatedly as not failing called every time we which is that's a good 1st step especially legacy code sometimes it can take you we take it to this point honestly and so a school so let's do action action actual update here this is going to get a little repetitive but will fix 7 seconds so the actual bodies can be almost exactly the same but before we do the verify we need to actually run our method rights update quality and then verify Adams after that and again that's can use master testing we get our approval where where
run verify and so now it's it's comparing after 1 update notices it's getting like subdirectories and test names from our spec which is really nice if you run this in the test set to provide a complex arguments just 1 the reasons I'm using our spec and for this and it's showing the new output again we don't care what the what's in here doesn't matter if
it's right we see we didn't change it from prove that and we'll go back and reverse specs again and now we a great model so we can keep doing this and how far should we go how many calories testimony to write that's 1 option but in this particular code though it keeps decreasing the cell and a has you and so the dataset we his comment was that we run until the output stops changing so run until basically gets little laid out and that 1 actually work in this case because cylinders keeps going down down goes negative and keeps going native so we can do that here and that would be a good thing to try them out based on take terminal chapter during delighted televised yeah yet yeah they don't actually ever become invalid so and that that 1 that will never stop so on tool to poet of it's called a simple cult that does code coverage and what it does is you run your tests and it tells you how many lines of the code of which ones have been executed which 1 1 which 1 7 so you can run this I actually have evidence or runs as part of test all the time it produces a directory called coverage that has a index at very small you can look at the remind has a nice and integration for this so I'm actually can run this test of coverage and it pops up a low coverage window your little tree actualize and all we really care
about is our goal of rows fossil and dive into that area so it's saying 67 % lines covered and if we go back to the code it is actually I turned up the
contrast of a cell because see it on the left side of the green bottle and on the left was visible at all at the the the small-angle but there's also some red bars there so anything that's red is a line of code that has not been covered yet OK so I when I ran the test all these red lines of code that have not yet been covered it does actually mark it ends and stuff like that so this gives us a sense of how much of our code has been covered so at this point we can say you know probably are tests are quite good enough yet to be really feel comfortable refactoring so we should probably keep going and this is another place for becomes in really nice because we can just kind of these tests are very very similar so I'm gonna do them in the loop so let's start with that so twice to those were doing some discovery factor the test now as long as the worse is we're OK and Julian dinner talk yesterday you sedulous talk on refactoring latest afternoon anybody she did a great job 1 of the points she made in the talk was that you should only be changing your code order tests at the same time never both because I you test a safety net for the codon bias so when change castle that were not to touch the code while we do that so organist literature 2 times and within that we're generated specify what here Ch again these are not assumed this so I wanna keep but during the work for now OK so on various repeat this loop twice and I is gonna start 0 91 and then after that many updates we run the update quality method that many times a 0 or 1 times and then we run verify afterwards so this should be exactly duplicate the test that we just had this running run and not quite and the reason for the it's
another tool where confined and after 1 updates start approves we'd call after 1 update is we actually use proper english hours and were in a go with that for now because again is a
throwaway test just against worried to be somewhere and my rules verify again and I'm and I could actually if I want to extreme spectra church just to be really safe the so about this after 1
update and after 1 updates I can actually differs to files just to make sure they haven't really really haven't changed other than the name I and there's no difference so were were pretty sure we can do this so now minimize verify again enforcement rules doesn't actually walk back up the directory tree to the root to have 2 can get yourself and my command export cluster proves
and so many good except that and will make
sure green now and we're up so
we already know that only doing 1 update only covers your code about 67 per cent so we can try to and and 3 and 4 and a jump up a little bit more go to 5 and try it out disturbed using formal but again we got approval areas as
to verify those so much for
and 3 and 2 and green
bar and was rerun of coverage again and look at the code so there's still some
red bars in here and just a few but still few so try 10 times so this is really kind of mindless just try to get enough covered on the code to be comfortable with that so over to an analog opera retained so
there's 5 updates and 7 and 6 and 8 and 9 now
agreeing and remove coverage so 93 per cent down here it says if we look at the code still this couple lines a code that are still not being covered so I'm askin bump it up I happen to know the 20 works on discovered you that and go approval all those the to at the
end 1 more time with coverage so people talk about code coverage and whether it's useful or not and knew what he she should for 100 per cent coverage or not a not get into that debate a lot they can be it can be handy metric to watch trends but it should be the end goal it should be a tool use in service on 1 cortical but now we are 100 per cent coverage so how confident do we feel refactoring this code now with 100 % code coverage 50 per cent the way yes OK so for confidence are code if was to comment out say 1 of this line and this line that's a break something right the no we have 100 per cent code coverage but there's lines of code here that we can comment out on a test or failed I don't know about you but that scares me what's really going on in this case whom dive to deep into it is that there is an implicit else right here that is not shown in the code and the else clause of this statement is not actually ever getting hit so the code coverage is helpful but it's not a hundred per cent reliable either so you have to be careful with something like that but for now I'm going to call this a good enough what will then and so the problem why were not cover all the lines of code is our tests are actually coupled to a predefined set of items in our methods and those items in the current state do not exercise all the possible cases in this case and so this is actually a dependency that we ultimately want to break but in order to break that dependency we need doesn't test coverage and we need to change the code so we don't have a way to inject new items into this test trees we can add some to the end of the rainbow around lobster suddenly because of the troll so we have to work within the constraints we have and so were after to start changing production Co without being 100 per cent confident that we're not going to break anything but were pretty sure I mean it's a pretty good test is not perfect and I want to make sure that pointed that out other so now it's time to start cleaning up the mess we have at least enough of the safety net were comfortable moving forward such talk about how we're in development but 9 to switch back to Q for a minute for that
OK so we we did that so I talked a little bit earlier about rewriting we do not want to rewrite almost almost never is a really good idea if you remember the browser wars back in the day there is a single Netscape they stop the world to rewrite the browser and got completely destroyed by the and didn't really come back and tell is in the form of Firefox not 2 years this just stop world world-renowned code users to craft you can't work here anymore and they died basically on no Joel Spolsky writes about rewrites and how the whole sometimes there's nothing else you can do but I wanna try to teach some tools that you know if you can't rewrite we do instead sending that's actually talk here last year using the same product and she actually didn't refactor the continuing incremental rewrite on as a very pieces timepieces time testimony as great as also often talking to watch it you have seen it but it was not actually a strict refactoring and so I wanna come differ direction must this straight up refactorings series get to women I get through it all today at given time we have available but was to see how far we can get with we have also remember and more problematic if call a cleaned up but this fall boys control and least leave the code clearly found a week ago hack in this new feature right now and the code would be worse and that's not if you if you have to keep coming back to this code to make it a little bit better every time and over time it will get better at it on the part of your application you touch the most will become the cleanest parts of the application if you follow this rule and that's huge I mean you can't stop the world for 3 weeks to clean code all usually so make a couple little better should go the were and take baby steps that talked about that already and we're not going to boil the ocean we hafta were trying to implement a feature here were paying down technical that were taking longer than it would take to just write to feature a clean system because of the methods and but we have a feature to do and so we can't we can't decided that now because of the speed of 1 workshop will like religions that have all but when you're working on on your your own code keep in mind the goal and the boys skill so make it somewhat better it might not be perfect yet make it better OK so can back has a great suggestion for how to make changes to code you make the change easy which might be hard to do and then you make the change and that's kind of the perforated rotate and then when I was 1st researching action delivery of a talk on this kind of a good recall last year and I found the school of preparing for it and had to include it in certain new programming job if to walk right up to the biggest function the artery factor in front of everyone can like presented by but that's already update quality is the biggest function the yard sort story factor OK Serena keep going the up formal but again if you want this car on the side on your own with this feel free and before as we start factory and actually commit the tests in the prostate so they can work from those so it's going to do that and decrease with it so
I'm actually to create a new branch 1st called the real scarf 2015 to and I need to have some
files so not only in the book so restrict sugar for and the spec fixtures that is relevant good
commit and purchasing aquifers working
excitation she will pull down the risk of 2015 registered why can work on this in your own book for now is to go with them up around a little bit and then we'll
see if he has 1 can do some Paris 1 group work work equality mobbing again so we have our tests we really don't need to look at them again probably for now and so we wanna do like a set lost a focused so let's there's there's some messes up here but those are 2 kind of the core of the problem and was focused on optic always sets where we know we need to make the change now for a look at this code I don't really want this is the least and every about this code and this is not what I wanna start making huge changes because I don't understand it yet and I wanna study for 3 days to figure it out so what I wanna do is to really simple brain dead seeming changes just to try to get a sense of the code is like uh a potter working with a claim this kind of feeling the clay all that kind of word in with our meeting here which is the code and soaring to try to make really simple changes and we want some sense of whether we're making the world a better place so wonderful problem terminal for a 2nd and I have a recast
called flaunt an eraser tool by Ron Davis uh who's speaking right after this workshop actually on many test your flogging what it does is it basically gives you a sense of how your code is it when something called the cyclomatic complexity analysis on it should been phrase but basically tell universities fog actually to ugly that used by cotinus a particle plans score is what fluxes so around this and see were starting so the overall code base has a score of 180 . 5 and the update quality method as we surmise is by far the worst 255 . 1 so that's our starting point and we can see how we do against that as we go along so so back to the code
which something really really simple we can do this code that would make adjustable with better or easier to understand that means and so on you would you all what do I was you in OK so actually make this and idiomatic Ruby each loop instead of this thing it was ported from the shop and so the 1st you
said do something with items I here is a i the and here we we is OK so I think of
stars and so on do a couple mechanical things that no organs the language at the legislature and on the direction the same so what he's saying is we do something with items I like may be extracted to a variable collide and that would then become the iterator variable Soneji this baby steps like i talked about so the 1st thing to do now review my has refactorings which is why I like using it so we can actually refractory extract variable uses commercial cancellous bone fashion orders and it says do you wanna replace all 34 occurrences of this expression yes I think it really do you think you so color room we're tests and for poor and
for again you does a pretty big improvement the loss so that guy score on quite a bit we've now left the code better if we had to act in feature at least a Kozol with that but we have to act in a
new feature yet because we still have of 45 minutes to go so we can keep going right so now you want to turn this into an each group instead the so they're really feel pretty comfortable with our factory at this point they should be reasonably safe right so the idea is to replace the for loop so actually before I do that is that for loop really equivalent item study each how hard yet to study it to figure that out can have to start a little bit right it's like what's the size minus 1 thing only zero-based indexing that's probably equivalent to an age but you have to certain and so 1 of things I would like to argue is that idiomatic code code written as native is much easier to understand for free readers because you don't have to stare at and for out what is really doing you just know I know that Indian great that so I put a place where you can make the code more idiomatic really improves the code they 1 of things we do with the variable abstraction Garrison duplication but we also is some noise in the code and that also makes it easier to read so which you know because who word Virendra we do we care what speed OK so it's just a way that is actually speed boost as well so so items that leads to and will call that item and so we should be able to get rid of those 2 lines and change nothing else broke sometimes right instance variable thank you test that's what they're there for a day so that's a green bar that's another good refactoring when teaching this to
the community college class last week they got completely addicted to running flog of all the time was worth selection it worse the surprises over I didn't expect that but it's making it worse and we're making better and are what something else we can do this code just to try to get some gets noise other that make a little more understandable so that we can the feature extractor quality prominent so like check method for OK so what would you wanna extractor just this 1 line OK we can do that and I wanna point out this pattern and that parents and that pattern so those that three-line fragment is repeated 3