RubyConf 2014

Aparca tu obsesión con las cadenas de texto y utiliza bien los tipos de datos de Ruby

Dave Copeland  · 

Transcripción

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

Dave Copeland otherwise known as Dave Front 5000 on Twitter I wrote this book about how to be a good software engineer I wrote this other book about how to build awesome command line applications in Ruby and these aren't particularly applicable to our

talk but what we'll learn in this talk will help us build better applications and be better programmers I hope the title of the talk right overcoming our obsession with string Lee type troubies extremely typed as what we're talking about is anybody

know what I mean by by string Lee typed oh and you came anyway so if you've ever heard of Jeff Atwood otherwise known as coding horror on Twitter he writes his programmers blog and he's got this great post it's a few years old he goes through different

types of program or jargon and there are these very amusing phrases for things that we do as programmers and the one that I thought was the most amusing was this one string lee typed and so what he says in the blog he says it's a riff on strongly typed

used to describe an implementation that needlessly relies on strings when programmer and refactor friendly options are available and we use strings all the time input comes as strings output needs to be a string rails love strings databases love strings we

use them all the time but it can get us into trouble if we use them when we really shouldn't so we'll have a motivating example to just demonstrate how we can get into trouble so we'll talk about zip codes so if you're not from the United States

this is also called a postal code but basically it's a bunch of numbers that help the post-office figure out where a particular piece of mail is supposed to go it's like sort of a pre sorting sorting thing that they do and the basics of the zip code

is that it's five digits that it's not a number it looks like a number but it's always five digits even if they begin with zero so like ZIP codes in Puerto Rico would start with zero zero nine and like I said they map to a particular area of the

United States as defined by the post office and you can sort of read into them like the first couple of digits can kind of give you a sense of where in the country it is and you can see that the numeric value of a postcode or a zip code is lower on the East

Coast than it is on the west coast but generally a just a a meaningless string of numbers that they use so with that in mind we're going to look at a very simple application designed to send letters to people so we have a database of addresses and we have

this third party API that we're going to use that's gonna allow us to send a letter so like your account is overdrawn or you're due for a refund or something like that and basically that service will take care of like printing and folding and stamping

and mailing the letters and we do what programmers do which is make two systems talk to each other and and wrap some service to do something more specific for whatever it is that we need to do so this is the Ruby API that the third-party mailing service is

providing us that's what the implementation isn't there so we will configure a bunch of letters that we might want to send in their system and those all have IDs so we will find whenever we want to send a letter we will find it by the letter ID which

gives us back this class that has this method called mail that takes the four parts of an address as strings a street city state and a zip code and so calling that we'll mail that letter to that address so to build our part of the system we'll make

a database of addresses and we'll use strings store them because that's what the database gives us it's a pretty reasonable design table and then we'll have some code of our own so this bit of code here it's job is to read in address information

from the command line and store it into the database and we can assume for the purposes of this talk that this dollar database thing is some super simplified way of getting data into the database it's not anything like active record it's just something

simple so that we're not too worried about the database layer for this and so we can use this bit of code to add stuff into our database now to actually send the letters we're gonna make two classes so the first one's job is to wrap the third party

mailing service with something that's a little friendlier for the internals of our system so our system deals with an address as a hash so this letter sender is going to take the letter ID of a letter we'd like to send a hash of the address to send

it and it will handle figuring out from that hash how to call the third party service the second class that we're going to need is going to handle getting an address out of the database oops sorry there it is so it's going to take the idea of an address

and the idea of a letter using the ID of the address it will again use our simple database API to get the address out and then call the letter sender so this is very simplified right but we've separated concerns and you know this probably has shades of

an application that you work on this is very simplified but you know it has all the hallmarks of what we do or integrating with something we're wrapping this we're getting things out of a database and everything in here is a string but it's you

know pretty simple so let's see it in action so we'll store an address 45 South Fair Oaks Avenue Beverly Hills California and the only a zip code everybody knows 902 now okay so we've created an address is the edge of the peach pit from 902 a now

if anybody ever watched that so let's send the peach pit a letter okay we sent letter number 12 - the peach pit everything looks good no explosions everything's great happy path works everything is looking pretty good let's send another one so

1675 east Altadena drive in Altadena California now this is the address of the house that they used on 902 102 represent where Brendan and Brandon Walsh lived this is where that actual house is so we're gonna send that house a letter but instead of using

the properties of code for Altadena California we're gonna just type Walsh database successfully inserted that row we now have an address that is clearly wrong let's send the Walshes a letter okay well the letter sent so that looks good right except

that the addresses is wrong Walsh is not a zip code yet our system was totally happy to send a letter to an address that had that as the zip code so what might go wrong in this case so if we're lucky the best case is that our third-party mailing service

will blow up and tell us and this is a bad zip code and refuse to send this letter every other possibility is worse we'll send what if it gets sent to the post office what will they do will they return it to us maybe will they trash it they might will

they try to send it sure and if they do send it somewhere where will it go is there a West Altadena drive and they send it there by mistake is there a east Altadena drive in some other town in California where it goes who knows we've we've given them

an invalid address we've made their job hard and if this letter was important it might not get where it needs to go so clearly there's a bug in our system somewhere so you could argue that the problem is in reading the input right where we have this

zip code as a string but the not every string is a zip code so maybe if we restrict our input from being anything that doesn't look like a zip code okay so we could do that so now when you're adding addresses to the database we're never going to

[ ... ]

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