DEV Community

Mike Coutermarsh
Mike Coutermarsh

Posted on

My Top 3 Ruby on Rails Best Practices

Here is what will give you the biggest bang for your effort.

  1. Learn the presenter and service object patterns. This allows you to break down complex logic into small, self contained and easily tested classes. This makes writing/changing/reading code easy. Remember to namespace things and create clear separation between different objects. Learn more here: 7 Patterns to Refactor Fat ActiveRecord Models

  2. Roughly follow Sandi Metz rules: Sandi Metz' Rules For Developers. No need to follow them exactly. But use them as a guideline. Small models/short methods. Methods should do 1 thing only. You can use something like Rubocop to force yourself/the whole team to follow these rules.

  3. Don't be too clever. What I mean by this is: Keep it simple and follow patterns. Don't add complexity prematurely. You may think you need that fancy event handler/rabbitmq/etc Ruby gem - but it can probably wait. I like to follow the rule of “do I have 3 problems I can solve with this?” before I add in something non-standard (I think I stole this idea from Sandi Metz' book: POODR).

I hope this helps!

Top comments (4)

Collapse
 
lachlanjc profile image
Lachlan Campbell

I completely agree with these ideas, and naturally want to follow them. But recently I've been actively stopping myself from spending time "cleaning up after myself" when I'm coding—I realized that spending time fixing problems I don't have/writing cleaner code no one else will see and I may not even touch in the future isn't a good use of my time right now.

This would obviously be very different if I was working with a team on a project that would be continued to be maintained, I'm just working alone on a lot of one-off projects at the moment (coded several school projects this week).

Side not: Prettier has been brilliant for my JavaScript—I set it up in Vim + Atom so that my JS formats automatically on save, and it saves me so much time I didn't even realize I was spending. I wish the same kind of project existed for Ruby.

Collapse
 
mscccc profile image
Mike Coutermarsh

Very smart of you! I'd do the same thing.

Collapse
 
lachlanjc profile image
Lachlan Campbell

Also—would love to hear about your best Rails performance tips ⚡️😀

Collapse
 
rubyandcoffee profile image
Alexandra Wolfe

Nice post, thanks!