 All right. Well, my name is Rayne Henricks. I am here to talk to you today about unfactoring from patterns Before we get started, I'd like to give you a brief look at what some people have been saying about unfactoring For instance Jason Sefer you may know from Rails envy Also Jason Sefer from Rails envy and once again Yosef Mendelssohn and Rick Bradley So how many of you are familiar with Sturgeon's Law? If you have you Sturgeon's Law says that 90% of everything is crap There's a corollary to that law. There is the Susser corollary and there is the Henricks corollary to the Susser corollary in other words So my talk today is basically about the fact that you are you all suck at ruby.com Really really I'm not just making that up and this raises an important question How secure are your jobs? When you think about it because Studies show a strong correlation between the maintainability of code and the expendability of its author now I know some of you might be thinking which studies Show that and the answer is shut up No, no some of you may not be good with words either. So we're going to try a little graph It's okay, you don't have to panic just embrace the failure and use it to your advantage by in factoring Now I'm sure some of you are wondering what is unfactoring. It's actually very simple Unfactoring is the process of taking well-designed software and through an iterative series of small changes Making it completely unmaintainable by anyone but you So now how do I go about unfactoring my code? Well, what kind of systems do we need to unfactor the things we're looking for our simple systems Code that communicates well and that is flexible Code that is ultimately maintainability is the is the watchword here. We're trying to avoid maintainable systems Because if your system is maintainable that means anyone can maintain it not just you and so all of that is bad We're all trying we're trying to avoid these things And so what are some of examples of some code smells or code perfumes as I prefer to call them? That you can look for in your code Well, we're looking for things like small methods that do one simple thing We're looking for small classes simple classes that have one goal one purpose That don't mix a lot of different concerns in the same place. We're looking for dry code We're looking for things that have intention of really names all of that is super bad and It's going to make your code far too easy to maintain. So now that I've found some code to unfactor What do I do to unfactor that code to make it less maintainable? First of all, you should be testing all the time And now I know some of you might be thinking if I'm trying to make code less maintainable Why would I write to what stop stop stop thinking that just listen you're writing tests You can unfactor your code into something that no one can maintain, but that you know still works Because it still has to work right if it doesn't work. You're just gonna get fired anyway because then you're incompetent So your code still has to look you can delete the tests when you're done. I don't care Just write test while you're doing this Okay, write test while you're doing this Unfactor your code will go over some examples and then when you're done and your code's unmaintainable and your job is secure You can delete the tests don't commit them because then people can find them So once you have tests that cover the code you're going to unfactor you want to start by making small changes now Unfactoring can be difficult work You're going to be taking something that's easy to understand and you're going to be making it much harder to understand and It can be difficult to keep all of that in your head at the same time So you want to go slowly you don't want to tie yourself out in one big bang So What kind of changes are we going to be making here? Well, one of the easiest ones is to complexify We're going to be using confusing logic and avoiding patterns and idioms the other people can understand It's good to use obscure Ruby tricks like the double bang. Does anyone know what that means? And if you guys know what the double bang trick is excellent. You shouldn't use that Good, okay, you're going to want to dampen your code as well now a lot of you have heard about dry And so that's the first thing to look for you want to look for duplicate code that's been dried up into one place and then Unfactor that back out into a lot of different places But not only do you duplicate the code you should make a lot of small changes each time you duplicate the code You know maybe use different variable names each time just to make it harder to to factor again The last thing you're going to want to do once you have all of this done is to obfuscate your code I prefer pig Latin For this task I find it works very well and So I think now the best thing to do Would be to show some illustrative case studies because you have to have those in a talk like this. I understand so here's an example from Capistrano Now, I hope you can all read that basically it's the deploy task From Capistrano that you would use if you're deploying using a cached strategy and what it does is it updates the the cache of The repository on the server you're deploying to and then it copies that repository cache into the deploy location that makes sense, right? You can pretty much read that and see what it's doing You don't necessarily have to go look at those other methods You just assume they do what it says on the box and the method works So that's what we want to avoid we want to fix that by Complexifying un-factoring taking this nice composed method pattern and breaking those out Into each of the inline code that those methods represent. So that's the first step Now if you probably can't read that and that's it's that's fine It's it's mostly so that you can just get a sense of the scope of the code But there are some things in there that have been derived There's a run command that's been duplicated. So we want to take that out and we want to you know Just obfuscate that code a little bit by just putting it all in line And then you can you can go in there You can change some variable names if you want and then finally we can take that same code and obfuscate it with Pig Latin And when you put all of that together you can go from this pretty factored easy to read and maintain code That's gonna get you fired to something more like this So that as an example, this is the goal This is what we're shooting for here So the more your code looks like this and the less your code looks like this the better you're doing. Let's keep that in mind As another example here, this is a webby task. This is initializing a webby site Does some pretty simple things again. It uses a nice composed Method pattern here the methods have intention revealing names So it's easy to scan this and understand what this method does when it's run and we would be able to un-factor this using the same process to have it look something more like this and So one of the things I wanted to do is how many of you remember the 15 minute blog video the rails 15 minute blog video I Thought I would do a 10 minute Ogblay video Actually a live demo which can be Scary, but we're gonna try it here Try a little bit of this So let me just a second Okay, so what I've done is I've forked rails and I've added some pig Latin to it So I'm gonna go ahead and ales ray Create a Ogblay here Now one of the things I have to do here is I have to copy in the fork real Cuz I didn't really feel like building a gym because that would be too maintainable So I'm just gonna copy in my ales ray code into vendor I'll take a second because my machine is about to die We go All right now We've got the basic rails structure. I believe this is the point where he does something like lists out the Files he has created and goes whoops look we have helpers right that's that's nice. So what we want to do here is Just create, you know a very basic blog so we can start by generating a Controller call it Ogblay now if I remember correctly we have an index here And so then we do this and we start the server So oh here we are at our rails welcome page whoops Then we can look at our Ogblay Whoops And Loha, Erldway so that's how easy it is to get to Loha Erldway with unfactored rails. I Thought I'd get more applause on that one Thanks So so what we want to do now is we want to see how easy it is to Create views because we want to be more MVC about this so let's create in our a view file here And let's say hello from the emplit T Go back and take a look in here. We have hello from the emplit T. So there's rails or Ails ray possibly But that's not really a lot of stuff. We really want to have some posts and The restful way to have posts would to have would be to have an host pays controller, right? So let's go ahead and scaffold an host pay controller Let's see. What are this? What does this need it needs an idle T? And an odd eBay So we generate that and Once that's done. Whoops Rails just writes all of this stuff for us. We've got host pay here. I'm to pay all and You can see if you just go to this new host pay controller Whoops we have to run our migrations So we run our migrations and we can reload the page and now we're listing host pays. Let's create an host pay So you see rails automatically gives you these text fields here because it's it knows how you generated the scaffold It can read the columns in the database because it's a ninja and then you can say Hello, hey Orlando way Create a post. Whoops. We have a post or Oast pay as I like to call it So now one of the things that you'll see if you look at This index file here is That it's it's not it's not as pretty if we go back to and look at how it looks in the HTML It's using a table and we don't like tables. So I think he'd went in and fix some stuff here And fix some stuff here Yeah, we're just some stuff here Put that there Put that there And it looks more like that there You can add another post Hello again So we're listing some posts here. We can see the index Looks pretty good. Let's make that an H2 Because that's what it should be. So now we've got some posts. We've got a basic blog here It's looking good. One of the things that we can do if we were trying to factor our code Would be to make an Oast pay partial here, right? So then it turns that into a render partial collection We've got a little partial here. We can go back and we can look we can see it looks the same, right? So if we wanted to make these in the order that they were posted, you know first at the top one way to do that Would be to just reverse that collection, right? That seems pretty easy, but I don't think that is unmaintainable as I would like it to be So let's do something else here. Let's get rid of these changes now How can we unfactor this? This is back to its unfactored state or moderately unfactored state One of the things we can do is we can change Ruby has a lot of nice iterators All right, this is a very nice iterator that we could use but I don't want to use that So let's use something else It would be Oast pays wouldn't it? Whoops So we can do that a couple times here Should still look right There we go. So now we're back to where we are. It looks the same right now. I'm not writing tests That's okay, but it looks the same. No one can tell we've made any changes But I think this is significantly harder to maintain now, right? Yeah, it's looking better. I think it's looking good here One of the things we can do. So what if we want to reverse this? How are we going to do that? How about this? Now here's one of the great things about this is because if you're not careful When you Unfactor you can create weird things like off by one errors. So we have to add another and no one's going to figure out So now they're reversed and it's everything still works the same. We can add another post You can see that it's going to come to the top of the list here. So we've unfactored our blog now Just think about coming back to this in six months, right? Or think about you're a new developer on the team. You've never seen this project before you take a look at this code or your consultant that is brought in to do an audit Your your response is probably going to be I don't know what this does it and it's terrible But until that point happens and usually we know that code audits very frequently happen outside of the company You work for until that until that happens No one's going to want to touch this code with a 10-foot pole Which means that you will be able to continue working on it for as long as you want and Your job will be secure So I think that is essentially all I have for unfactoring today I'd love to take questions the last time we talked about unfactoring. There were some great suggestions someone suggested a ruby-to-ruby library that would Paralyze your ruby code or that would They would that was aware of ruby keywords and such and could Rewrite your entire library in pig Latin for you things like that Some some wonderful tools for this what's what's the name of it? Can we go with something along those lines maybe I Yes, yes, if you forget to use their weird inheritance tricks to confuse people And instead of using true use bang bang question mark I Think you would you like to help me out here? We can refactor that right now. We got some time You start out with equals zero right so you start with Fred equals zero Wow Fred Now another opportunity for an off-by-one error here, I think that's correct good good Ospeys But we're losing this beautiful code here. We're gonna lose that That's all right. I'll space Fred got it good Now we have to remember do we pre-increment or post-increment this right? I'm sorry. Oh Oh, we do need to keep that don't we I'm sorry And then this will be No, that's better. It's better. Does it still work we had if we had test we'd know Still works any other questions Josh I Because obviously if you do something like on rolling a loop it creates a ton of code it's hard to read but it's also So we're really we're we're working to work. I mean, I think we had one back left I Just show up that would be too easy It's a good idea though any other questions I don't know one more. What do we have here? All right, hold on. They should be rainbow colored Yeah, that's too complex for me, I don't know it's a good plan though anyone else Josh again you're reaching your quota Middle-right Something like that You can have a function you can have a method that returns the name of another method and then you send and then pass that method I Got a couple here Injections for the area Dependency injection to really just flesh out the in factoring I think I another one How do you deal with pesky co-workers that don't buy into in factoring I Ninja unfactoring I Don't know it's not a problem. I've had to deal with I just work by myself. I Like to say as far away from other people's code as I can really I Got one here You talk directly to the database wouldn't that just make it easier to find the code. I mean that's what PHP does and that works out pretty well Helpers and I Locations this is uncharted territory here. We're opening up a brand. I like no I like this. This is good stuff What about sim links to you know mount points on someone else's hard drive. I don't know I'm just you know, I don't know what's possible here Any other questions Test directories so it doesn't really matter I like to know that my tests work so I can continue on factoring with the relative abandon But no one else ever sees my test so they don't they don't know or care No, I didn't elect to do that ales ray elected to do that you can thank ales ray for the inline Yes Evaling strings. Well, there's you know a few different kinds of eval so you can mix them up and confuse people Which which one you're using? We've I've seen some pretty awesome uses of eval. There was a There's a Ruby XML playlist format parser that had Out of its few hundred lines I think a hundred of them used eval and they use eval for things like setting an instance variable It was one of the most awesomely unfactored pieces of code It was fantastic Template back to the model everything can be done the model actually That's true Well, we're not really talking about deployment a because rails doesn't scale and We don't actually want the code to be slower to not run we wanted to run just fine and we just wanted to look like crap Yes So Bash or something else because Ruby is slow hmm. What about shelling out to see that's generated by pearl? How about that? Yes Like No, actually Fred would equal cheese here I believe we can test this look at that Food-related terms Yes, I like that. Yes I Think you should randomly pick one for each line and then change it the next line I like really strange things like camel case with extra underscores randomly thrown into the words Yes I Actually, we do use Hamel, but that's another talk. Yes After this presentation had some yes How about How about if we define no, I don't know let's say Cow to be the square brackets. I think that's good Is that better? Not kept now now. I like how that I saw in the red shirt in the back. I saw one Yes You are all being Surprisingly inventive here. It's actually kind of scary and the far back in the red shirt We need a more complex data structure possibly we can define some sort of class that iterates over objects in a linked list of some kind Rebuke gracefully accepted Yes Probably talk to obi about that one At every point you And secondly, I think we should exploit the use of raising errors Raising errors and then catching them so that you can do control flow. Yeah, that's good. That's good. I like that These are these are all excellent No, that's good you can use throw catch guys, I don't think anyone knows how that works Yes On the sash of some kind yes in the back Factor our code I think if the comment has absolutely nothing to do with the code under the comment How the hell do those things end anyway Anymore something like that Yes, I don't think we have too much more time so maybe just a few more questions Yeah, but then you don't have to do anything That's really just win-win. Yes, I'm really surprised. They're actually using the built-in Ruby methods. Shouldn't we alias everything? For instance, this you know should be I just you know, I only have so much time in a day to one factor I Think one more question Yes I Don't know