DEV Community

Cover image for Learn What it Takes to Be a Solid Rails Developer!
Shireen Bano A
Shireen Bano A

Posted on

Learn What it Takes to Be a Solid Rails Developer!

Unlock the full potential of Ruby on Rails and build web applications that will leave your peers in awe. But before you can do that, you need to master these essential concepts that are the building blocks of any successful Rails project. Let's dive in and explore them one by one

1. Familiarize yourself with Active record association and its best practices
Image description

I regret not finding this article when I was struggling to learn active record associations🥲(It's so good). This article will not only explain the various types of associations, but also provide a clear overview of how the associations interact with the database through the use of foreign key and primary key constructs.

2. Design patterns are the MACHINE GUNS🚀 of your codebase

The Best Design Patterns in Ruby On Rails

Learn how to implement 10 basic design patterns in Ruby on Rails and know how it helps refactor MVC components in Rails.

favicon bacancytechnology.com

Clean code should never be underestimated, as it can save significant time for both yourself and other developers when debugging. Design patterns are the best practices followed by industry leaders. They not only make your code more readable (eg: Query objects) but also reduces code complexity(eg: Form objects) by separating the concerns (eg: Service object). Therefore, easy to maintain and review.

3. Only Unit testing can save you from Production crashes🔥

Back to Basics: Writing Unit Tests First

Step-by-step instructions for learning Test-Driven Development (TDD) in Ruby. There’s nothing to fear! It’s fun.

favicon thoughtbot.com

Testing your code with every single input under the sun after every tiny tweak is like a recurring nightmare, amirite? 😴. That's where unit testing comes into rescue. It's a smart way writing code, where you write tests first before you even write the code. Basically, you make changes to your code, run the tests and boom👊🏻!, all the failed tests show up on your screen. Check out the above article for more details.

Code Coverage Tutorial: Branch, Statement, Decision, FSM

Code coverage is a measure which describes the degree of which the source code of the program has been tested. Following are major code coverage methods Statement Coverage, Condition Coverage, Branch Coverage, Toggle Coverage, FSM Coverage

favicon guru99.com

Just writing test for the concerned parts of the code doesn't make your code production crash proof. Writing quality test that can provide maximum code coverage is what makes your code crash proof (99.9% times😅). The article above breaks down all the ways to make sure you're testing every nook and cranny of your code before it goes live. .

4. Code optimization is our Ultimate Goal💪🏻

There's a lot of things that can slow down our code execution, like using too many resources at the same time, bad design choices, and unoptimized code, but the one thing we can control is our code. So, let's make sure it's running as smooth as butter.
The following are the most common causes of poor application performance.

Slow view rendering :

Run any view of your rails application and check the logs in the terminal.
Image description

You will be able to find a line that will let you know how long it took for your view to load, how long it took for ActiveRecord to fetch data from the database, and how much time was spent creating new objects. Slow view rendering can be caused by factors such as an excessive amount of logic in the view, a large number of partials, and unoptimized images and other assets.

If your view is taking forever to load and you can't figure out why, it might be a good idea to give the above article a quick read.

N+1 queries :

A common performance issue in Rails is the N+1 query problem, where a separate database query is made for each item in a collection. This can cause a lot of unnecessary database queries and slow down the view rendering.
To understand what n+1 queries are in rails, you can refer the below article.

Large number of partials :

When you've got too many little chunks of views (partials) hanging around, it can slow down how fast your page loads. It's best to keep that number down, But If your existing application has too many partials which are not optimizable then its time to move to view_components. View components are a way to group common view logic together, which can simplify your code and make your application perform better.

If you're interested in experimenting with it, the article below can serve as a good starting point.

Lack of caching :

Rails, by default, does not include caching functionality. However, it does provide several options for caching through the use of third-party gems, such as the dalli gem for memcached caching and the redis-rails gem for Redis caching. Additionally, Rails also provides caching options through built-in methods such as cache and expires_in.

There are several gems available that can help identify and solve caching issues in Rails code. These gems can analyze your application and provide recommendations for optimizing caching strategies, such as identifying areas where caching can be added or improved. Some examples of such gems include rails_best_practices, bullet, and rack-mini-profiler.

If you're feeling like a mad scientist ready to conduct some experiments with these gems, the below post can be your lab coat and goggles😜.

All in all, Clean code practices for Ruby on Rails developer are a combination of using OOP principles, adhering to Rails conventions, automated testing, and keeping the codebase organized and well-commented.

Top comments (0)