Summarized using AI

Our Fight Against Super Bad Patterns in Legacy Rails Apps

Yasuko Ohba • June 24, 2016 • Singapore • Talk

In the video titled "Our Fight Against Super Bad Patterns in Legacy Rails Apps," Yasuko Ohba, the President of Everyleaf Corporation, shares her team's journey in upgrading various Ruby on Rails applications. The presentation details their experience migrating applications from Rails 2.3.5 to 4.2.1 and Ruby 1.8 to 2.1, emphasizing the challenges and strategies involved in such a significant upgrade. Ohba discusses two primary topics during the talk: the general process of upgrading Ruby on Rails applications and identifying four 'super bad' coding patterns they encountered during the migration.

Key Points Covered:

- Migration Overview:

- Ohba introduces the concept of migration as upgrading rather than just transferring data.

- She notes the widespread use of Rails 2 in Japan and the importance of understanding the upgrade process due to the number of legacy applications still using older versions.
- Upgrade Process:

- The roadmap for upgrading was outlined, which included migrating from Rails 2 to 3, then to 3.2, and finally to Rails 4, highlighting a structured approach to tackling the migration step by step.

- The timeline for the project spanned 11 months and involved multiple phases, with specific focus on the complexities involved in removing deprecated elements and refactoring code during the upgrade.
- Common Challenges and Solutions:

- Ohba illustrates the challenges faced due to the absence of automated tests from the previous developers, which complicated the upgrade.

- Real-life examples highlight how the refactoring involved unifying models, adding strong parameters, and adapting to the new asset pipeline.
- Bad Patterns Encountered:

- Ohba identifies four 'super bad' coding patterns in Rails applications that include:
- Too many layers in code leading to complex systems.
- The use of global hashes in controllers causing chaotic code management.
- Overly complex methods that attempt to manage multiple tasks at once.
- Mass housing of features in single controller actions, complicating future changes and maintenance for developers.

Conclusions and Takeaways:

- Ohba emphasizes the importance of good coding practices and the need to address bad patterns carefully to ensure maintainable and efficient application development.

- The migration experience served as a vital lesson in understanding application structure and the necessary steps to improve legacy Rails applications, demonstrating that while migrations are challenging, they are achievable with the right strategies and focus on refactoring.

Our Fight Against Super Bad Patterns in Legacy Rails Apps
Yasuko Ohba • June 24, 2016 • Singapore • Talk

Speaker: Yasuko Ohba, President, Everyleaf Corporation

In 2014-2015 my team migrated a series of applications from Rails 2.3.5 to 4.2.1, and Ruby 1.8 to 2.1. Our goal was to add large features, while substantially refactoring the whole system. It was a big challenge!This talk will cover the timeline of the project and talk about all the things a team performing a large migration of Ruby and Rails will need to do. Secondly, you will learn about some "super bad" Rails code patterns we encountered and how we refactored them. Real life experience distilled!

Speaker's Bio
A Ruby / Rails programmer in Tokyo, Japan. President of Everyleaf Corporation, which provides software development service mostly with Rails for clients since 2007. I have written 2 books on Ruby in Japan. A mother of a 3 year old girl.

Event Page: http://www.reddotrubyconf.com

Produced by Engineers.SG

Help us caption & translate this video!

http://amara.org/v/ONqM/

Red Dot Ruby Conference 2016

