23
Learn Ruby on Rails 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?
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.
Instead of reading another book or watching youtube videos about pasta making during your lunch break, do this exercise instead:
- Set a timer for 10 minutes.
- Open this link in another tab: practice.rb from Upcase by Thoughtbot
- Read the
app/services/practice.rb
class. - Ask yourself: What's going on here?
- 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 theinitialize
constructor? - Did you notice these methods
promoted_unstarted_trails
andunpromoted_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. đ
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.
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.
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.
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.
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 or Reddit.
You can also send me a DM on Twitter 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! đ
23