 Thank you. I'm talking about our fight against super bad patterns in legacy lives apps I'm Yasuko over I'm living in Tokyo, Japan, and I'm a programmer. I have been using Ruby on Rails since 2006 and before that I have been using Java for seven years and I have an experience and of other languages too and I'm president of every week cooperation in Japan and Please call me over some just over some or an yasco sound or yasco Please do not call me over some Do you know the meaning of over some It's a bad name and for middle-aged woman in Japan and And please do not call me over some It means an old lady in Japanese. So please just call me over some or yasco sound my company is every leaf cooperation and We develop web applications for our clients using mostly Ruby on Rails since 2007 So it's almost 10 years and We are 90 people and 11 women Thank you, so 57.9% are women and 46.7% in engineering So it's a bit worse than Travis CI But I think this percentage is pretty higher than the average of Japanese technology Company I started to the camp I started the company for myself to keep coding Because I love to write code very much But of course I have to do management works these days too. I Put stickers and bookmarks from my company Near the job sport. This is my family and This is a picture of three year old Ceremony in our local shrine in last year when she was two actually They are just flying to Singapore just now and my hobby is Carlta, I suppose and nearly no one knows about Calta This picture is from the movie of Jaya full in Japan, which is kind of popular manga in Japan and Calta is a game that the reader reads Poem and the player takes a read card a red card as fast as one can and We have products for that in both iPhone and Android There's an English name that 100 Japanese poems and here I can demo Like this and this application and acts like the reader So if you are interested in this game, please check it Hello, Singapore. This is the first time for me to visit Singapore and of course attend red dot Ruby conf and this is the first time for me to travel to a hovering country alone and And First time to have an English talk longer than lightning talks Thank you very much. I'm really happy to be here as a speaker and Thank you so much. And this is a big adventure for me caution I Must say a or ma instead of well, so please be placed in your brain And I must say 10 instead of point for numbers sorry about that and I have a lot of slides here. So maybe it might be a busy talk. Sorry Okay, and I like I the outline of my talk is Here I'm talking about two topics The first topic is what we generally have to do to upgrade Ruby and rails in legacy applications And the second is Introduction of four super bad patterns we found Okay, let's start from Ruby and raise migration when I use the word migration it means upgrading upgrading not that and raise migration of active record and I won't talk about upgrading rails from four to five. I think it's not difficult This talk focus on the most difficult part of raise migration, which is from two to four and How many people have used rails to? Thank you. Maybe 30% and How many people have experienced the migration from rails to two three or four or five? Thank you Maybe 20% or this Rails to have been used widely in Japan at least So I'm afraid that many old rails applications remain waiting to be upgraded So you might meet them someday So knowing about how to upgrade Ruby and rails can be some help for that situation Is it possible? Yes Even with no old testing The answer is still yes We did this But is it better to develop an application from scratch? It could be but not always. I think an Existing system being used reflects its real needs It should be respected You can never remove its essential complexity by programming techniques That's why migration and refactoring make sense Let's look at how to upgrade Ruby and rails Let me introduce our case. This talk is about the real experience of me Our applications is kind of health check service B2B and It has been handed over to our company from the other development company which originally developed the system This is our project goal. We have three applications Up one is for administration up two is for end users and up three is Also for end users, but it's for English users and And up one and up two is using rails 2.3.5 and Ruby 1.8 point seven and up three is using rails three and we need to Draw it away from radiant CMS and up and draw up to away from writing CMS because it was and developed as a extension of writing CMS it's not that simple rails application and after that we need to upgrade to rails 4.2.1 and Ruby 2.1 and Marge the ups 3 to up 2 and Then we can do refactoring ancient code and We need to add many new features The total schedule was like this We did this from August in 2014 to May 2015 it took 11 months and There are three companies and working around here. Our company is a black line So the point is we needed more than two months for migration. I drew a map of Right on the map of rails of rails migration Let's start from rails two and we are going to raise three Between them there is a rebound. Yes Okay, then and we are going to raise three point one between them There are the mountains of modern rails and you can easily move to rails three point two and Please say goodbye to Ruby one point eight point seven here and To go to rails four There is a strong parameters force Now you're safe. Maybe you can do upgrade further more This was a real migration schedule of us We upgrade we upgraded two applications at the same time The point is we first upgrade rails from two to three. This is hard Then we upgrade the both ruby to two point one and the rails to three point two After that we migrated to race four After that we started to replace some gems for example for example file uploading page nation and so on We have burden from rails one actually And if you have this kind of definition in your roots are be it is hard to remove it Because we have to write each request specifically To do that Ah, sorry. No, no, no, no Sorry, you need to and know all actions to do that Let's see the road map from rails two to four, but we have no time. So I must hurry up And The river of tears the first point is active record chain number query language So you need to say goodbye to conditions and its friends in rails, too We write like this to search do search and conditions here and its friends is here and Conditions and its friends reside everywhere and If you have some manipulation of conditions hash it makes things worse you need to know all secrets sequels intended Condition manipulation is like this you manipulate the string and parameters separately but now we can write like using where and We have we found a small bad pattern here The prior developer like the pattern to do search first and manipulate the result But it's slow and hard to maintain So we have to replace to chain queries instead of array concat and The second point an obtrusive JavaScript we write ejects code if it uses all rails helper methods and The third point is helper and views you need to add equal and we write all of all the all all the helpers which supporting which is supporting blocks and remove H and Maybe need to fix W escaping sometimes And the fourth point is validation in rails, too, we write validation like this but dates presence of but Sexy validation has come so we can write like this now and the other point You can write your roots. I'll be much simpler and you can use bundler and There's a good point about commands in rails, too if you type rails my app it means rails new my app and and You can type rails see or we will see in after rails 3 but In rails 2 it was different and what happens if you run rails see in rails 2 environment So we don't have to create applications named C or CS anymore. I Used to create C or S applications twice in a day When when I go back from very sleep to very still it was very confusing Okay, go to the next step we are in rails 3 and We have to go over the mountains of modern rails. The first point is asset pipeline Move files and configure settings and we design CSS and just file structures and Good and it's a good timing to use as CSS so long and it takes much time and You need to know all about all asset files Ideally and the second point is default JS libraries changed to be jQuery from prototype JS So you need to rewrite JavaScript code if you are going to switch the library Okay, then you can move to the rails 3.2 It's easy and you can now you can use plug and unique for distinct C code and How about upgrading Ruby and I think it's quite easy You just need to care about string encoding and hash things Okay, almost down We are in rails 3.2 Then the strong parameters falls Security check went to controller from model So you need to remove a drug protected or a drug accessible from your models and add parameters at parameters check in your controllers I Love string as strong sorry string. No, no, no strong parameters Because it's very flexible and readable but Adding in the parameter checks and adding strong and supporting the strong parameters is Really hard and boring job You need to know all parameters for all actions and others and maybe you want to use Hamo and Replace and of course you need to replace many gems and rewrite monkey patching in config risers and Maybe you need to care about time zone Gems You need to replace gems through the rails migration generally I think in modern rails such as rails 4 or 5. This is the main job of migration It's kind of getting much easier Congratulations welcome to rails 4 or 5 So how to upgrade rails from to conclusion it's not only rake rails update You need to change so many places For that you need to know You need to know well about your app You need to know about all actions to remove default ruling routine and all parameters of each actions for applying the strong parameters strictly and Like and you know Sorry, you need to know about right looks of all pages to fix helpers and escaping and Problems and you need to know all secrets to replace conditions and the chain above query and You need to know all asset files for using assets pipeline effectively And you need to know all monkey patching making for making them work in modern rails So it's really tough, but not impossible step by step By the way, why didn't we have automated and why didn't we have automated tests? Because we had abandoned tests given from the prior deep company so According to the yesterday's spoke We have improved our tests, right? Those tests did not make sense at that time What two tests were really wrong and or extremely difficult to change I Know this is dangerous. However, having bad tests could be even worse than having no tests The distinct company helped us a lot fortunately And we had tried to write feature specs, but the old ruby version is courage less We wanted to use capybara and some for example This is capybara So we decided to write many good specs after migration After a year we have many nice specs happy Okay, what minutes I have All right Thank you and Let's move to a super bad pattern in there's our apps had been developed by beginners of rails coming from Java and We found many bad patterns there We love good patterns So we have easy access to the good patterns How about bad patterns if your team is modern and nice? It's hard to find natural bad patterns if you're in your daily work It means you might miss some chance to notice what to avoid in programming That's I want to share these bad patterns here In my opinion and natural bad patterns are like gold mines In other words, I'm going to teach you to fish in cloudy swamps Let me introduce four major bad patterns in the project one too many layers two dreamy method three global hash four mass housing in one controller One too many layers We have been fighting against too many classes and layers I suppose it's familiar style in Java and I like Java It was like this we have many seen active record models and We have spaghetti but non active record classes and These non active active record classes uses active record models very procedure and of course controller is using these classes very procedure and typical overmatch was one to one relation models Like company company address company something so on These models are in relation of one to one so maybe There is no required to separate them and And the other overmatch is facade proxy so on Wrong reasons to separate models, you know, which I I guess is here They have slightly different meanings the usage is different sometimes and The worst one is I would like to avoid changing existing classes for my safety. Oh my god separating models costs very much and facade proxy so on It's no problem if it's well designed of course But in our case these seem to be the result of that result that developers could not use active record properly They they look to not to trust active record Okay, we did refactoring for this first we unify the model and remove the medium layer classes and add features to a simple and one single class Make it rich and then modularized active record model and use a model directly in Most in most cases from controller, okay to dreamy method Dreamy method is ready to do everything Here is that the dreamy method do everything for you method It takes that I and I didn't fire and then it provides everything Of course, you know the right pattern You can write a simple method separated methods Or you can use inheritance sometimes Like this But are you sure why this dreamy method pattern sucks? It's hard to read you can You can recognize that okay, you mean doing a constant two job here So it's not really bad, but how about this? This is The variable so oh, I need to check what value is inside and This pattern is very easy to start you have a method without argument But sometimes sometimes go Sorry, but You need to change this behavior in my case. I'm sorry. I need I need to change this behavior in my case so Well, let's add an argument here and add if This is very and it is not really a case. I've seen this pattern many times in review code and code reviews And it grows like villas Then here and using do everything for you and I need to add a new logic after this So let's add a new method having the same identifier Okay, you have now the second method it takes and I didn't fire So like this and I did fire travels over methods classes and layers everywhere like this it's nightmare and Another little but but here in I'm like hash constant, but it's like this But it should be like this or you can use some in a pattern Why I think this is bad because situation who and it's not not exist Sorry situation who does not cause an error So you can't notice something is wrong Ali and you need to remember or check what key is available ID can do nothing for it and If values are integer you need to remember both name and value and It's difficult to search you can use variable as a key and hash contents can be changed too easily and We saw the combination Dreaming method and this hash in a constant Here so wandering around looking for something is very painful So separate things and give them good names 3 global hash Global hash hash is loved. I think everyone like hash and The hash feels like not scary. You don't have to create a new class and it's flexible But it can be a monster It starts like this Okay, you are in active users controller and the search action and you receive the parameters and It's hash Sorry, it's hash and you You use that hash to search search or you store a session Store that in session, but it grows like this in In you you are in another controller now and you add such similar to the prime prior one and This time you receive the search option, but it's including extra options and You want to make a search and all this is for new users not active users and And you add third controller and including extra options and much simpler and Such a node is for special users So in decision or such or such you face a global hash Well, you are great data. Anyway, and everyone can use it and look some of them check one key Add something for my friend and retrieve it Just store all of them in case So nobody knows what contents are expected for each purpose exactly chaos We had a long battle to defeat them Will replace the part of hash to be a object by each features Like this So the conclusion is hash is scary It's too flexible to be used widely for human beings and for mass housing in one controller That it and the problem was like this There are three features, but only one controller Action provides three features a Controller code was like this So many when but actually it was worse than this because it was using hash and enum like hash constant and The better design should be like this Have separated controller for each features and You may notice but that this is a combination of two bad patterns so dreamy method and global hash and With mass housing in one controller pattern, it is extremely difficult to change just one feature You need to chase the identifier not to harm other features Okay, then what to do to avoid these bad patterns Too many layers use active record if you can Doubt one to one relation Think about maintenance costs Dreamy method write a different method for a different me doubt adding an argument and if Extract common parts if you need and use polymorphism and Step this stop this pattern in our stage. This is very important and global hash Feel free to add a new class use hashes locally Conclusion I showed what you need to do to upgrade rails from two to four in general good luck and Then ask me if you need and I Introduced the natural bad patterns in rails we found let's avoid the traps Thank you Thank you So so tender was telling me yesterday actually that he he wanted to learn how to play mahjong and car Yeah, so maybe you can show us how to use your app later. Yeah, so any questions from the audience? Oh, you're all gotten over the tears and the mountain Yeah, we have one question Can you please tell me how a large was in line of course this application that you are great I Don't remember the lines, but it was quite so 30 so I think 30 controllers or For 40 models or and then that's the for one application so much more Yeah, I have to go for her code base or something. Yeah, that's quite a lot Anymore questions Hello, do you think it's worth it to upgrade from two to four for rails because I saw that you take about Ten ten months or ten months. Oh, yeah to upgrade So do you think it's worth it or do you have any reason to do that? Actually, we we are sorry The question is why we need 11 months I think no No, no, no, no, but I'm weird. Mmm. Yeah All right, let's Sorry All right the question is about schedules and In the migration only a month only I'm not sure but it took just more and then listen three months and we Yeah, refactoring was very hard But there are many features already. So if we develop this in scratch We can't do this in this time To the same pain as you I wonder if you put in production each time you Reach a step or you did everything then you start pushing to the production We we we changed whole things and and then release that because We need for the testing companies and work. So It's easier for us. Okay. I think That wraps up. Yes, we go stop. Thank you very much