00:00:18.050 thank you and I'm talking about our fight against super bad patterns in
00:00:24.240 legacy lives apps I'm yes coba i'm
00:00:31.610 living in tokyo japan and i'm a programmer i have been using Ruby on
00:00:39.030 Rails since 2006 and before that i have
00:00:44.670 been using java for seven years and i have an experience and of other other
00:00:50.789 languages too and i'm president of every week cooperation in japan and please
00:01:00.629 call me Oh Bassam just Oh Bassam or enesco song or ask oh please do not call
00:01:08.159 me oppa some dishes do you know the meaning of Awesome it's a bad name and
00:01:16.290 for a middle-aged woman in Japan and please do not call me Oh Bassam it means
00:01:26.400 an old lady in Japanese so please just call me obasan or yes cuz I'm
00:01:34.440 my company is a belief cooperation and
00:01:39.500 we develop web applications for our clients using mostly Ruby on Rails since
00:01:46.550 2007 so it's almost 10 years and we are
00:01:52.140 19 people and 11 women oh thank you so
00:02:00.080 5050 seven point nine percent are women and forty six point seven percent in
00:02:06.920 engineering so it's a bit weston and travis CI but i think this percentage is
00:02:14.130 pretty higher than the average of Japanese technology company I started to
00:02:24.360 the cap I studied the company for myself to keep coding because I love to write
00:02:30.720 code very much but of course I have to do management works these days too I put
00:02:42.209 stickers and Big Macs from my company and near the job sport
00:02:49.920 this is my family and this is a picture of three year old ceremony in a local
00:02:56.680 shrine in last year when she was to actually they are just flying to
00:03:02.350 Singapore just now and my hobby is
00:03:07.560 Naruto I suppose another nearly no one knows about kulta this picture is from
00:03:16.330 the movie of chihayafuru in Japan which is kind of popular manga in Japan and
00:03:22.680 kata is a game that the reader with a poem and the player takes the raid card
00:03:30.640 a red card as fast as one can and we have products for that in both iPhone
00:03:38.860 and Android then and there is an English name the 100 Japanese poems and here I
00:03:46.480 can demo like this and this application
00:03:56.739 and acts likes the reader so if you were interested in this game please check it
00:04:05.580 hello Singapore this is the first time for me to visit Singapore and of course
00:04:12.010 attend red rubyconf and this is the first time for me to travel to a hoe
00:04:19.150 pouring country alone and first time to have an English talk longer than
00:04:25.540 lightning talks so thank you very much
00:04:33.860 I'm really happy to be here as a speaker and thank you so much and this is a big
00:04:39.960 adventure for me caution I must say a or
00:04:46.770 ma instead of bail so please d placed in your brain and I must say can instead of
00:04:55.650 point four numbers sorry about that and I have a lot of slides here so maybe it
00:05:03.569 might be a busy talk sorry okay and the
00:05:09.900 Allied I the outline of my talk is here I'm talking about two topics the first
00:05:18.569 topic is what we generally have to do to upgrade Ruby andreas indego see
00:05:24.599 applications and the second is introduction of for super bad patterns
00:05:30.810 refund okay let's start from Ruby and grace
00:05:38.270 migration when I use the word migration it means upgrading upgrading not the
00:05:45.259 unrest migration of active record and I
00:05:54.349 won't talk about upgrading rails from four to five I think it's not difficult
00:05:59.800 this talk focus on the most difficult part of res migration which is from two
00:06:06.289 to four and how many people have used
00:06:12.319 ways to thank you maybe thirty percent and how many people have experienced the
00:06:20.270 migration from rails two to three or four or five thank you maybe twenty
00:06:27.259 percent all this rose to have been used
00:06:33.139 widely in Japan at least so I'm afraid that many old rails applications remain
00:06:40.759 waiting to be upgraded so you might meet them someday so knowing about how to
00:06:50.840 upgrade Ruby and rails can be some help for that situation is it possible yes
00:07:03.660 even with no old testing the answer is still yes we did this but is it better
00:07:14.020 to develop an application from scratch it could be but not always
00:07:24.440 I think and the existing system being used reflects its real needs it should
00:07:33.260 be respected you can never remove it's
00:07:38.600 essential complexity by programming techniques that's why migration and
00:07:46.520 refactoring makes sense let's look up how to a prayer Ruby on Rails let me
00:07:55.730 introduce our case this talk is about the real experience of me our
00:08:01.280 applications is kind of health check service b2b and it had been handed over
00:08:07.940 to our company from the other development company which originally developed the system and this is our
00:08:16.040 project goal we have three applications up one is for administration up to is
00:08:22.310 for end-users and apps three is emily also for end uses but it's for english
00:08:29.990 uses and up one and up to is using rails two point three point five and ruby 1.8
00:08:37.610 point seven and up three is using rails 3 and we need to draw it away from
00:08:45.500 radiant CMS and apt and draw up to away from writing CMS because it was
00:08:52.280 developed as a extension of radiant CMS it's not that simple rails application
00:08:59.330 and the end after that we need to upgrade to raise four point two point
00:09:07.220 one and Ruby 2 point 1 and master apps at 32 up to and then we can do
00:09:17.660 refactoring an ancient code and we need to add many new features the total
00:09:26.660 schedule was like this we did this from August in 2014 to may 2015
00:09:35.459 tooks 11 months and there are three companies and working around here our
00:09:42.149 company is the black line so the point is we need it more than two months for
00:09:48.420 migration I drew a map of right on a map
00:09:53.790 of rays of raves migration let's start
00:09:59.579 from rails to and we are going to raise three between them there is a river of
00:10:06.660 tears okay then and we are going to
00:10:16.829 raise 3.1 between them there are the mountains of modern rails and you can
00:10:24.959 easily move two rails 3.2 and please say goodbye to ruby 1.8 point seven here and
00:10:34.009 to go to rails for there is a strong parameters force
00:10:44.290 and now you're saying maybe you can do
00:10:49.329 the upgrade pass or more this was a real
00:10:54.910 migration schedule of us we upgrade we upgraded to applications at the same
00:11:02.050 time the point is we first upgrade rails from two to three this is hard then we
00:11:10.600 upgrade the boss will be to 2.1 and rails to 3.2 after that we migrated to
00:11:18.910 raise for after that we started to replace some gems for exam for example
00:11:24.459 file uploading pagination and so on we have burden from riles one actually then
00:11:32.560 if you have this kind of definition air in your roots are B it is hard to remove it because we have to write each request
00:11:40.509 specifically to do that ah sorry no no
00:11:46.480 no no tow each
00:11:55.030 sorry and ah
00:12:01.570 fine you need to and no all actions to do that let's see the road map from
00:12:09.490 rails do 24 but we have no time so I must hurry up the river of tears the
00:12:18.790 first point is active record chainable query language so you need to say
00:12:24.790 goodbye to conditions and its friends embrace till we write like this to such
00:12:31.210 do such and conditions here and its friends is here and conditions and its
00:12:39.370 friends beside everywhere and if you had some manipulation of conditions hash it
00:12:45.880 makes things worse you need to know all seekers sequence intended condition
00:12:53.140 manipulation is like this you manipulate the string and the parameters separated
00:13:00.310 Lee but now we can write like using where and and we have found a small pad
00:13:10.510 pattern here the prior developer like the pattern to do such first and
00:13:17.100 manipulate the result but it's slow and hard to maintain so we have to replace
00:13:24.790 to chain queries instead of awaken cut and the second point unobtrusive
00:13:32.740 JavaScript we right edge Xcode if it uses out waves helper methods and the
00:13:38.550 third point is helper and views you need to add equal and rewrite all of all the
00:13:45.160 old helpers with supporting which is supporting blocks and remove age and
00:13:52.680 maybe need to fix debris escaping sometimes and the first point is
00:13:59.410 validation in browse to we write validation like this but the its
00:14:05.680 presence all but 60 validation had come so we can write like this now
00:14:12.460 and the other point you can write your
00:14:18.130 roots I'll be much simpler and you can use bandula and there's a good point
00:14:25.810 about commands in Royals do if you type
00:14:30.850 raise my up it means rails new my app and you can type rel see or we will see
00:14:39.910 in utter Braves three but we're in verse 2 it was different and what happens if
00:14:47.470 you run rails see in rails to environment so we don't have to create
00:14:56.440 applications name c or CS anymore i used
00:15:01.690 to create see yours s applications twice in a day well when i go back from where
00:15:08.500 three two wives do it was very confusing
00:15:13.590 okay go to the next step we are in rails 3 and we have to go over the mountains
00:15:22.360 of modern read the first point is asset pipeline move files and configure
00:15:28.720 settings and we design CSS and JS file structures and good and it's a good
00:15:35.230 timing to use a CSS so on and it takes much time and you need to know all about
00:15:41.830 all acid files ideally and the second
00:15:47.020 point is default J's library is changed to be jquery from prototype Jes so you
00:15:53.530 need to revise JavaScript code if you are going to switch the library okay
00:16:00.340 then you can move to the rails 3.2 it's easy and you get now you can use black
00:16:07.870 and unique four distinct sequel and how about upgrading Ruby and I think
00:16:17.109 it's quite easy you just need to care about string encoding and hash things
00:16:25.439 okay almost down we are in rails 3.2
00:16:32.100 down the string parameters polls and security check went to controller from
00:16:39.160 model so you need to remove a drug protected or at work accessible from
00:16:44.230 your models and add parameters add parameters check in your controllers I
00:16:50.819 love string as strong holy string domino's strong parameters because it's
00:16:59.199 very flexible and readable but adding
00:17:04.829 per meter checks and adding strong and supporting strong parameters is really
00:17:11.380 hard and boring job you need to know all parameters for all actions and others
00:17:20.189 maybe you want to use whammo and repress and of course you need to replace many
00:17:27.549 gems and rewrite monkey patching in conventional Isis and maybe you need to
00:17:33.820 care about time zones James you need to
00:17:38.919 repress gems through the rails migration generally I think in modern rails such
00:17:45.370 as res 4 or 5 this is the main job of migration it's and getting much easier
00:17:53.940 congratulations welcome to raise four or five so how to upgrade welcome to
00:18:01.419 conclusion it's not only way graves update you need to change so many places
00:18:11.399 for that you need to know you need to know well about your app and you need to
00:18:21.879 know about all actions to remove default routing routing and all parameters of
00:18:27.789 each actions for applying strong perimeter strictly and like em you know
00:18:35.409 yes sorry you need to know about write books of all pages to fix helpers on
00:18:41.860 escaping and problems and you need to know all sequels to repress conditions
00:18:48.010 and chainable query and you need to know all acid files for using assets by
00:18:54.429 prying effectively and you need to know all monkey patching making for making
00:19:00.639 them work in modern rails so it's really
00:19:06.760 tough but not impossible step by step by
00:19:12.100 the way why didn't we have automated and why didn't we have automated tests
00:19:19.350 because we had abandoned test given from the prior deep company
00:19:27.190 so according to the yesterday's talk we
00:19:32.360 have improved our tests right and those
00:19:38.930 tests did not make sense at that time what do tests were really wrong and or
00:19:45.770 extremely difficult to change I know this is dangerous however having bad
00:19:51.980 tests could be even worse than having noticed the distinct company helped us a
00:19:58.730 lot fortunately and we had tried to write pigeon specs but the old ruby
00:20:06.170 version discourage this we wanted to use capybara and some for example this is
00:20:13.910 capybara so we decided to write many good specs after migration after a year
00:20:21.860 we have many nice pecs happy okay what
00:20:27.470 minutes I have all right thank you and
00:20:34.840 let's move to a super bad pattern in Dale's our apps have been developed by
00:20:44.090 beginners of rails coming from Java and we found many bad patterns there we love
00:20:54.710 good patterns so we have easy access to the good patterns how about bad patterns
00:21:05.450 if your team is modern and nice it's hard to find natural bad patterns if
00:21:11.990 you're in your daily work it means you might miss some chance to notice what to
00:21:19.190 avoid in programming that I want to share these bad patterns here in my
00:21:27.620 opinion and natural bad patterns are like gold mines in other words i'm going
00:21:34.490 to teach you to fish in cloudy swamps
00:21:40.240 let me introduce for measurable patterns in the project well too many layers to
00:21:50.110 dreamy method 3 global hush or mass
00:21:55.340 housing in one controller one too many layers we have been fighting against too
00:22:04.279 many classes and layers I suppose it's familiar style in Java and I like Java
00:22:12.909 it was like this we have many seen active record models and we have
00:22:22.549 spaghetti but non active record classes and these non active active record
00:22:32.450 classes uses active record models very procedure and of course controller is
00:22:39.380 using this class is very procedure and typical over match was one-to-one
00:22:48.190 relation models like company company address company something so these
00:22:56.650 models are when an iteration of one to one so maybe there is no required to
00:23:05.350 separate them and the other over match is facade proxies on own wrong reasons
00:23:16.300 two separate models which I I guess is here they have slightly different
00:23:23.620 meanings the usage is different sometimes and the worst one is I would
00:23:30.280 like to avoid changing existing classes from my safety oh my god separating
00:23:40.180 models cost very much and pasa proxy so
00:23:46.750 on and it's no problem if it's well designed of course but in our case this
00:23:54.340 seemed to be the result of that result that developers could not use active
00:23:59.470 record properly they may look to not to
00:24:04.900 trust active record okay we did
00:24:10.000 refactoring for this first we unify the model and remove the meat medium the
00:24:18.930 classes and add features to a simple and
00:24:24.190 one single class make it rich and motorized activerecord model and use the
00:24:31.870 model directory in most clock in in most cases from controller
00:24:39.570 okay to dream a method dreamy method is
00:24:45.460 ready to do everything here is that the
00:24:51.070 dreamy Messer do everything for you method it takes that I an identifier and
00:24:57.399 then it provides everything of course
00:25:03.250 you know the right pattern you can write a simple method separated methods or you
00:25:13.090 can use inheritance sometimes like this but are you sure why this dreamy missile
00:25:22.090 pattern sucks it's hard to read you can
00:25:29.380 and you can recognize that okay you mean doing constant to job here so it's not
00:25:37.809 really bad but how about this then this
00:25:44.320 is an the variable so oh I need to check what value is inside and this pattern is
00:25:54.309 very easy to start you have a method without argument but sometime sometime
00:26:02.860 oh sorry but you need to change this
00:26:09.279 behavior in my case I'm sorry I need I need to change this behavior in my case
00:26:14.889 so well hey let's add an argument here
00:26:21.130 and add if this is very it it not very
00:26:26.649 clear case I've seen this button many times in rabia code and code reviews and
00:26:32.220 it grows like villas mean this is a go here then using do everything for you
00:26:39.760 and I need to add a new logic after this so let's add a new method having the
00:26:47.770 same identifier okay you have now the second method it takes an identifier so like this and I
00:26:58.770 did fire travels over methods classes and layers everywhere like this snide
00:27:05.490 mare and and that's a little but but
00:27:11.310 pattern here in am like hash constant but it's like this but it should be like
00:27:18.750 this or you can use some in important why I think this is bad because
00:27:27.020 situation who and it's not not exist that in sorry situation food does not
00:27:36.560 cause an error so you can't notice something is wrong re and you need to
00:27:44.970 remember or check what key is available ID can do nothing for it and if values
00:27:52.110 are integer you need to remember both name and value and it's difficult to
00:27:58.080 such you can use variable as a key and hash contents can be changed to easily
00:28:04.770 and with the combination of dreamy
00:28:10.680 method and this hash me nem constant here so wandering around looking for
00:28:18.960 something is very painful so separate things and give them good names three
00:28:27.900 grow bahash global hush-hush is loved I think everyone like hash and hash feels
00:28:37.950 like not scary you don't have to create a new class and it's flexible but it can
00:28:45.210 be monster it starts like this okay you are inactive users controller and such
00:28:52.890 action and you receive the parameters in
00:28:57.979 it's a hash mm sorry it's hash and you
00:29:03.859 you use that hash to such search or you
00:29:09.779 store a session store that incision but it grows like this in you you are eyeing
00:29:18.479 another controller now and you add such similar to the Prime ma player 1 and
00:29:26.719 this time you receive the search option but it's including an extra options and
00:29:34.159 you want to make a certain all this is for new users not active users and and
00:29:42.690 you add a third controller and including
00:29:48.509 ex-royal options and much simpler and with such a no this is for special uses
00:29:55.429 so indecision or such as such you face a global hush well you were great data
00:30:02.129 anyway and everyone can use it and look some of them a check one key add
00:30:09.929 something for my friend we retrieve it just store all of them in case so nobody
00:30:17.580 knows what contents are expected for each purpose exactly chaos we had a non
00:30:25.619 battle to defeat them will depress the part of hash to be a object by each
00:30:32.479 features like this so the conclusion is huh sure is scary
00:30:40.640 it's too flexible to be used widely for human beings and for mass housing in one
00:30:51.450 controller that it and that a problem was like this there are three features
00:30:58.470 but only one controller action provides
00:31:03.900 three features a controller code was
00:31:09.030 like this so many when but actually it was worse than this because it was using
00:31:16.590 hash and enum like hash constant and the better design should be like this have
00:31:26.720 separated controller for each features and you may notice but that this is the
00:31:35.160 combination of two bad patterns so dreamy method and global hush and with
00:31:43.590 mass housing in one controller bottom it is extremely difficult to change just
00:31:48.809 one feature you need to chase the identifier not to harm other features
00:31:57.440 okay then what to do to avoid this bad patterns too many layers use active
00:32:05.460 record if you can doubt one-to-one relation think about maintenance costs
00:32:14.180 dreamy method right a different method for a different in doubt add in an
00:32:22.440 argument and if extract common parts if you need and use polymorphism and step
00:32:30.810 this stop thief pattern in re stage this is very important and global hush feel
00:32:39.900 free to add the new class use hash is vocally conclusion I showed what you
00:32:50.730 need to do to upgrade rails from two to four in general good luck and then ask
00:33:00.660 me if you need and I introduce the natural but patterns in routes we pound
00:33:07.230 let's avoid the traps thank you if you thank you
00:33:20.450 hi is important so so tangela was telling me yesterday I he that he he
00:33:27.539 wasn't learn how to play mahjong and character yeah so maybe you can show us how to use your hair later yeah any
00:33:34.830 questions from the audience
00:33:40.220 are you all gotten over the years and the mountain one question can you please
00:33:54.700 tell me how a lodge was in one of God's this application that you are great
00:34:03.110 canon coming I don't remember the lines but it was
00:34:10.510 quite big so 30 so I think 30 controllers or 440
00:34:26.379 models or another that for one application so much more could be
00:34:41.169 something yeah that's quite a lot any more questions okay oh hello do you
00:34:49.809 think is worth it to upgrade from 244 rails because I saw that you take about
00:34:58.920 10 10 months or 10 months are you are to upgrade so do you think it's worth it or
00:35:05.349 do you have any reason to do that thanks actually when we are sorry the question
00:35:16.809 is why we need the 11 months I think no
00:35:29.319 oh no no no but I'm weird hmm yeah all
00:35:36.849 right let's slip the nursery
00:35:49.460 get you this
00:35:59.720 all right and the question is about schedules and the migration only among
00:36:07.470 only I'm not sure but it takes just more and then less than three months and we
00:36:15.650 yeah refactoring was very hard but there
00:36:21.090 are many pitches already so if we develop this in scratch we can't do this
00:36:29.280 in this time
00:36:35.830 yeah that answers to bashing one well I
00:36:40.980 went to the same pain as you I wonder if you put in production each time you
00:36:49.170 reach a step or you did everything that you stop
00:36:55.780 introduction
00:37:04.560 we we we changed whole things and and then release that because we need for
00:37:13.240 the testing companies and like so it's
00:37:18.430 easier for us okay i think that rep I have to go stop
00:37:27.559 thank you very much and much
Explore all talks recorded at Red Dot Ruby Conference 2016
+17