Tricks and treats for new developers

0 0

[Music] welcome my name is David you can find me on the internet the dabit Twitter github I work for a company called michelada like the drink if you've never had one Mitchell is a great drink that of Mexicans drink all the time it's made with ice clam juice magical Mexican sauce chili powder salt lime and of course beer and it's delicious you should get one if you've never had one but anyway the name of my talk is tips and tricks for new developers I'm going to tell you who I'm going to give you a little bit of context why tis dark what I decided to give this talk so long time ago galaxy far far away 2009 Las Vegas my first rails come and it was great rails come back then I was building I just moved on to Rails had my first job I was being paid to ride my first rails app and I was building a huge ecommerce platform but knew nothing about you know the real stuff like were once once you're past like the tutorial face of rails like what happens next right and I remembered that there were several talks at this relic at this particular rail scamp that sort of changed my view and helped me a lot things like solving the riddle of search using Sphinx with rails like it made me figure out that it might not be a good idea to filter my products with sequel or do searches with like you know like searches so I learned that there was this thing that you could use to search we ended up using solar by the way not Saints but anyway I sort of learned something new I learn about web rat you probably don't remember web rat but it was what we used to test applications before Dobara and all these fancy new stuff that we have but I learn about it and then the most important one or one of the most important ones is I learn about get I was using SVN before that and bezel sourcesafe before that so I learned about gift and and it was I think it's one of my the best Doc's I've I've ever heard it started with get in 60 seconds I remember clearly and the speaker sat there and just basically read that in less than 60 seconds it was amazing and then he proceeded to do the proper talk right and another important thing that I learned that railsconf is how to put like what was I to do once I had my application I learned that there was this thing called mongrel that it it was a web server remember about mongrel of course I learned that I had to you know set it up in different ports you have a process a mongrel process that started in each of the port and then you had nginx on top or Apache on top of it that's sort of like the de proxying around and then if you want it to scale well you scale back that just added more servers and Asia proxy to do your the proxying and you will be fine you need more more power you just added another one of those and that was back then in 2004 Puma and all those new fancy - the stuff that we have so I remember Las Vegas right so I think that we still need stalks for beginners at railsconf we do need to know how to do like micro services and how to handle a gazillion of requests per second and all of that but we need stalks for beginners so people can come here and learn stuff that they don't even know they needed to know right so this talk is for beginners most of things that you learn here maybe you already know it maybe maybe not but I do want it that this talk is for beginners and it's just based on my experience what I do when I start a new rails application like the things that I always use things I usually do I know that so hope it helps for you I had to remove a bunch of stuff but this is the most important things that I think will work for you so let's begin first of all let's talk about configuration files when you start a new project right you use your rails new command the first thing that you want to do well it's gonna do like your bundle stuff it's going to copy files and all that the first thing that you want to do I considered this a courtesy to other developers is please copy your database file into an example file and then ignore the original one so we can always have on hand a computer action file that we can base on it's awful to go into a new project and then you know start your rails server you have no database Jamo file and then you realize there's nothing you have to go google it paste it and over it's just time consuming so please keep an example file of your database file it's it's it's going to pay out in the future and do so for any other configuration file that you think that it's needed for the project to run please create sample files and copy them and just ignore them for not the real ones from from get right and what we're talking about computation files did you know that you can have as many as you want that you can separate those in different files like for example you can have a payment jam of file and then you can just call it from the application like this you use config for and just send the the name of the file as a sim and then you will get that particular configuration so you can you can use it to just organize your computation files better and everyone will be happier when when when it's cleaner and you know exactly what kind of configuration is on each of the file and then remember that whatever there's a string or an integer on the code that's comparing to something else you probably probably want to use constant but setting what those config files so remember that if you if you see code like this in your in your rails application try to change it to something like this use a constant for that 30 and if you want to go like if you think it's going to change constantly just move it all the way over to the configuration file you're going to be very happy that you did this when your boss comes and say hey I need to change that to 10 minutes and then 15 minutes later she's gonna say like a change it to 20 minutes this way you can you know where to change something just restart your server and you know you're done instead of like looking for that setting in the codebase let's talk about the database now this one you can't imagine on 8 years of being a rails developer I've seen it a lot do not get ignored the schema file please I have been to teams where or adjoining applications with there's no schema file and then I asked like what happened to it and add there were too many conflicts with it so we just got rid of it like that's the point so please do not ignore your schema file it's very important that you always able to restore your database state with either DB setup or DB reset especially if you are new if you're new to the project you can just go there you know computer database run one of those and you're up and running to to start coding so please please do not do not ignore that file it's very important that you find those conflicts and then talk to your fellow developers to say hey this column yeah yeah me too so let's figure it out right instead of just ignoring it this is a tricky one don't change your data in your database migrations this is hard to achieve so I'm going to change a little bit the tip to be careful when you change data in your database migrations it is pretty common that you do something like this do you add a column to one of the tables that you already have and then you decide that you want to fill it with something maybe based on the the current data the jewelry have like here right we're splitting the code column into two columns so you do something like this right you add the new columns you go through all the companies you split it and then get rid of the of the old column the problem with this kind of code first of all is that it's not future proof it's not future proof because for example what happens in the future your company model is now the organization model for example this migration won't run anymore and while migrations are not supposed to last that long they're just you know they help you to keep track of history of how the database about your schema RB file is the one that's supposed to have like the the real data structure database structure it's still nice to sort of you know try to keep it as coherent as possible the other problem that you might find with this kind of code is that you can you know have longer deploy times it's very very common that you're you know on development mode and you run this migration in front really fast but you forget that there's like three million companies in your data base so now it's going through all of them in production and you know it's doing all that and you can't like your databases in a weird state maybe there's more migrations are coming and the site is can run without those migrations so you're you're doing maintenance mode or whatever and your boss is angry because it's taking along and you forgot about the three million rows so finite to do that you're risking downtime basically if you're changing your data and your migrations and the other one so this code for example this is going to happen in production I guarantee it dude if you forgot that maybe code is nil so it blows up and I hope you know you're stuck with a migration weird stayed on the database and everyone's yelling at you because you have to log into the server change that code real quick run the migrations again I've been there so it's a really bad idea to do this so one way to fix it refers equal over Ruby if you're gonna do it you're going to be careful but if you're going to do it you might be better off like this do your thing update you know use regular sequel to do to your update and get move on right this is faster of course because it's not I driving through all of the of the objects and not entertaining anything so it's just updating whatever needs to be done if there's Neil it's not going to blow up so so this is way faster so try to think your your changes in data a sequel and prefer this it's going to be it's going to be safer to do this but the best thing to do in my opinion is to use a gem like this rails data migrations there's a lot of gems that do the same thing but I think this is the the best one because it works with rails five by the way and it's if this gem is going to give you a different set of rake tasks for your data migrations right so for example if you had this this regular migration once you have the gem your added column and then doing an update o on all the all the records on the table what you would do in this case is create a data migration you know set authors active and it will create a special migration in a different folder and then in that migration you will add your data changes right but you don't need them anymore on the regular migration and now would you deploy you will run first your day your database migrations that will change the structure and then with a different rake task you will run your data migrations so it's a separate process and it makes everything cleaner and they also keep track keeps track of how data is changing using the same like the timestamp that the database migration use so this is way better and if you don't want to use a gem at least maybe first make all the migrations to change the structure then add migrations later to change the data right let's talk about background jobs it's very useful to say to use background jobs you have something in plate like sad cake rescue delayed job I still used delayed job careful months ago and what I notice is that it's a regularly a bad idea to pass objects to your methods that go in the background there's a few things that can go wrong but what I mean is this let's say you have a job class that has this perform method and you probably want to do this right you have an order and user and you're going to assign it to it what I mean is that it's better if you do this just pass the IDs then find again your objects on the job and just do whatever needs to be done the problem with using objects is that they're usually converted into Djamel and they end up being like huge strings that if you're not careful and there's special characters in a in a description field or something like that it just makes everything blow up and you would would not notice it until you see like all the alerts coming up so it's very beauteous use the ID it's smaller to just you know pass an integral around instead of a whole object at general and it's it's just gonna you're just going to be-better it it doesn't matter in terms of performance because it's already on the background so yes let it do it right and speaking of background jobs always when you start a new application always always deliver your email later don't wait until you know your first SMTP authentication error happens and then makes your check out blow up because you were expecting to check out to send the email after payment was completed or something is that always when when you start you start thinking about delivering your email later always and the same rule applies for mailers instead of passing objects try to ladies and just pull them over from from from the code on the baler right this way even if you don't have anything in place right now to sort of delay the sending up the mailers in the end you're just going to need to add that delay method if you're using side kick or delay job or if you're singing active job just use the label later and that would be it but use the integrals don't use the objects for the same reasons after background jobs okay mmm let's talk about breath this is my favorite lesson for all new developers you have to be restful all the time rest is like the core of rails and NBC and everything that you're doing and it's simpler it's very simple to to sort of explain but it's hard to like actually like actually implemented let me tell you why rath to me it means that on your controllers you only have this actions nothing more actions show with the index show new create a dip update or delete no other action but it's pretty common to find in code bases things like this right you have the products controller and then you have an action to deactivate the product and you you create your route and you get and member whatever right this is wrong this is not restful when you think about Breslau you should think that for every request there's a something a resource that's changing so in this case when you're deactivating the product what's changing it's the product State so the correct thing to do here is to create a product state controller and you're updating it right this is more restful much more wrestle for example you have if you have a shopping cart right instead of having an action to apply a coupon what's happening you are creating a discount or a shopping cart discounts create a controller for the discount and you are creating it this is restful right this is very common if you have contacts you are you have a search so you create a search action wrong that's totally wrong you create a contacts search controller searches controller and then you have an action for that search this is restful right you have something that is changing because that's what restful it's supposed to be so the way to make sure that you're complying with this rule it's very easy just don't have controller actions that are not on that list it's always index show it new create update or delete that's it if you have an action that's named differently wrong right okay now a few rubygems that I think must be on all projects I find them useful I always add them so I hope this helps for you too this one you probably heard of a lot Rubick Rubick up it's going to analyze your code it's going to check it how it's written and it's going to tell you where you're doing things right and when you're doing things wrong and it's automated so it's pretty cool instead of having someone yelling at you it's Rubik up like showing you better teachers and humans trust me a very common excuse that I've heard about not using grew up in your project is because you're already you know you have a large application you're already it's been two years in development if you added right now there's going to be like 500 errors and you don't have the time to fix them that's okay what you can do is you can add this run it with this option out to outer gen config and it's going to create a roof Rubick up to do that jumbo file that's going to include an exception for everything that it found so that way you just add it to you to say sort of an include file to your main profile and that way you have like a sort of a clean slate at that point in time and then you are supposed to come back later that's why it's called a to-do file and remove all those exceptions and fix them that's what you're supposed to do we probably won't but it's fine right at least at least from that moment like for example right right there each screen at least from that moment on if there's something like if you write bad Ruby code it's going to tell you starting from there instead of trying having to fix all other like 500 or whatever errors you find out - Jen config O's also helps for new project so you can like pull configurations that you might use like change the rules depending on your style for example there's this one where it configures Rubik up to how many columns how many characters you see per line I usually have it at 8090 you can set it to 130 I know there's people that says we have large monitors like why do we need that the thing is that you know your vision is only like it's really narrow so it's better view if you have less characters to read at once let's have to like sort of nest code around so it looks better if you if you try to to remove the characters on your lines but try that one this one style and documentation I usually move it to to the main file because I'm not going to write comments on all my classes or methods or anything so I just get rid of it who's no one has time for that right and this one I think it's important if it's the one I value the most about RoboCup the metrics method link it forces you to make methods are no longer than 10 lines it's hard to you know comply with this one but if you do your code will so much cleaner and so much readable so trust me tried this one I'll allow you if you go to 12 or 13 lines that's fine but don't go over that but you know forcing your methods to be 10 statements and that's it helps you like clean up your codebase it's gonna you know you're supposed to try to abstract all the other like the code into smaller methods and then test those unit testing that's why I call unit testing so keeping this one it's like really it's going to make your code so much real bow and so much better and then if you if you want to run this before you push code to your to get you can use a hook if you add that file that get slash hooks slash pre push it's going to run Rubick up before it actually pushes the code to the repo and if it fails then the push will be canceled so that way you can just push robocop failed oh I have to fix this first fix whatever offense you have and then push it over it's very useful for example us we have a CI server that actually runs through backup and a repos are filled with comments that you know read like fix that woke up think that rook is because the bill fails when hookup fails so its usual to push wait for the CI server fail why because you know you do maybe you went over with the with the characters or whatnot so you have to make a commit that only fixes that F Rubik up damn it Robocop there's a lot of commits like that on our repos so when I tell the guys it justice use a hook so you know you your run Robocop before actually pushing and if it doesn't pass and you won't push and you can fix it not curse at it annotate this in our gem that I don't see often and I don't know why but I love it annotate what what it does is let's say that you have a like a model user with with name username password active whatever when you run annotate on a rails project after you do your migrations what it's going to do is it's it's going to well annotate your models with information about the database automatically basically so if you add columns you just run on a date again and it's going to it's going to add that information at the top of your classes and it's going to do so for models test file unit test files and factory CPUs so it works as a reference quick reference if you are working on a model about the columns that he has instead of you know going to the database or um checking schema or whatnot do you just have it right there at the top of the model and you can see it real quick so it's very useful to have this these annotations handy and this one is pretty useful too if you use routes as an option then it will annotate your routes file so you don't have to rake routes every time you you need to to figure out where my controller should go you you will already have it and if you're very old like me and use VI as your text editor you're going to thank that it has like contextual autocomplete so it basically autocompletes only on what's open so you open your route file and you have like autocomplete for all those paths method so it's it's pretty cool very useful but I don't know why people don't use it so now you know reduce it annotate for useful bullet bullet detects the n plus 1 problem as it's happening as you're developing if you don't know what the M plus 1 problem is it's very common in rails but it's something like this so let's say you have a book method that belongs to author has many comments then you have the author that has you know name whatever and then you have the comment that they'll also user belongs to book and your index file looks like like sorry your index page looks like this right so when you go to this page the controller usually looks like this probably and then when you load that page your log files will look like this there's a bunch of queries going on like three queries for queries per row and and you know it's it's just getting all the objects one by one because that's what you told rails to do it doesn't know what else to do so Dennis called the n plus one problem it's very common when you're developing rails applications so if you use pull it and you configure it let's say for example what I'm doing here is well enabling it I wanted to show on the console I wanted to show on the rails logs and to add the footer what it happens is as you load that page you will notice that there is a footer that says hey you know you're probably better off if you include the author on the query or if you include the comments on the play and it's going to jail at you from the from the console to from from from the rails lover right it's gonna tell you hey you know you're doing something weird here so why don't you fix it and you're going to go do that okay yeah let me fix it so you go to your controller you just includes properly you nest it as needed and then when you load your page you will now have you know the queries for for each of the of the classes you don't have like bunch of micro queries so this is way better and bullet you know we just tell you as soon as you're developing that you're doing something not wrong but maybe weird and another similar gem point this detect memory leaks before it's too late I don't know if people work with Rails applications that have memory leaks you have a memory leak when your applications start growing like I don't know 1.2 gigs in RAM and then you need to restart actually that's the best way to fix memory leaks on Rails application just setup Monat and if it goes past a threshold just restart the server done right because it's so hard to find memory leaks on the code on Rails it's very very hard once it's there it's not gone ever like you're stuck with it deal with deal with it basically that's that's a solution so owing when you have it on your on your code base and you it's it's some sort of middleware so you initialize it and it's going to add at the bottom of your logs like when you're developing this information which is basically how much memory it's using and the number of objects that you are instantiating in that request so if you're developing something like this and you see that you have like 500 comments objects instantiate it's something must be wrong there's something wrong going on and it's actually kind of safe to use in production I wouldn't enable it all the time but if you're sort of trying to figure out where the problem is I would enable it and then just let it log a few requests and try to find where I'm inserting a lot of objects so it's pretty it's very useful for them and Wow right on the 30 minutes that's it that's a that's all I have for you right now like I said I had to remove a bunch of them of like advice but this is like what I thought was most important and I said thank you [Applause]