Rails Pacific 2014

Panel: Refactoring

Rails Pacific 2014

00:00:10.400 are you excited yeah thanks thanks for uh giving up sorry today is
00:00:17.440 the first day of the conference so uh i am uh first i serve
00:00:23.039 uh a little quick induction i'm excited i'm the founder of uh rails thailand also the organizer of
00:00:30.080 rails pacific so our opening will be more formal today
00:00:35.840 because today the reason we set up panel and the workshop before the
00:00:42.079 before the section is we saw the less should be more interesting event during the conference
00:00:48.160 uh if uh when i used to go to other countries conference i
00:00:54.879 usually find out the people are not just like like not state of all uh speakers
00:01:00.640 what they prepare they want to more to hear more real life experience from the speakers
00:01:06.960 so that's why we have the panel discussion on this morning and the following will be the workshop hopefully
00:01:14.240 everyone can get get something from the workshop so
00:01:19.439 okay uh today's our today's our panel discussions topic will be
00:01:25.200 the first one will be refactoring and the the second one will be how to become a
00:01:30.640 senior developer i encourage everyone can like uh
00:01:36.159 to ask more about uh more questions because they are very seasoned developer in the lab and they
00:01:43.840 they are so they are say oh so
00:01:48.960 our how do i say yeah they are all stars anyway i will head to next the host the
00:01:54.799 mark they will do the panels for everyone thank you
00:02:01.439 morning everyone um welcome to the first panel today's panel will be about
00:02:06.840 refactoring and my name is mark our bill host and today we are very happy to have mr
00:02:14.239 shibata hiloshi-sang and oh sorry mr akila matsudasang
00:02:21.200 and we have ryan mr ryan big and mr nick sutter
00:02:28.560 yeah so can you in like one or two sentences briefly
00:02:33.680 introduce yourself we start with akirasang alright
00:02:38.879 so good morning everyone i'm from japan i'm akira
00:02:44.720 probably i'm not on the program yet
00:02:50.319 um so i'm a ruby on rails uh committer and a ruby commander also i
00:02:57.680 guess known as a author of ruby gem called kaminari for
00:03:15.599 so i'm gonna do the opening talk tomorrow i guess and i'm gonna talk about action view
00:03:21.680 so see you there tomorrow again thank you thanks hiroshi
00:03:28.080 hello good morning so i my name is hiroshi shivata so
00:03:33.120 i worked a gmo paperball my job title is a chief engineer so
00:03:40.159 i'm ruby shirley committer so i work to
00:03:46.159 uh are refactoring to a river code in ruby core
00:03:52.000 so my job is uh
00:03:57.920 crafting very gorgeous so our company uses our rails entirely
00:04:03.519 in my company so our web service is over 20.
00:04:08.799 so please see you tomorrow thank you thanks and um
00:04:14.400 ryan sure um i work for a company called lifx we do some fancy lights you can control
00:04:20.160 with your phone i'll be giving out some tomorrow but right now i've got some shirts i've also written a book called
00:04:25.840 rails three in action and i've just finished my other book multi-tenancy with rails which i'll be giving a talk on tomorrow i hope you all come and stay
00:04:33.199 for that because i'm giving the last talk for the conference thanks thanks nick
00:04:39.360 hello everyone i'm nick i'm from germany i live in australia but not in the same city with this
00:04:45.120 beautiful handsome guy next to me i work
00:04:50.479 with ruby for about like 10 years i've done several gems i do some rails
00:04:57.040 contributions from time to time if i feel like it and i'm talking about
00:05:02.080 i'm basically talking about my gems in my talk tomorrow so it's a it's a new approach a new architectural style for
00:05:07.680 rails i hope you guys are going to be there and listen and
00:05:12.800 this is a refactoring panel so what i want to say upfront is i've been doing
00:05:18.080 refactoring the last five or six years i haven't worked on any production code only refactoring
00:05:25.600 yes cool cool thanks um we'll take the
00:05:31.120 audience questions in the last 10 minutes so let's get started and please don't
00:05:36.240 fight and share a mic you know so let's start
00:05:42.320 first what keywords flashes in your mind when you think about refactoring
00:05:50.880 well what comes to mind for me is that if you want to refactor code it has to be messy in the first place you can't
00:05:55.919 refactor something that's nice and clean and the whole test test driven development flow is red
00:06:01.440 green refractor or schnimz99 we're talking about in the car on the way over red red red green refactor
00:06:08.000 so the whole purpose of test driven development is to refactor at the end of it um so that's what comes out
00:06:14.000 test-driven development is dead
00:06:19.680 it's alive as a zombie and i clear some too
00:06:27.520 what comes to my mind something else than tdd
00:06:33.759 no please no please no tdd debate okay
00:06:38.880 i think we all agree that uh tests and refactoring goes hand in hand right i mean there's you cannot refactor stuff
00:06:45.120 without knowing if you whether you break stuff or not so let's skip that part and
00:06:51.520 so maybe um right
00:06:56.639 so i used to be a java and dotnet programmer before coming to rails and
00:07:03.840 you know reactor was much more easy using the ides yeah
00:07:09.919 because the language supports the static typing and we had wonderful ideas like
00:07:15.840 eclipse or some something so um in comparison to that ruby is more
00:07:22.000 like reflecting unfriendly language
00:07:27.440 but um do you think that um because what i've seen in eclipse and stuff the refactoring
00:07:33.039 logic helps you basically helps you extracting methods and helps you extracting
00:07:39.120 it doesn't really help you extracting classes so because i think refactoring is more about um
00:07:44.560 introducing more classes or changing classes whereas um the ide is only support like move this method into um
00:07:50.560 into that class so i don't know um and so
00:07:56.000 in um so there's no diff for me there's no there's no difference in ruby or java if i if i refactor i mean i agree that
00:08:03.039 typing helps you refactoring because you have you have a strong interface and ruby your interface is always a bit
00:08:09.199 blurry because you never know if it's an array or is it an active record collection you're passing in and out so
00:08:14.879 okay but um so i've never found this um eclipse stuff really helpful for
00:08:20.080 refactoring to be honest that's interesting i asked do any of you
00:08:25.280 use a proper id for ruby development ruby mine yeah it will be mine
00:08:32.000 no no i use sublime stop line for the win
00:08:37.120 it's australian isn't it it's australian for good
00:08:42.560 and it's uh thirty dollars per per per license man
00:08:47.839 we're not affiliated with well i use them then okay i still use bin do you use the
00:08:55.360 vim plug-ins for refactoring no there's a parking for vane fabric
00:09:00.959 factory there's a plugin for everything in vmware so
00:09:07.519 there must be at least 10 plugins for refactoring i guess but um that brings me back to the thought of i haven't seen
00:09:13.200 one so so the tools i use for refactoring is still like get grip and
00:09:20.560 said and vim it's like
00:09:25.680 sometimes miserable yeah i find my my favorite um way of starting a refactoring is a ctrl shift f
00:09:33.360 that's sublime command to look through all the files because when i look for like let's say i change a method
00:09:39.839 i i just use ctrl shift find and try to find where this method is used and that's how i start my
00:09:46.560 refactoring and then i use i often use rays so i put if i don't understand the flow i put race in the method i want to
00:09:53.600 get rid of i want to change and then i just run the code or around the the app to see where they run the tests
00:10:00.320 well what if there is no tests write some tests because uh
00:10:05.839 yeah we'll talk about the test in in my history maybe we'll talk about this a little bit later but let's let's okay
00:10:11.839 let's assume we had tests i just run the test and then i see i see the stack traces so and then i can i get a feeling
00:10:17.680 of how the how the um methods are being used so so my favorite choice is a race i know it's
00:10:24.560 really primitive but it works it works yeah
00:10:29.839 i call it race driven development cool um
00:10:37.200 do you have any like how do you usually reflect her like do you just
00:10:42.720 um i guess do you put reflecting as a low priority or do you have dedicated time for
00:10:50.560 refactoring so i'm our developer productivity team i'm
00:10:58.000 i am so i uh
00:11:03.279 i don't or write a production call though
00:11:08.320 usually so my work is
00:11:14.959 yeah so i my work is uh
00:11:21.839 my work is uh my old work is refactoring
00:11:28.399 yeah so every day so uh eliminating to cause eliminating calls and greater method and
00:11:37.440 a tiny right production code so you're like improving someone else's
00:11:44.320 code every day can i can i ask you because i i i ended up in the same position can i ask you
00:11:50.480 how you um came or how you became the refactoring expert is it because you wanted to be
00:11:56.560 the expert or is it because someone told you refactor
00:12:01.600 i'm really into like i'm serious like how did you get into that position because i find the position great like i
00:12:07.279 love refactoring so and i think you you love it as well otherwise you wouldn't be here and talking about it
00:12:17.200 yeah so do you um do you
00:12:22.880 did someone push the reflecting job to you or do you volunteer
00:12:32.160 oh you can pass how many how many years did you did have you been working as a refactoring
00:12:40.079 power like one year two years three years two years okay two years wow
00:12:46.880 and you're still smiling because um i had the like i don't know
00:12:53.519 how i ended up as a refactoring expert but people actually started
00:12:58.639 hiring me to work on their legacy code to change structure to introduce classes and make it more maintainable and
00:13:05.360 testable or to introduce testing at all and um
00:13:10.959 i totally don't know how this happened like i i mean i'm enjoying my job but i don't
00:13:16.079 know why i i never work on production code and this is so it can be it's it can be really
00:13:22.160 fulfilling working on production code because you develop something then you push it out and users are like hey this
00:13:27.279 is awesome but this never happens to me because i always refactor and no one sees my refactoring that's a bit like
00:13:33.200 yeah i don't know how i keep myself happy but i used to work for a company um called
00:13:39.120 re-interactive and we had a refactoring service called we called it sentinel and what we do is we get these legacy code
00:13:45.680 bases brought to us and then we'd write up a big document like this method can be changed at this and this class should
00:13:51.279 be split into like three different classes and there should be some tests around here and here and here and here are the pain points so we'd write up
00:13:57.680 like a 20 to 30 page document and give that to the customer and then they could hire us again to do that refactoring
00:14:05.120 like as a consultancy or we could just give them the document and they would go ahead and do the refactoring so i think
00:14:10.720 that that's you know a productive use of our time is it is teaching people how to refactor it's like
00:14:17.040 yeah oh that's really unique i have never heard such a practice actually when you so when they
00:14:24.000 when they got this document and they pass it on to you guys telling you like hey please refactor it did you actually
00:14:30.160 do what is in the document or did you just refactor it i think that only happened once and we did what was in the document and
00:14:36.480 then the client ran out of money and we could no longer continue that you stopped working on page 20 and
00:14:42.160 there was still 21 pages to yes sorry out of money cool um my next question will be
00:14:50.320 what's the longest piece of reflector you have down like how many days did it take
00:14:55.920 i had this problem with spree people wanted to have a customized state engine for spree so when you go through
00:15:02.399 the checkout it's not address delivery payment confirm complete it's different steps
00:15:07.440 that refactoring took me four weeks
00:15:12.800 i think and you didn't change any functionality it's pure just refactoring refactoring
00:15:19.760 and adding a slider tweaking the functionality a little bit but just mostly just refactoring around
00:15:25.920 the state machine and yeah that kind of thing oh and i reworked the adjustment system which took me two months
00:15:32.079 which is more it's not as much refactoring as it is upgrading oh yeah that's for two months and four
00:15:38.320 weeks that's so long just a lot of persistence a lot of patience a lot of tears a lot of tears yes
00:15:45.920 yeah a lot of why isn't this code working i refracted it it has to work now
00:15:51.759 right and akiya um yeah what's the longest piece the longest
00:15:57.199 i did i did well i'm sorry talking something like
00:16:03.519 you're not asking me again um
00:16:10.000 something that someone else did um the longest rectangle that i've seen was
00:16:16.160 on active record whoa so active record is frequently
00:16:23.440 um refactored like like totally rewritten by aaron
00:16:30.079 patterson and um
00:16:36.959 uh i forgot his name from england uh josh no what's his name john johnson
00:16:43.199 john lennon yeah so you were part of the team for the refactoring yes sir yeah oh
00:16:49.600 and oh my next question would be so let me let me okay sorry let me go on so um
00:16:56.399 that is a actually very bad example of refactoring because
00:17:02.320 because after that big refactorings that the kind of public api breaks
00:17:09.360 so as a gem author we we we sometimes
00:17:14.799 like experience the total breakage of our gems and so we had to like
00:17:22.400 like put if statements if rail's version bigger than three else if rail's version
00:17:28.880 is bigger than four or something like that so the semantic did change yeah so
00:17:34.480 what i mean is um open source softwares shouldn't be reflected that often
00:17:41.679 oh so today's first tomorrow is open source so far shouldn't be referred
00:17:46.799 to too much what's the time frame on that is that like it shouldn't be refracted every
00:17:52.000 three years four years how long how long is it that's exactly what happened to action
00:17:57.039 view it hasn't been touched in seven years how long is it going to be until the refactoring is okay is it like when
00:18:03.360 you when you're comfortable when you're sitting on your big pile of money that you've made on your open source project is yeah okay so far as
00:18:10.240 it works it works so you could be upgrading and then rails upgrades and then you have to like
00:18:16.400 upgrade to rails three and then upgrade to rails four is that the problem that they made that too quick
00:18:22.960 three to four that's okay measure major upgrade is okay i think
00:18:28.559 yeah so that's the ruby way indeed ruby there are many many like bundle
00:18:34.320 libraries on in ruby but usually the commuters don't touch the
00:18:39.679 code until it breaks i think that's the ruby way right
00:18:46.640 so i think i know exactly what you mean when you say don't change it too much but actually i do the opposite in my all
00:18:52.960 my gems i keep changing apis i keep changing like aggressively i call it aggressive
00:18:58.640 refactoring and and i know i know the problem with um like if rails version this if rails were so i
00:19:05.679 actually wrote a gem to handle that kind of stuff but it's horrible i mean it's but that's
00:19:11.679 the problem with compatibility i mean i love innovation i love changing my stuff if i if i work on my gem and i feel like
00:19:17.360 hey this is wrong i refactor it and then i just bump the minor version i don't bump the major version and this is of
00:19:23.200 course wrong but um i don't care and then so i do like and i know it's wrong you
00:19:29.440 know like and from now on i will always remember you this is wrong
00:19:34.720 but um and i find it um that's for me this is the only way to to drive my gem
00:19:40.960 the the innovation route because i don't want to wait for um like
00:19:46.640 for the next major version bump or something i i just want to if i feel something is wrong i change it and
00:19:52.400 this is why some people hate my stuff because i really change internals and then just bump the minor version
00:19:59.760 but um i don't know the the active record refactoring um
00:20:07.840 well they usually change stuff from from one major to the next major version right so usually the when they change
00:20:13.679 like 3.1 to 3.2 nothing really changes in the i don't think so is it is it that's that's semantic version
00:20:20.000 versioning yeah okay i mean i'm not too i'm not true that's how it's supposed to be done
00:20:25.200 well but it's it's it's hard to say what's public and what's a private api in a gem well anything that's above the
00:20:31.760 private keyword is public well that's that's thank you professor dr big
00:20:40.400 i wrote a blog post about this now the problem is if you write a gem
00:20:47.440 a gem i'm not talking about i'm talking about gems like um let's say active record it's really hard to to define
00:20:53.760 this is public like a find or the where class method that's public and then you
00:20:59.360 have like semi-public methods that that can be used but you're not really sure and then you have private methods and if
00:21:05.360 you change them that's all right in a in a minor version but it's really hard to to um like to
00:21:11.360 define what's my what what can i change what what do i have to what do i have to um document
00:21:17.360 in in gems i can if you write an application and you change the public api that's like clearly major version but if
00:21:23.840 you write a gem it's so hard to like i had this problem 100 million times and then i just usually bump the minor
00:21:30.000 version and people hate me okay um
00:21:35.440 if you don't mind um i want to go back to active record so it's a team of three
00:21:40.720 or four people working on reflector of active record and it took a long time i guess
00:21:48.000 and it's really feasible for multiple people to work on the same
00:21:53.280 refactoring at the same time like to see actually
00:21:58.960 do you have to have divine work so you don't touch each other's refactoring and also to ryan as well i guess four
00:22:06.799 months is a long time it's it's only you working on a factorial did you work with someone else
00:22:12.559 um i was working with the community on that but the community was more like this is what sucks and then i had to go
00:22:17.600 and fix it so i was just holed up in my office in my in my house fixing this stuff for
00:22:23.280 about four months by myself and then i i'd show it to them and they thank you thank you the tears have long
00:22:30.080 since dried up but and then you're married
00:22:36.320 i'd pitch it to them and they'd say yeah that's great but then there's this corner case that it doesn't support and
00:22:41.440 you're like no oh and then you go again and again and
00:22:46.720 again but after four months we did get there um back to active record though i reckon
00:22:52.720 you could work in a team like that three to four people on the same pieces of refactoring so you could say hey i've
00:22:58.559 changed this method it's got this speed up but the api has changed slightly is this okay and then somebody else in that
00:23:04.400 team can go yeah i think that's a good trade-off or no it's not a good trade-off and then you can talk about it working on a team
00:23:10.400 in refactoring i think is much better than working by yourself um in in all regards of programming actually because when you're working by
00:23:16.720 yourself you're going to be questioning is this the right thing to do or is this the wrong thing all the time and if
00:23:23.039 you're just working by yourself you can't turn to the developer next to you and say look at my code is that okay
00:23:29.440 so working on a team with active record it would have driven aaron insane if he was doing it by himself it would have driven him literally insane if he did it
00:23:36.240 by himself he's a little crazy but he's not like he's not going to get locked up anytime
00:23:42.960 but it's a it's a really really interesting question actually because um
00:23:48.000 i don't know if i sometimes i have like let's say a week or something where i need to work by myself because i cannot
00:23:54.720 i cannot especially when you change structures like if you if you just change a method that's no problem in a
00:24:00.240 team but if you change structures if you introduce three four new classes that have different wirings to each other
00:24:06.320 i don't sometimes i cannot even explain what i'm doing but i feel like ah this could be the right thing and if i if i
00:24:11.360 had a like i i have teams to work with but sometimes i'm like hey guys leave me alone for a couple of days i want to i
00:24:17.760 want to play with this and because i would be insane if i had to
00:24:23.440 if i had to define what i'm doing so i don't know maybe it's a the the perfect mix is to have like
00:24:29.919 give people a couple of days if they need it but they still have to be able to communicate what they what they want to
00:24:35.679 do or what they did afterwards to the to the team so maybe 50 50 or something because i couldn't i
00:24:42.559 couldn't communicate eight hours a day with what i'm doing it's like some i don't know like maybe
00:24:47.600 i'm crazy right so i could ask your experience how did it
00:24:53.440 go as a teamwork refactoring
00:25:00.240 i know the answer okay
00:25:06.080 smooth yeah um
00:25:11.279 well one thing i can say is get helps it helps
00:25:19.600 especially the compare view when you compare branches like subversion so in
00:25:25.039 ruby's development we still use subversion i mean ruby core ruby language
00:25:31.840 so we sometimes have some like difficulties on
00:25:37.840 like doing that such big change what's the i'm just curious what's the i remember subversion being really hard
00:25:45.200 with branches right like you had to copy directories or something is that still right because i haven't worked with subversion
00:25:51.200 in like five six years so i don't know if it's still the same i think so okay well why is it still on subversion ruby
00:25:58.720 so in fact there are cerebri committee don't use servers on there
00:26:05.279 all of the committee is a gita as well because um ruby has to support windows
00:26:13.200 and there's no good windows client at the moment uh forget i mean
00:26:19.200 interesting i didn't know that this problem exists so shiban hassan you were saying
00:26:26.400 you are only working on refactoring were you working on a team yes so
00:26:32.559 i i upgraded radio 2.0 to or 4.1
00:26:39.919 or it's uh only work uh it's um only my work it's only used
00:26:47.200 yeah so uh one or one two about a two year
00:26:53.840 wrong i do factor in it so i guess working alone should
00:26:58.960 it's all your call you are the boss cool so
00:27:05.360 in my company i i don't see a
00:27:12.240 team i don't see gym refactoring so
00:27:18.559 refactoring the only personality work personally
00:27:25.679 you always do it alone right oh okay okay
00:27:42.080 so even if the team works on this refactoring thing
00:27:48.399 they more vertically divide the task like you touch this something's on rb
00:27:54.640 and you you can touch this something's on rb so it's not gonna conflict all
00:28:00.159 right so different branches and everything like the same part the same branch yeah and
00:28:06.159 two people working on two refactorings in the same branch yeah oh it's a first week or
00:28:12.559 a several program are refactor the model and next week
00:28:19.039 some program reflected the view and controller and just finished our magic
00:28:27.600 i guess if the functionality is really separated then it works
00:28:32.880 but that's the problem sometimes it's not that easily separatable and then
00:28:38.960 you cannot only work on the view you always have to work in the view and the model and the controller and then so
00:28:44.240 that's why i'm asking if you have two branches for two developers working on two different refactorings and then you
00:28:50.480 merge the branches and merge the branch into master but apparently you work on the same range that's interesting
00:28:58.799 branching is awesome branching and ctrl shift f and sublime
00:29:03.919 that's your factoring and race
00:29:10.000 my next question will be have you ever feel that you have over refactored like
00:29:15.679 what are the signs and i would think we'll start with akira some
00:29:22.720 okay um the question is like
00:29:29.039 who were affected is i mean uh over abstraction maybe
00:29:34.559 oh i was thinking like i regret i did this reflector in it's too much change
00:29:40.880 or something or maybe you can talk about your definition right so i'm going to talk about action view
00:29:46.799 tomorrow and which is in my opinion of too much
00:29:52.559 abstract abstraction oh okay you can find a
00:29:58.480 def render thing like 28 times in the
00:30:04.080 whole library def render renser renzo the renzo method
00:30:10.880 appears 28 times in the whole library which is which is the render method right yeah which is
00:30:16.720 crazy each of those have their own different api sometimes uh options hash sometimes only
00:30:22.880 the view name sometimes view name and option sometimes partial and view and sometimes
00:30:28.000 yes i've i've felt that i've ever refacted sometimes i've moved code from one
00:30:33.279 method into it into its own method but that new method is only ever used in the
00:30:38.799 first method so why have i split that out into a new method it's just to give it a fancy name
00:30:44.240 so my method could have been called something like find client and application and i'll split that out into
00:30:49.279 two methods find client and find application and then the first method i call find client and find application
00:30:55.360 that's when i feel i'm over refactoring when i'm separate separating out methods that don't need to be separated or
00:31:00.880 moving functionality into entire new classes and that new class is only ever used in one spot that's no
00:31:08.000 point in refactoring that you can refactor when you want to clean up the code when it's actually less code and it's clearer you don't click don't clean
00:31:14.799 it up because this file is 100 lines and 100 lines is too long no it's not right
00:31:21.039 i disagreed 100 lines is too long 99 labs
00:31:26.480 i disagree um i mean i agree that so over refactoring usually means over
00:31:31.919 abstraction as you said because it means like you refactoring always means you
00:31:37.519 um introduce new classes or you split up methods into smaller methods that's refactoring right so um but um often i
00:31:45.840 take a huge chunk of behavior and put it in a separate class and even though the class is only used in one place
00:31:52.080 it's it's it's fine it's got an interface now because the the new class has only a limited scope because it's a
00:31:58.960 separate instance so so i see i clearly see okay the dependencies are i don't know like the view context and the user
00:32:05.919 object and whatever so because i have to pass it in the new object that i introduced and i find this extremely
00:32:11.600 helpful even though it's used only once it's it's just and it's also um i often introduce classes that represent
00:32:19.200 like a domain object in my in my application in my gem and i find this extremely helpful for um
00:32:26.000 understanding what i'm doing because i'm looking at the class and i'm like okay template finder that means it finds a template whereas something whereas if i
00:32:32.880 have a method somewhere in actual viewfind template you never know okay what's the scope what's the what
00:32:38.480 variables does it use what's the dependency so i find introducing classes extremely helpful we actually moved out some code in spree
00:32:45.440 that was in the order model we moved it out to another class called order contents and then we could stub in in
00:32:50.799 the order contents test a fake order that has an api like a real order and
00:32:56.240 then we were able to test that when you add an item to the order these things happen when you remove an item from the order these other things happen so
00:33:02.640 having that abstraction having that refactor actually helped make the test clearer we had our order spec over 800
00:33:08.960 lines of code and we removed that whole chunk of adding and removing and checking the
00:33:15.840 order contents out to a different class different set of tests it made the logic a lot more clearer to read through than
00:33:22.000 having it all just chucked in the same thing as ever everywhere else
00:33:27.360 sounds good
00:33:33.120 so over the fact that sign is
00:33:39.279 a break for me so
00:33:51.360 example for uh i changed the uh one method name and reflected it
00:33:57.840 for me but our running test is our very favorite
00:34:03.200 it's over reflected sign so you find the dependencies
00:34:09.520 it's referred to many places at too many places and it's hard to do a refactoring
00:34:16.800 is that what you mean
00:34:30.720 i'm talking about integration tests or units
00:34:46.839 okay um i think it's almost time so we'll take
00:34:52.480 some questions from the audience so does anyone have any questions you can raise your hand
00:35:02.000 oh cool hello i'm alex
00:35:10.880 so we know so you know a race call has a debuter in the json
00:35:18.240 supply is called api and also we have on another like a graph api the
00:35:25.680 the graph that the is also popular and but
00:35:30.720 you know when you like sometimes grab your as you long use
00:35:37.520 it will become very messy and you will like to be asian with the controller so
00:35:43.680 how do you think how well reflected this part because api has become more
00:35:49.200 more important than before i had this problem recently i've been building an api using grape um on our
00:35:55.680 major project at work which is controlling light bulbs with the cloud
00:36:00.880 and we had an api in grape but we wanted to make sure that only the parameters that were being passed through so we
00:36:07.040 were only permitting some parameters to be passed through so we wanted the permitted parameters stuff that's been built into rails four
00:36:14.000 so we could just create an action control parameters object and then pass that in and permit the correct
00:36:19.359 parameters or we could just use a proper rails controller instead of using grape and that's what i've elected to do
00:36:25.520 so i've got a proper rails controller there's no grape in my project whatsoever and instead of j builder i'm
00:36:31.040 using active model serializers which is it provides a nicer api in my opinion to what j builder provides
00:36:38.160 yeah actually my talk tomorrow talks about this kind of stuff like how
00:36:43.520 do i encapsulate my application domain logic so it's so so so it can be used in
00:36:49.920 it the reality can be used in different frameworks you know like how do i how do i encapsulate my api logic so
00:36:56.880 you better be there tomorrow or else
00:37:03.839 because graph api has several kinds of its fiber you know they
00:37:09.040 do there's a one choice they send one choice and also a grab grab entity and the theorizer you can
00:37:15.920 use so do you think you must have an easy way to do this thing
00:37:25.839 i think what are you after something like responders in in grape
00:37:32.079 maybe you know like where i i think i think what you need is like a an object to do your business logic
00:37:40.000 and then a layer to to negotiate between grape
00:37:45.359 or to do the json rendering or so i think i think what what would help is
00:37:51.440 decoupling your application logic into a separate object and then let us that let a third layer
00:37:59.119 uh interact between grape and and your business logic sounds a bit like this like it sounds
00:38:04.880 like what active model serializes provides active model serialize is only serialized but what if you want to deserialize stuff
00:38:11.280 ryan well nick
00:38:16.839 yes you could jason pass it and then write something uh hey are you david hind of my hands no
00:38:22.400 what oh look at this i mean because i was talking with dh about
00:38:27.920 exactly this a couple of days ago and then he said he's um he does usually does the parsing code in
00:38:33.440 a in a what did he say like in himself so which basically means json
00:38:38.720 pass and then you deserialize objects and but typically when i'm writing an api i'm not going to ever consume on
00:38:44.320 third parties api yeah what if you want to consume a post request to create a
00:38:49.599 new order or something that's then so create a new order in a different system no no like i mean your api is uh
00:38:57.920 is it read only or is it read and write because it's written right yeah okay so how do you consume like a post request for creating a new article okay so you
00:39:05.119 get out post to articles.json you post a list of articles and then you go to the model
00:39:11.200 and you say create yeah but what if what if there's more logic involved and not only what if
00:39:17.119 article.create doesn't do all the stuff you need wait i think you're trying to trap me i i totally
00:39:23.359 so um basically to come back i think the solution is introducing an abstraction
00:39:28.800 layer for your business logic and then hooking that to the to gray but i'm not
00:39:34.560 i'm i'm not sure how i know about grape there's a there's a binding for uh using raw
00:39:40.480 which does exactly what i was talking about like that's one of nick's gems and
00:39:46.960 and this um so you basically um run your business logic pass the object
00:39:52.560 to raw and raw does the rendering and parsing and parsing by the way
00:39:58.160 wow so much the singular responsibility
00:40:05.760 is that but am i are we avoiding your question or is does it help
00:40:12.240 yeah yeah now you're even more confused like active model serializer
00:40:20.400 but i agree that rails has rails lex something like um
00:40:26.480 json d serializer because um we have
00:40:33.520 we have uh strong parameters in rail sport but it only
00:40:39.119 like um only cosmetics now i mean it checks only the
00:40:44.640 posted params not the json body exactly so i'm i'm talking about this tomorrow
00:40:50.319 exactly this like um rails rails actually has a params passer for json built in i think yeah but it's
00:40:56.560 it's j rails just has this global paris parser as a rec middleware and what if you want to customize your
00:41:02.240 destabilization what if i want to pass what if i don't want to use the generic
00:41:07.440 json parser which just gives me a hash what if i want to pass json into objects into an object graph yes that's so
00:41:13.760 that's totally lacking in rails and this is where dhh he does it himself but i find it wrong because you're
00:41:19.680 spreading you're spreading your document knowledge to rendering because you have the j builder or whatever and then you
00:41:25.200 have the parsing code which is kind of redundant and this is what you're saying like there's a missing
00:41:31.040 missing ingredient which we're gonna learn about tomorrow thank you
00:41:36.240 for answering my question cool and does anyone have any other questions
00:41:42.480 how did we end up talking about a red disorder um what are the please you look for when
00:41:48.720 deciding uh when to refactor and how much to effect
00:41:54.319 when my eyes start bleeding eye drops can help
00:42:00.240 i know but when i when i'm trying to look through so spree's old promotion code was very extremely complex and
00:42:06.800 promotions are just in general it's actually the reason why they're complex in general is because the stores don't want
00:42:12.480 you to compare to other stores about their promotion logic so you get 10 off at this store when you spend 50 but only
00:42:18.640 on tuesdays and you get like 20 off at the store but it's only for these items and the promotion logic is extremely
00:42:24.640 complex so on spree we mirrored it it was it was also complex and inside of spree i decided to refactor it when one
00:42:32.400 day i was trying to jump through it and i just saw all these update this this method called update called everywhere
00:42:37.760 like what does this update method do okay you update the adjustment it updates the order and then once the
00:42:42.800 order order's been updated updates the payments and the shipments and you're like i don't understand how this is working
00:42:49.119 so i wanted to refactor it so i could make it clear to anyone else who would come along after me
00:42:54.720 and say this okay it goes from here to here to here that's it and any parts along that way after the
00:43:01.760 refactoring if it still wasn't clear i would leave a comment so i'd say this is why this method exists this is what this
00:43:08.319 method does and then i think that leaves a good signpost so if you look at spree's adjustment code now it should be a lot
00:43:13.760 more simpler i mean check out to like 1.3 and compare 1.32 today it should be
00:43:20.000 a lot more simpler um sorry because it's almost time don't want to keep
00:43:25.200 anyone from not having the snacks so
00:43:32.400 um just the very last question one sentence each one of you like do you recommend any resource book blog post
00:43:39.200 about refactoring no martin fowler refactoring the book
00:43:45.520 that's the answer i like sandy metz's book on the
00:43:50.640 principles of object-oriented design in ruby oh okay p-o-o-d-r
00:43:56.400 yeah good book cool and aaron patterson's commits
00:44:02.079 commits okay uh
00:44:08.079 his own commits so our code climate work
00:44:14.400 called climate called climate in a camp company oh
00:44:20.000 dearborn is a good resources for uh moderate money refactoring cool i guess
00:44:26.480 we will put this on the twitter later and then thanks and it's time and
00:44:32.880 thanks for ryan nick sparta and akira sound to come and
00:44:38.960 yeah it's snack time thanks thanks for having us