VISIT OUR NEW CAMPUS IN WYNWOOD! CLICK HERE TO SCHEDULE A TOUR

Wyncode Academy Blog Posts

Read about South Florida’s first development bootcamp

Most Common Rails Programming Mistake and How to Fix Them

Most Common Rails Programming Mistake and How to Fix Them

Written by wyncode on 19th December 2014, 10:00 AM

Ruby on Rails is one of the most flexible languages, which makes it perfect for the development of web applications and extensions of existing applications.

The problem is that it’s all too easy to make simple but critical mistakes that can drastically diminish everything from the readability of your program to its performance.

Below is a quick list of the most common Ruby on Rails mistakes that even professionals make, and how you can fix them in your own applications.

1. Using Ranges Instead of Comparisons

Most programming languages utilize comparisons due to their boolean nature. Ruby on Rails is one of the few environments where comparisons are anything but the fastest way to create your applications.

Use something like the following when you have multiple ranges:

number = 9489
case number
when 10..99: “Tens”
when 100..999: “Hundreds”
when 1000..9999: “Thousands”
end

2. Data Without Schema Constraints

One of the features that Ruby on Rails introduces that most programmers find different is the ability to constraint data in various ways using schema information.

For example, a common mistake is to create a structure that looks like this:

create_Table “businesses” do |t|
t.integer “id”
t.string “name”
t.string “phone”
t.string “address”
t.string “website”
t.string “email”
end

The mistake here is that schema constraints are not being used. Properly constrained data models are a necessity to ensure that your application works with fewer problems with less of a need for validation upon submission.

As it stands, everything in the above example is considered optional.

To correct this, use constraints like “:null => false” to ensure that data isn’t empty or “:limit => 60” to limit the number of characters that can be used.

create_Table “businesses” do |t|
t.integer “id”, :null => false
t.string “name”, :null => false
t.string “phone”, :limit => 11
t.string “address”
t.string “website”
t.string “email”
end

3. Complicated Logic

Ruby on Rails has a number of features designed to make complex functions into very compact ones.

For example, the below function determines if a given number is even.

def even?(x)
x % 2 == 1 ? false : true
end

The problem with this function is that it’s complicated compared to the simplest solution available. This makes it harder for other coders and, given enough time and a large enough project, even yourself to forget the purpose of some of your code.

To fix this, take advantage of some of the logic Ruby has to offer:

def even?(x)
x ^ 2 != 0
end

Fixing Other Ruby on Rails Problems

While only the three most common Ruby on Rails mistakes have been listed here, a majority of the other common mistakes relate to these problems in some way.

By becoming more familiar with the logic Ruby offers, the innate functions available for comparison, and the principles that keeps Ruby on Rails beautiful and strong, you will see a tremendous decrease in the number of mistakes your Ruby on Rails programs have.

Come to an Upcoming Event

Fill out my online form.