ElixirConf 2022 - Jason Axelson - Quick Iteration in Elixir - Tips from 6 Yrs of Elixir Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you [Music] hey everyone uh let's go ahead and get started it's 11 25 thank you all for coming to my talk it's titled quick iteration and elixir I'll tell you about talking about some of the things that I've learned over six years of working with Elixir professionally and personally as a hobbyist and just sharing different things that are hopefully helpful for everybody so first a little bit about me I really care about the community as part of that I am a moderator on the Lister Forum I've contributed tons of PRS to various Elixir projects occasionally have been Elixir LS core contributor a little bit less now than in the past uh you can follow me on Twitter and I'm also an engineering manager at felt and a maintainer of various open source projects like x-sync Scenic Library load nerd Library load which is coming soon and depth is you may notice that there is a common theme running through all those applications all being around sort of like Library loading and quicker development and that totally ties into everything I'll be talking about today let's start off with a little bit about My Philosophy so I believe as programmers we should really care about our craft earlier on in my career or actually in college I've read the pragmatic programmer which is a book that I would just highly recommend to anybody that's interested in programming no matter where you are in your career also I believe that investing some times in your in your tools will pay dividends over the course of your career and just add up over time and help you be more productive and effective um also um uh one thing that's really important is to have shorter feedback Cycles in your um in general and in development so a feedback cycle in general is you're making some sort of change in the world and then you observe what the result is and then based on that result you use that as feedback to make another change and then you observe again and you just do this Loop multiple times so reducing the speed of that Loop allows you to move and learn quicker a simple example of a feedback cycle in code is you write some code then you run it You observe the result and you see you get some feedback and you use that to write a new version of your code and this expands into other larger Loops so after you write the code maybe you create a pull request on GitHub somebody reviews it gives you a feedback and you create a new version of the code that's hopefully better and you put up push it up for more review again and eventually that code then gets deployed to production where now you're getting feedback from a user and using that feedback to improve your code even more and if you zoom out even further then you're actually basically getting feedback from the market and the point is that all these feedback Cycles compound so if you're improving the the speed of your local development then you're also improving the speed of PR feedback and then customer feedback and your Market feedback um I have a few goals for this talk today I'd like you to walk away um caring more about your craft than when you came in I want to inspire you to try a few new tips and techniques and motivate you to think in terms of feedback cycles and how you can reduce them in your development and perhaps even in your life I also say what this talk is not so this is talk is not rebuttal it's the idea that you should spend more time thinking than typing I think that is very true I'm just focusing on the typing part today partially because um by being able to have that typing part being very simplified you will be able to stay in like a state of flow and not have like all these like roadblocks where you're getting interrupted all the time the slides will be on my GitHub I'll be going pretty quickly today so don't worry about taking uh too many notes uh so let's warm up so IX is awesome um I my one of my biggest tips just get familiar with iax try all the different functions try out your code in there I use that whenever I'm prototyping any sort of new module I'm about to create one super simple tip to start with is um if you use recompile in Elixir you can get your changes you can get your changes of your project so here on the left I just have a very simple starter Phoenix project and I was going to write a function here def high do io.puts hello Elixir comp and actually before I save that I will start up the server on the right here and now I save this file and so I try to call this function but an error because that code hasn't actually been compiled yet so because I started this with ix-s mix I can run recompile here and the code gets recompiled and now if I run the function I get the results one thing that sometimes trips people up is so say I make another change here and I'd add 2022 to it and then in a different terminal window here at the bottom this could be something happening like somewhere in your editor I run mix compile and this also compiles the code those Elixir source files into beam files on disk and then I try to call um hello phoenix. hi here and I get the old code and then okay maybe I'll just type recompile and I'll get the new code nope still the old code so what's happening here is that Elixir is looking for the modification timestamps on the source files and because it's already been compiled it's not recompiling it one way to kind of get around that is to use as the the r helper in IEX so you do R you pass the module name hello Phoenix and it'll reload the file from disk and then when you call it now you're getting the new code there's lots of other helpers in IEX some basic ones here are H to get information about a function or a module I to find details about a variable and B to get help with different callback functions so I will close this other window and so I can do the henum.map it's pretty basic you can also do that for anything in your project so h Hello Phoenix will get you these docs that you're defining up here in your module dock um I really like the the ihelper if I have like some super large map that's like printing out over a bunch of screens um because sometimes it can be hard to tell like oh do I actually have an okay error Tuple or do I have the actual map so when you use I you can see that okay this is what the data type is it's a tuple it's not a map and you can see like what protocols are implemented um the um some other fun things here is like there's a runtime info where you can see your um information about what's being run and if you type h you can get a list of all the different helper functions here so like you can see recompile here another good one to get familiar with is V where you can get the results of any command that you ran in iax up to this point Colonel don't expect is this another function to be familiar with that's really helpful to have this in Elixir I wish we had something like this in other languages where you can just inspect any term or variable and you get a string representation and usually you can then take that string and actually put it back into IX and get the same result out but not for every data type some of that will be improved in Elixir 1.14 which I'm excited for just something to be aware of another really fun IX tip is that you can actually use IEX to open up any source file of your project in your editor and the way you do that is setting the environment variable Elixir underscore editor and here's an example for how to set up that for vs code so if I go inside IEX and I've already set that environment variable so I just open up enum.map and then vs code loads and directly to the first line of the enum.map function so I think that can be very helpful and then you can look at the limitation all the function heads Etc some other IX tips people are always running into issues with chart lists I see this as an issue like daily on the list of slack or the Forum or the Discord there's a way to tell IX to not print out the result as a try list so by default if you have a list of strings or sorry a list of integers that can be printed as ASCII characters then Elixir will print them out as our chart list which is what that ABC with single quotes is not double quotes um but after you do ix.configure you can actually print out as a list and here's what that looks like as a demo another thing sometimes when I'm developing I will accidentally create an infinite Loop usually they're a lot um not as obvious as this one is but there is a way to get around that and in case you didn't know you can actually just create um models directly inside of IEX which is pretty cool too so here's oops I'm mistyping that to Loop and I guess it was a module and then I call it and now my terminal tongue I press enter I type hi nothing happens so what you do do you control C control C no so instead you do this like map basically like a magic incantation so you press Ctrl G I enter c enter and then now that Loop has been aborted and you're back at your terminal prompt you can see anything it types while everything was locked up so I think that's really cool if you want to see what else is available via that special Control G menu which is built into the erling shell which is what IX is built on top of you can press Ctrl G and then h and then you can see what we just did was that first helper here I to interrupt a job and then C to connect to that job again after you've interrupted it another fun thing here is the J helper which you can see all the different shells and you can even start a new shell it's really easy to start an airline shell just to do s and then now I see um might do press J again I can see the other shells and I'll connect to that erlang shell with C2 and I'm at the erlang trail and then I can go back with the C1 and now I'm back at IEX prompt and the other shell is still running another really cool thing with IEX is that you can actually Auto close brackets so say you had typed like a bunch of random brackets oh that was not valid syntax and then you forget like oh which what's what bracket is next you can just hit control um square bracket a bunch of times and it'll automatically close all the brackets um one one there's a caveat to that which is it will not Auto close strings for you um I did see somebody mentioning this on the Elixir mailing list I think a few weeks ago hopefully that goes somewhere I think this is a feature that's built into the erlang shell so hopefully someone can create a PR to improve that another good tip for IX is there's an IX configuration file dot iex.exs one downside to it is you can only have 1.1 IEX configuration file for any IEX session but there is a way around that with that first snippet that I have listed here which is just checking if the user has a iex.exs in their home directory and if it does then it Imports that file and then it continues on so if you have like a application that might be a good thing to put is the as the first line in the your iix configuration but inside there you can put things like aliasing your repo function or your user so you don't know how to type my app my app my app which gets a little bit annoying especially if you made the potential mistake of creating a really long my app namespace um and this might be also a place where you'd want to put in your iex.configure tarless as lists so that you don't have to look at Charlie's new code or in your IX sessions um also IX is able to save your shell history and it's not something that happens by default so by default if you had closed out IEX and you go back into it and then you press up you don't get any history here um so if you haven't enabled that you should definitely use those incantations from the previous slide and you can also do like control R to like look at like things you've called in the past by search by passing um a search like read line uh now let's move on to some Elixir tips so one thing that I find handy when I'm developing is sometimes I'm working with some dependency and then I want to try to make some changes to it whether I'm adding an inspect to it or maybe changing tweaking the output having a call a different server rather than whatever is configured for uh and one way you can do that is once you've actually downloaded the dependency you just change your dependency to be a path dependency and you just pass it the path to the depths folder where it already exists and then now whenever you make changes in that folder you can restart IEX to recompile and get those changes back in and you do have to re you have to you do you do have to restart IEX you can't just use recompile for that because that'll only recompile your project um and if using the Phoenix code reloader then you can recompile path dependencies by setting the reloadable apps in your endpoint configuration the docs for that are in the phoenixcodereloader.reload um that can be handy if you only have a few dependencies that you want to be recompiling all the time but but the way that I really like to recompile any path dependencies is with the library extinct that I help maintain and so xync is a library that will watch all of your source files and it'll recompile any code as soon as you save the file and you don't have to do any sort of like page refresh they would have to do with like Phoenix code reloader so it's just very um very quick I guess so let's do a demo of that so I will go into my mix.exs for this project and enable x-sync and I just have it locally so that I don't have to depend on the conference Wi-Fi and restart my Phoenix server here okay and then I'll just create a new function here hi2 and I'll just call it let's do a high to say second function and then now as soon as I save the file xync will saw that I saved the file and it recompiled the project and then it reloaded that module for me so now I type hello Phoenix dot High 2. and I get the second version of the function or the second function um which is pretty cool but we can do even better than that so if we go back into IX here and then if I change this to be a path dependency so and so this is sluggifies as a simple library that will take like a human readable string and convert it to just be like Words plus a hyphen in between so just do depth slugify save that and then now I have to restart IEX so that it'll use that path dependency and okay let's create a function that now uses that so back in hello Phoenix I'll just call it slug do so slug Dot slugify and I'll pass in a title here okay and now when I do hello Phoenix dot Slug and I'll pass it hello Elixir cough and it returns a slug for me and if I want to modify that I'll combine it with the previous tip and I'll do open slug.slugify in an editor and now so here's the the function and I'll go down to somewhere at the bottom and modify the results so you just use then create pass it Slug and as the modification I will add a snail icon because that's the closest Emoji I could find to a slug uh and paste that in and append it with the slug here and and then I'll save the file here and switch back and you see it's already being recompiled and when I call the that function again the slug is prepended uh so this is just a really thank you so this is a really easy way to do a quick prototype of modifying a dependency then maybe after this you actually go in like fourth repository clone it locally make the change and submit as a PR hopefully if you're trying to improve the dependency um cool that's how I like to use xync another thing is I think that editor Snippets are just very underused by lots of developers I see people like creating weird helper functions in their in their library or their applications just so they don't have to like when they could just create a snippet and then they're going to have to like pollute their like library or application namespace here's a few Snippets that I like to use um there's one called Lin for a labeled inspect a lot of them are based on iow inspect and then like I have some really thing for piping and also for logging and then I'll show you the q1 in a second so let's just see how that kind of looks so say we have our function here and I want to see perhaps this this title is coming in somewhere deep within the system say and I just want to see what it looks like so I just type Lang Tab and I press type title and then it's filled out this whole i o dot inspect for me and like fraction time would take me to type out this whole thing and one of the fun things about how have oh how I have this setup is it'll actually input the file name and the line number as part of the snippet so if I had like modified it maybe I put like new title I don't know bad example and then I want to inspect it again I just do that again and then now it's gotten a different line number 17 so I can tell the difference between these two invocations and so you can see when I call the function now title is printed out twice with the separate line number for each of them and so say we wanted to return a map from this function maybe with the slug and the original title so you create a map here and then this is where I have my K helper that's very tiny but very useful because it doesn't have anything like shorter Maps so I just type K Tab and then I type whatever um whatever key value I want to use and then it's inputting on both sides of the of the map for me the key and the value then I can do the same thing for the title and that's I don't know I use that all the time it's very handy so here's how creating a snippet looks like in vs code it looks a little bit intimidating it's like a Json blob but once you have one as an example it's not too bad to input and customize it to be be your own so this one is from Isaac yonamoto who uses the prefix ins to trigger it and this is how you put in that relative file path and line number envious in a vs code which is where I got the I got the idea initially from Isaac and here's a similar snippet but in why I snippet for emacs which is what I use my main editor um another tip moving on is to customize your editor I am somewhat biased but I definitely recommend installing Elixir LS um it's very handy for like looking at the the help of of your functions to find out the docs you can use it to go to definition for any libraries and you can even use go to definition on Elixir source code but right now you have to have Elixir installed via Source compiled by a source in order for that to work but it should be theoretically possible to like detect that okay we're trying to go to definition on Elixir source and we'll send you to like GitHub or some download like the code locally and then open it up so if you're interested in that maybe you can contribute to PR um some other General Elixir tips is tracing is really helpful if you want to see the um you can see anywhere in your system when a specific module or function is being called you can see the input to that function and the output to that function and you can even do this in production which is really cool so my some good tracing libraries are rexbug and Recon X I'm really looking forward to Elixir 1.14 which will release the dbg macro which will let you inspect a full pipeline to see the results of all of that oh and like one step Phoenix live dashboard is really cool I definitely recommend the Ecto plugin if you are using Ecto because you can see all like your table stats which indexes are being overused or underused um and it can just be really helpful or you've not used at all which is just a waste of memory and space live book is also really cool to Define modules and share them with people and and just play with things in that and create graphs and visual output um but who here uses git okay yeah I thought so uh everybody's using it these days so I like to customize git a lot uh I have a kind of a lot of git aliases so I don't recommend that you have more than 100 git aliases but here are the top four that I do recommend you you try out so the first one here is gwip so say you are um working on a branch working on your PR and then you need to like suddenly drop it and do like a hotfix for production not that that ever happens to me then so what you just do is you gwap and it'll create a new it'll add all the files um in your working directory and I'll commit it as a work in progress commit and I like this better than get stashed because with get stash it's kind of similar but then it doesn't actually track like which branch you Stash from and then if you go back to like a branch you try to reapply the stash maybe you have like a conflict and then you have to like clear the stash after and then I've lost work that way so I just like to use this instead um if you are using GitHub I really recommend the GH helper which is github's command line client that's super helpful so my GPO Alias here will check out any PR of the current repository so I'm always using that to check out one of my co-workers PR so it's just like GPO space in the pr number makes it really really quick um also the git status output is very verbose by default um and I'll just show you what that looks like so inside here so you have git status and you can see okay some files have been um some files have been modified but they haven't they're ready to be committed some have not have been staged they won't be committed and you have like all this like helper thing about what you should do so if instead you get status short dash dash branch which is my GT Alias then you get a very very compact version where you can see just the branch that you're on you can see which file is modified but not committed and some that you can actually make it easier to see which ones have been are partially staged like the mix.ass which is showing up in both of these lists here and if you ever forget like what the these colors or the letters mean you can just type get status and like the colors even match which is nice so this this Capital blue m is the same as a modified here and similar for the M's over here and I also really like this GPU Alias which will push up on your current branch and set it to be tracking um and then it'll automatically open up a web browser in github's UI so you can create the pr and I find that really helpful because I always like to attach screenshots and videos before I actually initially create the pr so I can do it all in one step and very quickly this is partially how I've created so many pull requests um so another another tip with um get so I'm off I'm often tracking some of my co-workers um branches and sometimes they've like rebased their code because they're the only ones working on it and then so now I have like an outdated local version of it so I got tired of having to always type uh get reset hard and like origin Tyler Slash new feature um and so instead you can do get reset hard and do at curly bracket you close curly bracket and it'll reset to the origin Branch for whatever Branch you are tracking so you can get their their latest code locally uh also if you're using the command line a lot I recommend using TIG it's a really great command line git client it's super fast and you can browse and to see what every um what all the commits are you get the Shahs and the details of what changed also with Git another fun tip is this horribly named git feature called ReRe which stands for reuse recorded resolution so say you are rebasing 10 commits and the first two commits have like some relatively minor merge conflicts maybe it takes like a couple minutes to fix each of them but then you get to commit seven and has like a horrible merge conflict and you need to pull in like a co-worker to like understand like oh what is this code actually supposed to be doing um and then so before you do that you have to wait for them to be available and you um after you this so you do a git rebase abort and then when you get back to them uh you do get rebase again and you are let's see what was I and then you have to go through those first two commits and fix those merge conflicts again even though you already did it so enabling re-re will tell git to save your um save the the conflict resolutions that you told it so you don't have to keep doing the same thing multiple times uh now let's talk about libraries starting with Phoenix so one thing that's been very helpful for us at felt is for every PR that is created we automatically create a separate application and database and we pre-fill that with a bunch of seed data so this is really helpful for any reviewers because if they want to look at a result of a PR all they have to do is Click one of the links on the pull request and it's just so much friendlier for any non-technical members because like your designer is not going to like check out your branch locally pull it down build it fix any conflicts or database issues just to see like if some UI thing needs to be moved by like a few pixels so this is something I definitely recommend to any team I want to create a blog post about it soon we'll see but I did talk about on the thinking Elixir podcast a couple weeks ago which was a lot of fun and worth checking out if you're interested Phoenix comes with a mixed setup Alias it's very useful especially if you're switching branches so I just recommend that you maintain it and use it so that every time after you switch branches you can call it and maybe it'll install any front-end dependencies you need it can call any data migrations and it's one of your changing branches you don't have to think oh do I have to run mixsteps.get do I have to run npm install no you can just run mix setup and everything will be done for you Observer is a really great tool for looking at your all the processes that you're running I found if I have a newer computer that has a lot of cores it can be a little noisy in Phoenix so here's an example of my 12 core desktop machine and you can see the the site the scroll bar is only showing like a quarter of the processes here so that's what happens by default because so many things are based on the number of processors that you have um but you can clean it up pretty easily and then afterwards you can actually see like oh all this processes now fit easily on one screen and you can see the the here's the end point and here's like the Telemetry things and then you'd have your rest of your supervision tree uh probably somewhere down below here if you had like a real application and the way you do that is in your dev.dxs config you set transport options for your endpoint to like gnome acceptors too so I hard code it and then similarly for the the live view socket you can set the number of partitions for each sockets why do Fallout that's your live view socket and your code reloader socket I also really recommend using compile time configuration or not using compile time configuration when possible that way you don't have to suffer through like a large full project configuration full project compile whenever you are changing any of your your configuration and another benefit is that you can then use config providers which only work at runtime you can read more about that on the Block if you're interested also if you're interested in compile time dependencies I wrote a project called devviz which gives you like a graphical view of what which files in your project will cause compile time dependencies on which other projects so this is not generally what you want where there's like this is 32 compiled dependencies on one file so you want to try to keep that minimized and there's another tip for that later um so I believe you should also make the the Bots work for you so to do that one way is to use xrf compile connected so if you use label compile connected fail above zero as part of your CI then whatever you're about to push a PR that will actually create like a compile time Loop um it'll warn you and the the CI will fail so you won't make that uh mistake so what you're avoiding there is having like a set of like 10 files or something that all depend on each other and when any of those files are touched then all of them have to be recompiled so that's just a real bummer um whenever you're trying to recompile and do some work uh mixed format is great you can reduce the amount of nitpicking in your team um and you can enforce that in CI as well I also really like Credo I don't like all the defaults so you have to customize it a little bit but it's very very helpful too I have a few meta tips as programmers we're always coming across problems that's the that's what we're doing with problem solvers and sometimes we come across problems multiple times so I think that the second time you encounter a problem you should just write down yourself a short note because if you encountered it a second time you're probably going to counter it again so having the note there will help you to I remember what it was and then you can come back later and maybe you'll even make like a short blog post about it or create like a til and help the community that way also pairing with other developers has always been very helpful I always learned something new basically every time I pair with somebody I something about their workflow some new tool they're using or trying I kind of liken it to a sort of um programmers like oral history or tradition which is a fun way to learn things if you're trying to put some of these tips into practice one way to help that is like um rather than like say you're about to type IO inspect manually rather than using a snippet um you can just say okay even though I've already typed iot inspect I'll actually backface over that and use a snippet because that way you're building the muscle memory to use a snippet it can even do that a few times in a row to to really start to build that muscle memory um my largest overall tip is Tool Sharpening so this is something that I like to do there's a daily ish practice or maybe weekly where I'll spend five to ten minutes in the morning maybe with my coffee where I'm just working on my tools I'm polishing them I'm improving some of my Snippets or aliases or whatever else and it's just something that's been really helpful for me because over time all these like all this Improvement compounds and this makes your workflow that much better and as part of that I also keep all this in a dock um and so when I'm doing my actual work I'm like I encounter some snag or issue I just write it in the dock to look at later so that I'm not like messing around in like emacs config when I should be like shipping a future or fixing a bug so that's been helpful for me but most of all I believe you should have fun programming should be fun um thank you [Applause]
Info
Channel: ElixirConf
Views: 7,742
Rating: undefined out of 5
Keywords:
Id: BotVs6TXR-c
Channel Id: undefined
Length: 33min 22sec (2002 seconds)
Published: Sun Nov 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.