RubyConf 2014

Técnicas para mejorar el código de tu aplicación Ruby

Jim Gay  · 

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

so I'm going to be talking about concept called East orient code which is related to tell don't ask I'm Jim Gay Saturn fly are in most places in Twitter land on github and everywhere also tweet at me and tell me what you thought ordering but I'm

not going to be checking I traveled West to tell you about going east and I brought these people with me so if you see them say hello I practice my presentation with them this morning they didn't quite get all the concepts so it taught me to go a little

bit slower and this is funny actually this one I posted this picture online a friend of mine asked about my daughter there she is she referring to all of us as losers I don't think so she hasn't said that but all right Eastern encode so I'm going

to be talking about this idea it might not make sense at first and that's okay I just want you to stick with me and we'll get to some rules and things that you can do you can walk out of here and try it on your own code and see where it takes you this

is an idea coined by a man named James lad who's an Australian developer and we'll get into more of what it is and what he says about it but we're going to be looking at our code and see which way the flow of information goes in our code so this

idea of information traveling eastward can help us and information traveling westward can actually be a problem so if you were to look at a compass to get around in your code you'd see queries traveling westward this information going in a particular way

should be pointing that way and commands traveling eastward when I first read about this I didn't quite get the concept until I realized that I could actually look at my code and see this happening in it sometimes you can look at your code you can see

how deeply nested it is and you realize you know a bunch of if statements you have this gigantic angle or rocketship shape or whatever you want to call it and you you can see that that's bad but you can also look and see where there's the information

going is it is is it traveling westward or is it traveling eastward so let's go over a couple rules we'll get into the code and how how we will apply them rule number one always return itself rule number two objects can query themselves and factories

are exempt so it's okay it doesn't make any sense it will one of the great things about programming is that we get to use fancy words words like polymorphism and encapsulation and coupling which has nothing to do with Valentine's Day idempotent

these concepts matter but sometimes it's really difficult to see them or know how they work into our code we also get to use some not so fancy words like this one does everybody know this one yeah there's some Liars in the room this is probably the

most the simplest and the most powerful word that we have that we get to use and we can do amazing things with it this is awesome right although sometimes this kind of gets out of hand too much of this and you think well I'm not really sure what's

going on that's okay you can refactor it it sounds like some people have done that I've heard about people talking about not using ifs and avoiding ifs and sometimes you think about the argument and and it doesn't quite seem right what's what's

the harm in one little if right but the problem isn't that one it's that our code ends up looking like this it's all over the place so let's start looking at some code here we have a class person that has an address and we want to display it

right I know I've done stuff like this in active record when I was first learning rails it was amazing you just put code in your active record object and it displays properly in the view so here's what we might want to display here's some here

are some information right depending upon what data we have we'll display it in a certain way we want to have a carriage return if we've got a street and a city or a you know province and postal code we don't want to have a comma if there's

no city followed by its its state or province so we have to make some decisions about the data and what we do when there are missing pieces I'm not going to keep the type that large so I'm showing a good amount of code you're probably not going

to have to read all of it but I got to shrink it down so let's look at a implementation of this don't read it but how many ifs are there how many five six maybe there's more there's some some down below we probably can't see how many responsibilities

too many too many I think that the correct answer for both of those questions is too many and there's actually a bug too we'll still get a comma even if we don't have a city but it's hard to see I actually wrote this code and I thought well

let me use these ifs and let me find out about this information and see if I can piece all this together and ran it and I still had a bug I'm sure a few more ifs would solve that all right so let's start following the rules rule number one always return

[ ... ]

Nota: se han omitido las otras 2.545 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.