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.
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:
- Set a timer for 10 minutes ⏰
- Open this link in another tab: practice.rb from Upcase by Thoughtbot
- Ask yourself: What’s going on here?
- Come back here when the time is up! ⌛
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
trailsbeing passed down to the
Did you notice these methods
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.
Frequently Asked Questions
Here are the answers to some questions you might have about this exercise.
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.
What if I get stuck?
When that happens, ask one of your colleagues for help.
You can also send me a DM on Twitter and I’ll be happy to help.
Can you give me other examples of questions to ask?
I’m glad you asked!
Some other interesting questions you may ask:
- What does this class/object afford? What is the potential action enabled by this class/object?
- What are the dependencies of this class?
- How would I feel if I had to use this class (happy, angry, confused)?
- Why is this code here and not somewhere else?
- If I call this method, how much do I need to know about its implementation?
Even when you think you know something well, challenge your knowledge about it by asking interesting questions.
It can be hard to separate understanding from memory or just familiarity.
Sometimes you think you understand a concept because it’s familiar to you. You remember it vividly. But you’re not able to explain the idea to anyone, even to yourself. And that’s not fun!
Is there a list of projects I can use for this exercise?
Sure thing! When you’re finished with Upcase, you can pick a different project to study.
But BEWARE: Analysis paralysis is a real thing, so be careful!
It’s easy to procrastinate instead of doing real work and practicing.
Don’t waste too much time choosing a project. Pick one at a time and keep practicing.
I’ve compiled a list of projects and other resources to help you.
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! 📓
PS: I recently gave a talk about this exercise, you can find the slides here 🍝