Learn Ruby on Rails Best Practices With One Exercise
Tags: ruby, rails
Published: 2021-07-15 02:00:10

---
title: Learn Ruby on Rails Best Practices With One Exercise
description: You're craving some squeaky-clean code, but all you have for lunch every day is a big bowl of legacy spaghetti code. Here's one way to change that.
published: true
date: 2021-06-30 20:00:00 UTC
tags: ruby, rails
cover_image: https://www.thd.codes/images/posts/thdcodes-learn-ruby-on-rails-best-practices.jpg
canonical_url: https://www.thd.codes/posts/learn-ruby-best-practices-with-one-exercise/
---

You want to write smooth professional-looking code.

You're craving some squeaky-clean code. But all you have for lunch every day is **a big bowl of legacy spaghetti code**.

You consider yourself a decent developer, but you're never sure how to organize a project. How to name things. Where some piece of business logic should live.

_**How can you become a Ruby on Rails expert if the code you read every day stinks?**_ 🦨

Is it possible to learn and follow best practices when you're trapped in a big ball of stale legacy code? 

Of course, it is!

Start changing your coding diet today by consuming something healthier.

Yes, it's going to take some work. But don't get discouraged. If you want to advance your skills, you've gotta try something different.

Ready to get started?

## First: Be Careful When You Ask for Advice

You're committed to advancing your skills. When you ask people for guidance, they tell you to read a bunch of books and start throwing these terms at you:
> Skinny controllers, fat models, service objects, design patterns, DRY, SOLID, OOP...

The list goes on.

Is this information really helpful to you? What are you supposed to do with all of that?

The problem is that it's not actionable information, just a bunch of ideas and vague suggestions.

There is a more practical way to learn all this stuff. Here's how.

## One Exercise to Teach You Ruby on Rails Best Practices

Instead of reading another book or watching youtube videos about pasta making during your lunch break, do this exercise instead:

### Exercise

1. Set a timer for [10 minutes](https://e.ggtimer.com/10minutes).
2. Open this link in another tab: [practice.rb from Upcase by Thoughtbot](https://github.com/thoughtbot/upcase/blob/master/app/services/practice.rb")
3. Read the `app/services/practice.rb` class.
4. Ask yourself: What's going on here? 
5. Come back here when the time is up! ⌛

Sounds good? 

Okay, now go do it!
I'll wait for you.

...

...

...

⏰ _beep! beep! beep! beep!_ 

Now that the time is up, answer at least one of these questions:
- Why is this class so short? It's less than a hundred lines!
- Why is the variable `trails` being passed down to the `initialize` constructor?
- Did you notice these methods `promoted_unstarted_trails` and `unpromoted_unstarted_trails`? Why do you think they were given these names, and what's the difference between them?
- What is this class responsible for? 🤔
- What else picked your interest?

Write down the questions and your answers to make them stick.

Last but not least:
- Repeat this exercise tomorrow, but pick a different class from the same repository.
- Add a daily reminder to your calendar so you don't forget about it. 📅

### It's okay if you don't understand anything

The goal of this exercise is to notice some patterns. Not to understand every single line of code.

Use your answers to the questions above as a _study guide_. Even if you answered "I don't know", that's useful information. 

### It's okay if you feel uncomfortable

Feeling uncomfortable means you're doing something different. You're challenging yourself. You're learning and growing.

If you're thinking _"OMG this code is too clean and my code is super dirty!"_ or _"I'll never be able to work on such a clean codebase!"_, don't worry!

That's okay. That's not the point of this exercise.

The point of this exercise is to read the code and notice anything that looks different or any interesting pattern.

## How am I going to learn anything just by reading one class?

It might seem like it's not much.

If you do this every day, here's what's going to happen:
- At the very least, you're going to learn how the Upcase codebase works.
- You will pick up new ideas and patterns.
- You will end up with a list of things you don't understand. That's a great guide on what you need to focus on.
- You will see how other experts structure a project to make the code neat and clean.
- You will learn how to find answers to your own questions and how to read source code.

That's pretty good, right?

All professional developers and experts have these skills. It comes from practice.

## Why Pick a Well-Written Codebase?

Good question. You have to be exposed to good code if you want to advance your skills. Especially if you don't have this opportunity at work.

If your goal is to learn best practices, you have to read a well-written codebase.

[Thoughtbot](https://thoughtbot.com), the company behind [Upcase](https://thoughtbot.com/upcase), is well-known in the Ruby on Rails community for setting industry standards and following [good coding practices](https://thoughtbot.com/blog/sandi-metz-rules-for-developers).

## What if I get stuck?

When that happens, ask one of your colleagues for help.

If that doesn't work or you don't feel comfortable doing that, ask [Rails Forum](https://discuss.rubyonrails.org/) or [Reddit](https://www.reddit.com/r/rails/). 

You can also send me a DM on [Twitter](https://twitter.com/thdaraujo) and I'll be happy to help.

-------

Now that you've done this exercise, you will improve your Rails skills every time you practice.

It's just a matter of consistently exposing yourself to good code. Keep using the suggested questions as a guide, or come up with different ones. Pick a different class every day, and keep going.

You've already done the hardest part: getting started. Now keep practicing.

Let me know what you've learned by practicing this exercise. 

**Bookmark this post to keep the questions on hand. Share it with a friend who will find this exercise helpful.** ⭐

Happy learnings! 📓

---

## Do you want to become an expert developer?

I write about Ruby and Software Development on [hexdevs](https://www.hexdevs.com/) and on [Twitter](https://twitter.com/thdaraujo). Let's connect!