Just when you thought you couldn’t refactor any more…

Video in TIB AV-Portal: Just when you thought you couldn’t refactor any more…

0 views

Formal Metadata

Title
Just when you thought you couldn’t refactor any more…
Title of Series
Number of Parts
69
Author
B., Claudio
License
CC Attribution - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Identifiers
Publisher
Confreaks, LLC
Release Date
2017
Language
English

Content Metadata

Subject Area
Abstract
Ruby's philosophy is to provide more than one way to do the same thing. Faced with choice, we are left wondering which methods to use. In this talk, we will travel together on a refactoring journey. We will start from code that is easy to write but hard to read, and gradually advance to a level where the 4 C's of good code are satisfied: Correctness, Completeness, Clearness, and Compactness. On the way, we will learn new Ruby 2.4 methods (String#match?, MatchData#named captures) and review old methods (Enumerable#find, Regexp#===) that are more powerful than they seem at a first glance.
Loading...
Context awareness Parsing Code Design by contract Parsing Complete metric space Coma Berenices Mereology Formal language Uniform resource locator Hypermedia Videoconferencing Ranking Programming language File format Software developer Bit Arithmetic mean Message passing Process (computing) Code refactoring output Right angle Regular expression Data type Digitizing Numerical digit Real number Streaming media XML Rule of inference Theory String (computer science) Touch typing Regular expression Statement (computer science) Maize Subtraction YouTube Form (programming) Compact space Home page Expert system Personal digital assistant String (computer science) Iteration Videoconferencing Matching (graph theory) Library (computing)
Complex (psychology) Constructor (object-oriented programming) Code Multiplication sign Parsing Complete metric space Coma Berenices Stack (abstract data type) Mereology Disk read-and-write head Variable (mathematics) Hypermedia Computer configuration Single-precision floating-point format Hash function Core dump Square number Videoconferencing Pattern language Bit Motion capture Instance (computer science) Group theory Message passing Hash function Regular expression Digitizing Data type Resultant Writing Motion capture Control flow Number Local Group Revision control Causality Natural number Operator (mathematics) String (computer science) Quantification n-Tupel Regular expression Statement (computer science) Data structure YouTube Hydraulic jump Condition number Compact space Pairwise comparison Standard deviation Matching (graph theory) Poisson-Klammer Expression Operator (mathematics) System call Local Group Personal digital assistant String (computer science) Statement (computer science) Videoconferencing Matching (graph theory) Library (computing) Gradient descent
Greatest element System call Code View (database) Range (statistics) 1 (number) Complete metric space Parameter (computer programming) Coma Berenices Mereology Formal language Telephone number mapping Uniform resource locator Array data structure Mathematics Type theory Hypermedia Core dump Videoconferencing Social class Mapping Block (periodic table) File format Electronic mailing list Shared memory Instance (computer science) Control flow Numeral (linguistics) Hash function Phase transition Pattern language Right angle Escape character Whiteboard Block (periodic table) Regular expression Data type Thermal conductivity Resultant Reading (process) Slide rule Interpolation Characteristic polynomial Motion capture Control flow Student's t-test Rule of inference Theory Power (physics) Number Integer Regular expression Statement (computer science) Data structure Subtraction Hydraulic jump YouTube Condition number Form (programming) Compact space Default (computer science) Standard deviation Matching (graph theory) Forcing (mathematics) Expression Variance Division (mathematics) Line (geometry) Loop (music) Personal digital assistant Logic Statement (computer science) Iteration Object (grammar) Videoconferencing Matching (graph theory) Library (computing)
Divisor Instance (computer science) Thermal conductivity Social class
Coma Berenices
the rules of the theory of movement from the year and this
afternoon but his culture and really excited to be here it becomes and the as I was saying this is actually talk about really but the Ruby programming language and it's language that we all laughed to rights would love to read and it's a language that is a little called but it's still evolving as a matter of fact there is a new release every year and their new methods added every year and so this is just an overview of some of the methods that I'm gonna touch during the start and some of them have been and will be for many years and others a new they were just added 2 comma decimal 4 really is the last December bit format they chose for this talk is about the refactoring journey and I invite you all to come to this joining with me were going to do is gonna take a problem so it's actually a real problem that I had the previous job and then try to find a solution in ruby and I would a 1st pass and then iterate through the problem using some of these methods to make the cold better so whether you are a beginner and all you are an expert to the developer I hope you will all learn something from this side so now that the doors have magically clothes we can get started so let me give you some context here because this is the example of the problem that we're gonna use to look at those methods it has to do with U-tube you're outs in the previous job I had to deal with you to your house a lot if you have ever watched a video need you you have priority in a Europe of the 1st type daddy's a youtube video the 2nd urine is also you to be there's just a shorter form the 3rd and the 4th 1 hour you euros for it YouTube channels and then the last 1 is for you to play so what we have here is a different at your house leery really different strains than mean different things and what we're then try to write is a method that is called parts and it accepts a string as an input and it's gonna tell us what type of you and you to the resource we're looking at so in the 1st case daddy is a youtube video so ideally this method parse would tell us it's a type video and that's the idea of the media in the 2nd case is that he is a YouTube channel so that's what we expect we expect I've channel in the name of the channel and the 3rd 1 it's not easy to be around so it's simply type a known now because we are a is would never be happy we just writing code that is correct and complete we want something more so we're also gonna try to write code that is clear many readable and contract to follow the philosophy of do not repeat yourself so you know this is a problem that I'm gonna talk about in the next 30 minutes and before I start I looking into that I'm just gonna stop here for a 2nd and give you all a chance to think about this if this was like a job interview or something like that and this was the problem what methods would you use if you had all the methods of the Reston the library available to try to so obvious that's so let's start and I'm going to get the 1st pass so what I'm can observe here is that those neurons they start in different ways the first one starts with youtube dot com slash watch and they can imagine that is a video the 2nd 1 just as we youtube dot com slash and they can imagine that's a challenge the 1st the last 1 doesn't even have eat you so may be unknown so my 1st attempt is gonna be to use the matter start with from string and this is a straight from the documentation start with returns true if the string starts with 1 of the prefix is given so you can say hello start with how you can also ask no start would have and how it's also true because 1 of the prefixes is a match hello start with having paralyzed that's false so using this method we can try to find the type of a Europe and this is how a first-pass would look like so how does this rank for what we're trying to achieve where it's not perfect I would say it's clear meaning it's real readable it's British English if text stocked with and so on It's not Compaq because we have some repetition there the method start with appears twice it's not complete where only trying to match the type were not returned and the idea of the name and even worse it's not correct and the reason why I say that is because if you're you're around is simply youtube dot com slash that is not a channel that is actually U-tubes homepage and the reason is that you you has some have strict rules about how the Urals performed Khot and these what is physically the rule when you have a video on you tube every video has a unique ID and this Sadie is always 11 characters and it's not any 11 characters they all letters digits the underscore or hyphen so I don't know if you knew this maybe this is something you can take away from this talk when you have a channel name also it has to have at least 1 of these characters so as you can see it's not as easy as just saying start with we need something more powerful me to deal with regular expressions so for the next iteration we can use this method string match question mark this is actually a new method he was just added to Ruby 2 comma decimal 4 and lets you compare stream with the regular expression is gonna tell you whether
it's a match or not in the 1st case the string ruby is a match for dot dot dot which means and any 3 characters that's true but if you just try dot dot that's false cause it's missing 1 character and if you try p dot dot dot it's also false because the 1st letter is an on the so using this method we can now rewrite of code to look like this if it's a match for certain regular expression then it's a video otherwise it's a channel otherwise is unknown now let me explain what this forever expression means for instance the first one is youtube dot com slash watch question mark of the equal that has to match exactly and then there is this thing in square brackets with the backslash W means these match any letter or any digit and the underscore so it's all there in that backslash W and the and the other thing that we wanna matches the hyphen so we just wanted there and then the number 11 is a quantifier in curly brackets and means we need to match exactly 11 occurrences of those characters so that's exactly what we want that's what you to the your looks like in the 2nd case we still have the same square brackets and we have the plus that means he has to match at least 1 occurrences so channel namely the least 1 character and so with this in mind we now have a solution that is correct where now actually matching videos channels or unknown and I would say still pretty clear as it reads pretty English it's not complete where only retain the tide and did your name and it's not compact as you can see we have some repetition text match text message so let's give it another try and let's try to remove this duplication to do that we're gonna use that to put equal operator this is what the real conditions says the following a regular expression later on with a triple equal operator allows you to compare against history and even more important it's used in case statements with that means is that if you want you can explicitly used for the Tripoli equal tried to compare Doctor Doctor Ruby that's true but you don't have to because if you use a case statement then internally ruby is gonna use that operator so if you say cases will be when the dot dot ruby is actually using that operator and if it's true then it's gonna return starts with P otherwise it's written starts with so it's kind of the same as before but it allows us to remove some duplication because we don't have to repeat match so in short we can take the colored we had before as we change it to use a case when statement stacks when a matches the 1st expression is media and so on so the call these the correct it's still a pretty clear and now it's compact not repeating any method but it's not complete where on the returning the type and if you remember we wanted to return to things for instance when we match the U. N a return the type and the the when the match challah 1 the type and the name so we are they may be halfway through it but we still missing 1 part in short what we want is when we have a match thing the we're matching 1 a return so if we look back at the code that we had that those 11 characters that were matching we wanna capture them and then going to give them a name ID and then returned that in the result the same thing with a channel when we're matching a channel name we wanna return so how do we do that how can we capture and these groups we can use another feature of Andrew B standard library called regular expression captures what it means is when you write a regular expressions in who be you can actually group a part of the expression in parentheses in you can give it a name and if you do that then when you have a match you can ask just for that match in return this is an example from their redox again say you wanna match the string and 3 dollars and 67 cents and you have a regular expression that is the dollar then this group in parenthesis that we call dollars it's any number of digits then you have a dot and then another group called sense so once you have a match then you can just say Give me from the last match just the dollar amount and that is where that a dollar to that c is is a special variable that holds the value of the last match so you can just say give me the dollar amount of the last match given again 3 give the sentiment of the last Matewan against 67 so with this in mind we can now write the code in a slightly different way it starts to look a little complicated but it's not very different from before we had the same regular expressions but now we're capturing the thing that we're matchings in the 1st case we're capturing it would come in that idea and then if it's a match with gonna take that and return it in the resulting hash as ID in the 2nd case we're matching what is the channel and then we're turning it and so finally we have a solution that is correct and it's also complete so if we were now writing will be we could just stop here and say well this just works I don't care no if it's readable if it's complex Lajous go to some other problem but you know way writing will be so we wanna make things a little better specifically in this case but it's still not come back because we're still repeating something here this idea in his name that we gave we actually see 3 times and the same with name below so is there a way not to have this repetition well live you ask because there his and it's another method that was just added to read 2 comma decimal 4 and it's called named captures the concept
is very similar to what we just saw would capture it's just a more complex and elegant way basically when your capturing with tuple groups you can just call this single method name captures and you get back all of them you don't have to individual specify I won the dollar amount where 1 descent amount this is the same example as before so if we just call named captured from the last match which is gonna get a hatchback that says dollars and 3 cents 67 so let's see how we can use this in the core that we have let me break it down for instance that say they were trying to match the URA out youtube dot com slash conflicts that is the name of a channel when it's a match this named capture is just gonna return a hash in this case name conflicts all we need is just then to add together part that we want the fact that the type is a channel and we have back what we were looking for so in this case name Confrey expect shall so the code is becoming a little more dance but it's doing exactly what we want and this is how the court looks like when we're capturing the the URL there we get the deal back in named captures and we just attached type media and we have the result that we want from a captured the channel lame it's the same thing now is this any better than before well I don't know maybe we now are just Kedia head against the wall because it feels like we're not really getting anywhere like we still have code that it's not really clear it's not complex now we're repeating something else so it feels like we iterating but put really not getting there and you know this is a fitting that you all might experience when you're trying to make your code better you think you're following some pat that's gonna lead somewhere and then it does it so normally when this happens you know I take a coffee break or a lunch break or just go home and sleep and then in in the morning and it's like wow and I know would have to do I have to use something a bit different so wouldn't have time to go home and leave the thus give that far ahead but it does work like that when you look at a problem too much of them see exactly why things are happening and in this case this repetition is happening because what we had so far is either if else the as the or case when when and it's almost in the nature of the structures to have some repetition because you were listing all the options as they said at the beginning there are even more options of YouTube euros so if we had to do that will probably end up with some at the beach so is there a way in will be not to do that and instead to have ruby itself do that for you yes at the very so this is look really like the biggest jump there we are gonna make now who gonna
jump and UCLA animals so innumerable as is to have a class and the the structure that sets the Ruby apart from many other languages was our numerable as arrays hashes ranges you know things that you can iterate through and innumerable class has this kind of method that is a very powerful once again from the docks and they fuse find it passes each entry to the bloc and its return in the 1st for which the block is not foster so busy can you have a list of objects and move is gonna go to all of them 1 by 1 and try to see if there is 1 of them the matches your condition and as soon as it finds was gonna stop and return there 1 the 1st example if you have all the integers from 18 to 99 and you say find a number that is divisible by 17 internally Rubin's gonna say if 18 divisible over 70 and then return ELSEIF 19 ELSEIF 20 and so on but you don't have to write that and as soon as it reaches 34 just can return that for you another characteristic of this method is that if it does not find a match for instance if you try to find a number between 18 and 29 that's divisible by 17 by default is just gonna return nail but you can actually override that you can tell will be what to return if there is no match you do that with the extra argument if and so in the last case I'm just saying if you don't find a match just returned the value 0 so it's really powerful because then you don't have to write the if else if you just use the power of what's so now jumping back to our problem this is really like the biggest change that we're gonna see we're not dealing with the cases anymore what we are doing here is we're recognizing that we have a list of patents those are the same part is that we talked about before the 1st way is a regular expression the matches of the new idea and that is gonna be of type media the 2nd 1 of it for a challenge the type chart so we just basically given a name to these phase called patterns and there were were telling me to do is just go through this patterns 1 we 1 try to see if the text that you get is a match for any this expression as a student and so as soon as you find a match just returned what we wanted from before another type video ID 1 to treat so it was a Asbury Park powerful because now we only have to know 8 lines of code then we don't have any method that's been repeated the only portion that's missing is that gray line how do we how can we return something you know what we want in that case basically how can we tell these find a matter that as soon as it finds a match it has to return something specific what we want to return and not just the 1st match to do that we have to use the break statement so would break down as these you know exactly that you have a look en you can use break to break out of the loop and to return a specific result in this example in the 1st case when I use break we're just saying stop as soon as you find a number that's busy with 17 find is simply going return the number itself 34 but if you wanna return something else for instance the strain founded then we can just use break and there was a break with that value when you find a so that's what we're gonna do here and we're gonna use break so now this code is becoming very dense black it's really getting to the core of it and saying go to the patterns so patterns try to see if there is a match for in expression if theory is stopped there take what you captured in the match for instance idea and the idea of the video and then just add the that you met so if we're matching of the the URA out within again type of media and the if matching a channel you rather than against tive channel and the name if not matching anything as you remember we wanted just to return type unknown we can just put it there we can just use this extra argument of fine teachers say this is where you can return if there is no match and so this is getting close to release to being very compact you can actually make it even more complex the fuel line the if condition and this is as compact as it can get probably it's the correct it's a complete but now we're dealing with clear like is it We the you know that it's probably the most subjective of those countries 6 some people might find it readable some people like Compaq called very dense other people like you know to be a little more verbose so you know I don't have an answer for that what we can try to do is we can see if we can do a last iteration to make it a little more explicit so for
our last iteration just gonna take a step back and remind ourselves when we wanted to go we wanted to have a method that is able to parse all these different formats of your house now in the code that we have so far we're actually only dealing with 2 of this Form arts the video ID in determining if we had to put all of them and discovered it would actually look more like this and now it says becoming even not readable because I guess it's it's a very long hash hand you know this pattern structure that we then defied now doesn't really have a specific meaning it's including many patents we have partners for beers patterns for channels punished for playlist so just for a self just to make the code more readable what is important in what's so 1 of the hardest thing to do is to name things since we that have identified at some of these of media patterns and some of them a channel patterns and please parlance it actually makes sense to seperate them in different time constants because if we give them a name and then we can add the new have to read the expression in that we just say this is not the least of the department's if ever you know you too has a new way we can just added there wouldn't have to actually read you know once it's there it's there we have channel patterns and we have these status so it's very declarative it's very readable the only step that it's missing is we want an object is basically gonna go through all of them force is gonna try all the v dependence 1 by 1 and stop as soon as a find match then it's going to go through the channel patterns and so on and so this is the very last method from there will be some the library them gonna into today and that is in where done you with this method does is it creates a new numerator object which can be used as an enumerable so it's an object that you can tell Ruby to iterate through and it this is what we gonna want here we want an object and basically 1st goes through all the view part is then 2 of the channel patterns and latest patterns and this is how you write the code you just use in written you and then was again it's very declarative inside the block you say well 1st for each new patent added to the Patent and benefits to match the type is easier than do the same for all the players patterns and from the champ of and so here we are now it is called that maybe maybe to dance you know as I said before some people might prefer another format and you know I'm happy to discuss this reality after the stock but I think it has some advantages the first one is that really on the 1st lines of code there just declaration new we're just saying is a b the patterns China patterns place patents and patents that that part really is gonna stay as it is and the board could be a code is just the last 3 lines of code that's what we do in the real iterations so in if the 1st lines seem a little scary they're just things that you know right ones and they're gonna stay like that unless you know You Tube adds more formats into just another minute and you're really a logic is just as 3 lines at the bottom so in my opinion have this is good is means correct its complete for sure It's conduct for share may be too much and in my opinion it's clear because of these because it's really the finding things as they come and instead if you have like a Yves LC or a case statement you can't escape from it like as you're reading you have to read all the conditions that govern in a way and so and this is physical a stop just out 1 very last thing those patterns that a type at the top they just say youtube dot com really at YouTube you're asking us to be www dot youtube dot com they can be h to the use calm itch to be asked me to the com you don't add all of that in all of those of the variance of the top you can just tell the matched to that match and interpolate river expression that has an optional capture his physically matching ever ever expression and also so this variance whether there is w w or not it's still going to be a match so and there was a lot through lunch but I really the stock to just give you an overview of the power of some of methods that are inside the ruby standard library and as somebody said yesterday another TOC ruby is very powerful and you can do amazing things with it put in a good way and about we're like you can just play around there are no rules these are some of the concepts that I introduced and you know it's I think it's a mapping and this was a great journey to have because it really you know if you guys have different opinions and if you have seen something that give you some other ideas just feel free to document and that's that's all I have and once again the slides are available and progresses to have any thank you if we
the question is what they consider extracting those found in 2 separate classes yes I would I tried to keep the conduct in the stock the that's that's really if for instance those the hero channel factors it can definitely room classes so you can play around with it you know I really can't see
anything but if you have questions just come find me on wanna be here for the rest of the conference suggests that no opinion thank you we
thank you
Loading...
Feedback

Timings

  590 ms - page object

Version

AV-Portal 3.13.0 (5c8e2efe74178fb46921415ac72371299d2e6b01)
hidden