What I Learned to Love About Ruby When I Switched to Python

0 0

[Music] hello the thing works we're good so hi this is what I learned to love about Ruby when I switched to Python I tried it out on some Ruby co-workers I did not massively offend anyone so I feel that it's safe this is a little bit about me I'm a senior software engineer at Flatiron Health Slytherin health is an oncology data company we use cancer data to accelerate cancer research and improve patient outcomes I occasionally write things at the fourth party comm and also at Flatiron dot engineering prior to coming to Flatiron health I've worked exclusively in Ruby stacks about half dozen different Ruby stacks and Python was my first foray into sorry Flatiron was my first foray into a Python company I tried to find some photos to paint the picture for you I found a photo of a happy hour a photo of puppy day and a photo of my current coworker and I was a Yeti I learned that I don't take photos with my offices so we'll jump right into the languages how many people here have worked with multiple stacks in your day jobs professionally ton of people awesome and how many people here have tried to learn a second language that wasn't your native language a spoken language also a bunch of people awesome I know that a coding language in a spoken language is obviously different but I'd like to draw a comparison here and start by talking a little bit about language in general so the quote from a researcher from University of California San Diego Larry Bora disky and the premise here is that language contains away perceiving categorizing and making meaning in the world that languages give us a habit of thought to give us a framework in which to understand our world there is a theory like 70 years ago that a language could limit the way that we understood our worlds if I'd never learned the word happy I could never experience happiness was widely debunked by common sense and also lots of research this is a new philosophy that we're not saying that a language limits the way that you could think and limits your capacity but rather than a language again gives you these habits gives you these natural tendencies in the way that you shape and interact with your world so there's four things I'd like to talk about in this context the principle of least surprise version management's defaults and their consequences and the philosophy all in the context of Ruby so first I learned to love and dearly miss the principle of least surprise talk about what this is first David Hannah Mayer Hansen has a great excerpt on this from the rails doctrine basic premise here is that Ruby should behave the way that you expect it to there's a big caveat here if you are brand-new to coding nothing behaves the way that you expect it to but if you have some degree of experience and you're in an interactive Ruby console and you type exit it exits and if you type quit it quits if you are in a Python console and you type exit it says did you mean exit open parentheses close parentheses or control-d to exit I find this extremely surprising obviously it knows what I'm going for I am not allowed to exit the program I literally never get this right ever and I get this message and then every time I write out my parentheses and we try this again so here's that caveat which math acknowledges himself the principle of least surprise is promised around math because math is the author of the language but as you gain experience with the language and I have found over time that this is increasingly true for me that there are fewer surprises and it's like the program is designed to do what I expect it to do and I think this is really lovely this is in direct contrast to my Python scripts and my Python consoles that died on whitespace errors every time it's like a 15 spaces or a 16 spaces is some sort of existential crisis and the whole thing crashes and burns this is actually mostly surprising during web development I'll be working through a web app I will accidentally space the wrong way I flip over to my browser i refresh my page everything is dead again I find this surprising and this didn't come lightly right this came with a lot of work so math had to jump a lot of hurdles again from GH I really love this quote to smile at and flatter it's human co-conspirator I like the idea of a language and I working together I like the idea of a language responding to me and acting as a complement to what I am trying to do I think it's really beautiful here's an example it's really simple Ruby code we have an array we can run the collect method on the array multiply it by two we can recollect with an exclamation point and it affects our original object is the differentiation of the exclamation point an exclamation point alters the original object the method without the exclamation point does not think this is really intuitive I find it hard to remember memorizing documentation of anything isn't really my jam but I find it hard to remember when I'm effecting the original object and when I'm not I think this is an important distinction the other thing that Ruby excels at in this area is synonyms Python is famously one way to do things synonyms are an anathema to the Python language ruby has a ton of them collect and map are exactly the same the word that comes to you first is the word that you should use and go for it a lot of times companies will have style guides recommending one option or the other they'll have a personal preference but when you're on your own whatever you like works for Ruby you see this right down to the documentation the documentation for find is exactly the same as the documentation for dirt detect and the code samples includes both this is the same code block is used for both you can see these two are the same these are the synonyms I tried to find the actual list of synonyms online I don't know if there exists a list somewhere if there is someone to send it to me searching for Ruby synonyms get some really lovely gems not a lot about the language itself actual jewelry gems there's one more thing I think it's really nice and unsurprising or rather that helps embody this principle of least surprise is the question mark in a method name a question mark to me says a yes or a no a true or false and I can use that in my methods and I'm authoring to really further this principle of least surprise I can make my code less surprising for the future people who read it and Ruby steps me up well with this pronunciation to be able to do that to be able to apply that significance to my future readers so that's the principle of least surprise also known as safely ignoring whitespace and not being an exact memorizer next up I learned to love version management so so much I do this graph of a timeline of Ruby and Python over the last two years I'll put a ruby first the Ruby - L was released in February of 2013 the last major release of ruby ruby 2.0 was released in December 2014 in April of this year it entered security maintenance mode a version of ruby has three modes normal mode and which is fully supported bug fixes and security a security mode where security patches are supported and an end-of-life where it will no longer be patched but you can probably pay a third-party company to pick up a patch version of the language - ddoddo is about two and a half ish years in normal mode we'll spend one year in security mode and after that or the end of life so it's about three and a half years start to finish if we take a look at Python I actually had to triple check these dates because I was so shocked that Python 3 predated the most recent route recent major rulers release of Python qi7 Python 3 was released in December of 2008 python 2 7 which is the most recent major version fish I mean technically a middle number but Python 2 7 in July of 2010 Python 2 7 will be end-of-life in 2020 in April it's 10 years start to finish for a version of the language Python 2 the Python 3 is a breaking change every organization that has Python 2 will have to refactor their code to get to Python 3 and in fact when python 3 was introduced there was a ton of complications all of a sudden every package we had every library we had every support in the community was split between 2 and 3 and maybe the tools in the libraries you wanted existed for one and not together or vice-versa but you had to pick you distill all where you fell on that side of the line flat-ironed health was founded after 3.0 was released and is a Python 2 company and with two and a half years for that to no longer be true before we are in a different situation I think this is really impactful on the cadence of development in the community with it in context like I said I've worked with roughly a half-dozen Ruby stacks I'm I worked with a ruby stack that I haven't seen upgraded at some point where I've worked there I've never worked with a Python stack where I've seen it upgraded it's a super different impact on a community how will we get new features how will we get the latest and greatest how will we get the speed improvements it really has a downstream effect on everybody using your language and I can't tell you how much I missed being able to say hey there's a new version out and it's got some really cool stuff you know we should do go and get it nice and easily this is a Python clock somebody is maintaining at Python clock calm to count it down as we approach this dire date also in the space of version management I want to talk a little bit about packages some of you may be familiar with the Ruby version manager or the RB end manager how many of you are our VM users great and RB ends ok it's actually a fairly big lip I know that these tubes can be a touch polarizing I've witnessed some very heated discussions over which one of these and it never should have been that way polarizing or not both of these are fantastic compared to what I have at least seen is commonly used in the Python community and what I think is really great about these is that inherent in both of these tools is the bundling of the rubies with the gems of practicing this talk and I was practicing saying rubies and pythons and I realized I'd never said pythons before it's just not a thing that we say but rubies coupled with gems where I can switch from environmental environment I can make the transition to an upgrade super effectively I can make this path I have the tool that my disposal that I am familiar with in order to progress through the development of the language the same isn't true in Python if there isn't this common adoption of dealing with multiple pythons at a time how will I get from two seven ten to two 7-eleven or way worse how will I get from two to three there are two tools that are fairly common in the Python community in this space one of them is anaconda and the other one is Python virtual labs so that anaconda first anaconda is primarily targeted at data scientists it is a bundling of Python with the packages I think it's really heavy weight out of the box an anaconda contains over a hundred packages so if I just want to work on a light web app it probably isn't the right fit for me it does have that bundling that our VM and our VM can offer but again it's a strong powerful solution virtual ends can bundle my packages for me it can keep me isolated in one container but it doesn't bundle Python with it these packages alone so if I have a machine with pack with Python 2 7 3 on it and I have a virtual of I have a really hard time switching from a virtual m4 those packages to a Python 3 virtual EHBs I need some other tool some kind soul has ported RDM for pi m this is not the real logo of Pi M it actually doesn't have one and I made this by putting the py on top of the RB so now I'm try to look it up like this but it is there in my experience it's not in common use you can see how our BM is used here for multiple rubies on a machine I can have a local I can have a global I can have this grouping you can see PI M PI M can actually be used to install anaconda so I can just use anaconda out of the box as well and I can have a Python 2 7 and a Python 3 6 on the same machine and I can flip back and forth again this sort of solves the problem it doesn't solve the problem if it's not widely adopted because it doesn't improve that cadence for the community as a whole last side web gems this is a gem file a selection of a gem file from installing a rails gem out of the box so I'd stall rails I open it up I get this gem file and a couple of gems here you can see the version numbers heavily annotated so I know what I'm working with and if I run bundle install I also get this gem file lock which tells me the version numbers of everything I'm working with this is highly transparent it's in a file I have every version number I know my constraints and I know my range this is so beautiful I used to think some other thoughts about gem files but I missed them so so dearly because my Python alternative just doesn't have this level of transparency a Django tutorial if you run it out of the box you don't actually get a requirement text file which is a Python will sometimes used to articulate these dependencies in a similar way to a gem file and if you run a pip freeze to check your dependencies you don't get what's under the hood you get the top level versions but I don't even know if Django is depending on something it is incompatible with something else I would install right next to it and especially because of the dangers of to Python 2 and Python 3 I don't know what complications I could cause in this moment so that's version management also known as lots of rubies and all their dependencies living super happily side by side one big happy family take a look at defaults and their consequences my argument here or rather somebody else's argument here is the default to make a difference this is the last line of a paper called do default save lives from 2003 it's by Goldstein's and will endlessly Johnson Johnson and goldstein and the paper is about organ donation so the question of the paper resolves around this diagram as you can see in the gold you have opt-in rates of organ donation and you see in the blue opt-out rates of organ donation you might look at our gold countries and think a lot of things they could have religious hesitations they could have cultural hesitations maybe they just really really hate organ donation or maybe they just really really hate their neighbors the difference between these two categories is a checkbox on the left in the gold you are required to check a box to consent to organ donation on the right I don't want to imply the data check box sometimes there are actually fairly significant institutional hurdles to getting out of organ donation but you have to opt out so the explicit consent versus presumed consent and it's about a sixty point difference so if we think about that this opt-in versus opt-out and the consequences of it in terms of our framework again let's talk about rails and Django and start with rails if you start with rails of the brain new developer you can go to the rail site and you'll see the rails guide you see a tutorial we can go through the tutorial actually and get to a running web server that interacts with the database it's about 14 commands and we see our versions here we're going to start up a controller we're going to generate a generator model we're going to migrate our database and run our server fairly lightweight fairly from there we'll get a page on to input to post our article where we can put in the title and text well save the article and we'll get redirected to this page that shows our title and text as you may have guessed the tutorial is to build a lightweight blog and behind the scenes we're going to get this this is our rails console which I hope you can see I'd like to look at what is actually here so at the top of our rails console we get a lot of environment information we're running Puma we're running rails we have Ruby we have a certain number of threads we're listening on this port once we get down a little bit into our console we see our web requests we see the parameters that we're posting back to the web server we even see a more robust parameters than what we could sing on the page we see that we're running utf-8 we see an authenticity token we also see our database interactions and our database parameters we see how much time it takes to interact with the database and our render time a lot of you have seen rails consoles before I'm sure more articulating things that many of you work with on the day-to-day but I really want to call out the level of detail here rails ships with three default configurations development test and production there are all sorts of defaults in here we don't care about caching locally we don't care about eager loading locally we do want to kill the server if we don't have a migration that has been run yet we are articulating all of these in this development file importantly we actually don't mention the log level in a development file of the three files that ship with rails production is the only one that mentions a log level and it's set to debug debug is the lowest level of rails logging there are six it is also the only one that shows you database logging why do we set the log level in production and nowhere else when in fact everywhere is defaulting to debug to me this suggests that production is the one where I'm most likely to change it so in my development file I get a set of defaults that there is an implication that maybe I want to adjust these the same is true of my test file and the same is true of my production file but by shipping with these rails make suggestions on what they should be it suggests to us what is important in each of these environments and how we should distinguish them so if we look again at this rails console this robustness of information is built in to this tutorial is built in to how new developers interacts with rails out of the box let's take a look at django django is very similar honest website has a nice tutorial we're actually going to build a polls application this time so that we can pull we work through our tutorial we get about six lines in and the tutorial informs us that we should see a pleasant light blue box with the words it works so we run our webserver and actually the first thing we see is a security our localhost 1 to 700 won and are not authorized to access a django development server out-of-the-box I of course googled this and I got this nice Stack Overflow answer from 4 months ago it only has a small amount of views roughly 3,000 that last count I was not the only one who found this surprising but we add this to a loud host we get a light Pleasant blue we can proceed through our tutorial again about the same number of commands a couple different things are happening here we've edited that aloud host file so we've got the security there we started both a project and an app migrations are included here as well and then we're running a command that says create super user out-of-the-box before we get to a running interactive version of a website in django we're creating a super user creating an administrative back-end for this application and when we run the server in fact the first web that we're exposed to is this one the administrative super user endpoint to manage our polls to manage our users and to manage our groups the very different emphasis on what's important first of all it's pretty it's lots of CSS it's all dressed up it is super usable secondly the implication here is that we're going to need this so whatever you're building your web server for we're going to need a super user outside like right out of the box if we take a look at this console compared to rails this is extremely quiet the one thing that we see here that we don't see in rails the server reloads you see them there as performing system checks system checks identified no issues it's funny because even if it did identify an issue say something like white space it would just die I wasn't able to get it to identify an issue where it didn't die but in theory we reload we continue we get web requests we get timestamps we get HTTP status codes no parameters no database no really not that much environment information besides the Django version itself the really different recall to different energy right this actually looks to me a lot more like a production web request blog as opposed to a development web request if we take a look at the Django configurations the first 30 lines of the Django configurations are referencing security security security security but then there's this line highlighted in red here that these settings are unsuitable for developed or for production it should only be used in development so we both have this massive emphasis on security and the fact that this is for our local environment I think it is debatable what we want a brand new engineer running through tutorial to focus on I think it is telling that these applications focus on such different things Django teaches me to focus on administrative purposes out of the box it teaches me to focus on security out of the box rails teaches me to focus on timing and to focus on my database it's a really different direction to send a programmer in and to increase their sensitivity to at the very beginning of their work with this language if we sum these all up side-by-side we get the sense that rails does have this much more robust information about what's important this is also rather ironic because Ruby and rails are often cited as the magical languages everything's happening on hood and yet I'm pretty sure there's a lot of stuff happening under the Jango hood that I am not privy to when we give new developers our consoles whatever they may be when we set the settings at our more established organizations we're probably we've tweaked these we influence the way that they develop the reason that I called out this example the reason I added this to this talk is because when I left rails to move to Python I lost my database logging and I didn't notice wasn't configured it wasn't turned on a console whatever work precedes and then one day I slowed a website down from a regular load time to a 10 second per page load time and I went looking for my database logs and actually couldn't figure out how to get them into flask I ended up tailing my Postgres log and when I did I found I was making the same rather slow query ten times a page because I wasn't passing the variable around correctly I was just redoing it and I found that I was not nearly the only developer who had done this in fact every page of our application requested the user information eight times when you don't have transparency into this you don't teach people to look for it consider that every policy must have a no action default again from our organ donation paper if you do nothing there is still an implied direction if you pick nothing if you have an absence of choice people still understand it as a suggestion and the implication is that the majority shouldn't have to change it and we see it an organ donation and we see it in configuration we should be really sensitive to this and I really missed the sensitivity built into rails that I don't think I ever understood or appreciated at the time so that's defaults and their consequences also known as training beginners with principles that I personally think are extremely valuable last but certainly not least I loved to learn the philosophy I loved - I loved the philosophy of Ruby I really learned how much I messed up if we go back to language here a language offers a window on a cultures dispositions and priorities and I think Adam actually set me up really nicely for this it matters the way that we talk to ourselves so let's think about how we talk to ourselves in Ruby and in Python fair warning it's fairly different so these are our two founders our language founders on the Left we have Yuka hero Matsumoto the author of ruby and on the right way of guido van rossum the author of Python both of these guys have pretty significant cult of personalities I'm guessing that most of you have heard their names before and we know who they are people seem to nod exactly we know who they are they're important they're part of the origin stories of this language and we talk about them differently in Ruby we talk about Matt and we say Mass is nice and so we are nice those of you who know me might know that I'm actually not that big on niceness per se I think it can have a stifling effect on how we talk to each other we should be direct we should be welcoming and respectful but we should be direct so I'm gonna say that math is nice and so we're respectful I'm in a soir if you will but whether it's nice or whether it's respectful this is a super different conversation than a benevolent dictator for life I think it is fantastic the Guido steers the ship I think he is the captain he steps in at the right point he was one of the main authors of the Python to to Python 3 he made a hard decision for that language and one that can have a really great effect on his community but I think it's telling when we talk about niceness and we talk about dictators it's a different thing that we're telling ourselves my language is nice and my community is nice and welcoming and respectful to each other my language has one way to do things and my language has a benevolent dictator for life at its head there's one more way we talk about these two we talk about the joy of ruby and we talk about the Zen of Python I by no means invented these terms I stole them from the internet and when we talk about the joy of ruby we talk about developer ergonomics we talk about magic I mean it's very playful rhetoric it's a very really it's enjoyable it's fun and then we talk about the Zen of Python talk about one way to do things we talk about things being pythonic this one way to do things I actually find a little grading there are fewer if any synonyms in Python absolutely but I can still write a for loop in a while loop and I'm pretty sure I can accomplish the same thing there are ways to do things in more than one way even in Python and yet that's the party line one way pythonic very consistent I gave this talk to a set of Python developers and they asked me if I thought ruby was harder to contribute to as a team because there was so many ways to do things how could you ever agree and it truly I had never thought of anything like that before I think they really overestimated the number of synonyms that we have but I thought it was really it was really telling that they was seen as a challenge to overcome for a team that we couldn't find like a consistent way to agree on things again I make no personal preference besides the one that I'm subtly stating throughout this but I do think it's important it matters the way we form these habits of thought it matters the conversations that we have with each other and I think it trickles down into the way that we interact in the way that we actually use these tools that we're using every single day Liz Bailey made a call out to why why the lucky got by the lucky stiff poignant guide to Ruby which I will call out here again this was actually authored in 2005 which is why in the top left corner there you can just see live TV in the year 2012 and I didn't personally see this on TV that year but that's alright this is chapter one of wise poignant guide to Ruby it's incredibly playful I love the idea of the cavity of my body being filled with avocado and mayonnaise whip it came with a soundtrack that I think why Ock authored himself that would play alongside as you learned Ruby it's so much fun it's so welcoming and it says anybody anybody can do this I don't want to go too hard on Python so I will call out that there is also a lot of joy and simplicity for the new people joining the Python community as well there is an anti-gravity module that you can import that will return you a random xkcd comic the author of xkcd himself is a huge fan of Python and it is still a fantastic language so that's philosophy also known as being super welcome and having fun with the tools that we're using every day so where does this leave us I want to take one more minute to talk about Python Python as a language is actually a multi master language we think about Ruby we often think about rails they're coupled so tightly together that when I was looking for example of how to demonstrate Ruby and how Ruby behaved a bunch of people gave me rails examples because they conflate the two because they're such a tightly coupled tool set Python on the other hand has three distinct categories of its community systems administration web development and data processing ETL work all kinds of fantastic things you can do with data in Python and I really respect this in Python first of all it's really powerful secondly it's a really challenging hurdle so even if you are upgrading as a community and presenting these challenges to your community to be able to upgrade from two to three massive breaking changes for three separate steps of your community is much much harder much much more impactful and I think for Flatiron health Python is absolutely the right tool we work with a massive amount of data every day we want our quantitative Sciences team our systems administrators to all be able to converse and to work with our data in a really consistent way that being said for all the reasons listed here and many more I maintain I maintain my Ruby fangirl status and I'm very proud to be a member of the Ruby community thank you for having me [Applause]