“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live” — John F. Woods The above quote is very intriguing and it makes us programmers think twice before writing a piece of code. I first saw this when I was at the beginning of my course at , and it changed everything for me, I was always thinking about how to write better functions and use the best linters. Microverse In this article, I’m gonna walk you through some steps to make your Ruby on Rails code more and . You should understand what a basic is before going forward, take a look at . Read and apply each of the steps in order, I will start with some messy code in the and split it through the whole MVC until I get to the . beautiful readable MVC this article View Model Example Project Let’s say we have a model named that has a column and a column named , which is an integer. We want to list all the users that are older than 16, so we know who’s able to get a driver’s license. User name age 1 — Views Main Views Here’s a representation of our initial View: Of course, this solves our problem and will yield the desired results, it would never pass a code review. Just imagine a problem 10 times bigger than ours and you will understand why. BUT For the next step, we are transferring this code to a , which will isolate our code from the . partial view main view Partials After transferring our code to a partial, our main view would then render it and the partial would have the previous main view code. This change is a little better, BUT it’s still not good enough because we have an in our View, which is the worst possible scenario. Check it out: Active Record Query Helpers Using a helper is a big step from the views since it allows you to your HTML code from Ruby's logic. Check it out: separate Isn’t it nice? It looks a lot more organized for me, and also for your code reviewer. But it is not perfect, we didn’t get rid of that query in our View yet. 2— Controllers Wait a minute, aren’t we using an MVC? Then we should make use of it and transfer our code safely to our controllers, here’s how we do it: Right now that psychopath is a little calmer, but he isn’t happy yet, we need to make sure he won’t harm us, so let’s continue. 3 — Models Ah, the Models! This is the heart of our MVC, where we should put the most complex code logic and transfer (almost) everything to. From our example, the most important thing we need to transfer here is that Query, which is currently in our controller. For that, we are going to use a scope: Here you go, now you can sleep peacefully. I hope you could get the main idea behind refactoring a Rails code, you can just simply follow these lines: Fat Models, Skinny Controllers, and Dumb Views. If you liked this article, make sure to: - Check out my GitHub profile