Ruby on Rails Guide

📑Ruby on Rails Guide to Perfect Coding Standards and Clean Code 🚀

In the fast-paced world of software development, writing clean, maintainable, and efficient code is crucial for the long-term success of any project. In Ruby on Rails (ROR), a framework loved for its simplicity and productivity, adhering to proper coding standards is key to avoiding technical debt and making collaboration easier. Let’s dive into some best practices and guidelines that will help you achieve perfect coding standards and clean code in your Ruby on Rails projects.

1. Follow the Rails Conventions 📏

2

Ruby on Rails is built around the principle of Convention over Configuration (CoC). Rails provides a set of conventions that allow you to write less code while maintaining clarity and structure. Here are some common conventions to follow:

  • File Structure: Keep the standard Rails folder structure intact. Rails expects specific files in certain locations (e.g., models in app/models, controllers in app/controllers). Stick to this structure to avoid confusion and compatibility issues.
  • Naming Conventions: Use snake_case for filenames, method names, and variable names. Classes and modules should use CamelCase. For example, UserProfile for class names, and user_profile.rb for filenames.

2. Keep Methods Short and Single-Responsibility 🧑‍💻

2

A hallmark of clean code is single-responsibility. Each method should perform only one task. Avoid the temptation to cram multiple functionalities into a single method.

  • Single Responsibility Principle: Every method should have one responsibility and do it well. Break down complex tasks into smaller methods.

  • SLAP (Single Level of Abstraction Principle): Keep methods operating at the same level of abstraction. Don’t mix high-level and low-level operations in the same method. It adds to confusion and makes the code harder to follow.

# Bad Practice
def process_order(order)
  validate_order(order)
  total_price = order.items.sum(&:price)
  order.update(total: total_price)
  send_invoice(order)
end

# Good Practice (Separation of Concerns)
def process_order(order)
  validate_order(order)
  update_total_price(order)
  send_invoice(order)
end

3. DRY (Don’t Repeat Yourself) Code ✂️

2

A key principle in Ruby on Rails development is to avoid repetition. DRY code leads to fewer bugs and a cleaner codebase. If you find yourself repeating code, it’s time to extract methods, use modules, or find Rails helpers that reduce duplication.

  • Use Rails’ built-in helpers such as before_action in controllers or concerns to reduce repetitive code.
  • Extract common logic into service objects or POROs (Plain Old Ruby Objects).
# Using before_action to DRY up repetitive code
class OrdersController < ApplicationController
  before_action :find_order, only: [:show, :edit, :update]

  def show
    # action-specific logic
  end

  def edit
    # action-specific logic
  end

  def update
    # action-specific logic
  end

  private

  def find_order
    @order = Order.find(params[:id])
  end
end

4. Code Readability Matters 👀

download

Your code is read far more often than it is written. Readable code ensures that others can quickly understand and work on your code. Use meaningful names and add inline comments where necessary.

  • Use Meaningful Names: Don’t be afraid of long variable names if they clearly express the purpose. For example, order_total is better than ot.
  • Comment Where Necessary: Avoid over-commenting. Comment only when the intent is not immediately clear from the code itself.
# Clear and self-explanatory
def calculate_discounted_price(price, discount_percentage)
  price - (price * discount_percentage / 100)
end

5. Adhere to SOLID Principles 🏗️

2

SOLID principles provide a robust foundation for writing maintainable and scalable code. In a Rails context, here’s how you can apply some of these principles:

  • Single Responsibility Principle: Keep controllers, models, and views focused on one task. Don’t overload them with too much logic.
  • Open/Closed Principle: Your classes should be open for extension but closed for modification. Use inheritance or modules for adding functionality instead of modifying existing code.
  • Dependency Inversion Principle: Inject dependencies into your classes rather than hardcoding them, which makes your code more flexible.
# Dependency Injection Example
class InvoiceSender
  def initialize(mailer = Mailer.new)
    @mailer = mailer
  end

  def send_invoice(order)
    @mailer.send(order)
  end
end

6. Test Your Code Thoroughly 🧪

2

Clean code isn’t just about formatting or structure—it’s also about reliability. Writing tests ensures that your code behaves as expected and helps you catch potential issues early.

  • Use RSpec for unit and integration tests in Rails.
  • Focus on testing edge cases and core functionalities.
  • Test small chunks of code (unit tests) and also how they integrate together (integration tests).
RSpec.describe Order, type: :model do
  describe '#total_price' do
    it 'calculates the total price of the order' do
      order = Order.new(items: [Item.new(price: 10), Item.new(price: 20)])
      expect(order.total_price).to eq(30)
    end
  end
end

7. Leverage Rails Linters and Formatters 🚨

2

To maintain clean code consistently across your team, use tools like Rubocop. It enforces a wide range of coding standards and helps you spot issues like unused variables, incorrect indentation, and other style offenses. Set up .rubocop.yml for custom rules tailored to your project.

  • RuboCop: Enforces Ruby and Rails style guides.
  • Prettier: Helps in formatting HTML, CSS, and JS files within your Rails app.
# Install RuboCop
gem install rubocop

# Run RuboCop to check code style
rubocop

8. Avoid Code Smells 🛑

1520092416441

Code smells are indicators of deeper problems in your code. Common code smells in Ruby on Rails include:

  • Fat Models: Avoid cramming too much logic into models. Extract logic into service objects or concerns.
  • God Objects: An object that knows too much or does too much. Keep classes and modules focused.
  • Too Many Conditionals: Replace complex conditionals with polymorphism or use Rails features like scopes or delegates.
# Refactoring a Fat Model
class Order < ApplicationRecord
  def total_price
    items.sum(&:price)
  end

  def send_invoice
    InvoiceSender.new.send(self)
  end

  # Bad: Too much responsibility in the model
end

# Refactor: Extract logic to service object
class InvoiceSender
  def send(order)
    # Logic to send invoice
  end
end

Conclusion 🎯

Writing clean, maintainable, and scalable code in Ruby on Rails is essential for long-term project success. By following Rails conventions, adopting SOLID principles, using tools like RuboCop, and avoiding common pitfalls like fat models and code smells, you can ensure that your codebase remains robust, readable, and a joy to work with.

Keep these principles in mind as you write your next Rails application, and you’ll be well on your way to producing high-quality, clean code! 🚀

Feel free to share your tips or ask questions in the comments below! Happy coding!

© Lakhveer Singh Rajput - Blogs. All Rights Reserved.