5 Tips for Beginners to Write Better Ruby Code Instantly

February 20, 2016   

Now that you've learned the basic syntax and gained a basic understanding of ruby works, you want to learn how to write better and more concise code. Here are 5 quick tips picked up from a fantastic book called Eloquent Ruby that you can start using right now to make your code more readable.

Don't write comments for the wrong reasons

Adding comments, especially in a more complex app can be useful. You have to make sure that you provide comments so that other people can read and understand your code. You also have to make sure that you can understand the code yourself after a while.

On the other hand, you don't want to get in the mindset of, "Oh, this code is pretty messy but I'm going to add comments so it'll be fine." Adding comments to make badly written code somewhat comprehensible is an extremely bad reason for adding comments. In general, your ruby code should be clear enough so that it explains itself. Hence the quote from the book, "Good code is like a good joke: it needs no explanation." (pg. 8 - Eloquent Ruby)

Ternary Operators

Ever write code like this?

if current_user.subscribed?

This makes perfect sense, except we can make it much shorter with ternary operators. Ternary operators work like such:

if_this_is_a_true_value ? then_the_result_is_this : else_it_is_this

So in this example, we can re-write the code like this:

current_user.subscribed? ? render_already_subscribed_message : current_user.subscribe!

Method Name Conventions: Using ? and !

When you are using ruby, you may have noticed that a lot of the built in methods use ? and !. For example .nil? or .flatten!. ? and ! aren't special symbols in method names, but rather just naming conventions in the ruby community.

?s are used when the method returns a true or false boolean. So for example instead of this:

def is_legal
  self.age >= 21


You want to write add a ? to the method like this:

def is_legal?
  self.age >= 21


You can see here that current_user.is_legal? makes a little more sense than current_user.is_legal, since we are essentially asking a yes or no question.

Now let's take a look at the ! operator in methods. The ! operator when used in a method infers that the method is potentially dangerous and could change the state of the variable that the method is called upon. Here's an example:

a = [1, 2, 3, 4]

print a.reverse

print a # returns [1, 2, 3, 4]

What we see here is that we expect .reverse to actually modify the array a. However, we see that it doesn't modify the actual array, but it just returns the reversed array. Now what if we change the reverse method to reverse!?

a = [1, 2, 3, 4]

print a.reverse!

print a # returns [4, 3, 2, 1]

Now we see that reverse! has actually modified the actual array. It's important to know the differences between methods with and without the bang operator.

Using unless and until

When you want to write logic when something is not true, you might be tempted to write code like this:

if !current_user.is_legal?
  puts “I can’t buy alcohol”

or alternatively

if not current_user.is_legal?
  puts “I can’t buy alcohol”

Instead of combining if and !, it is often times more intuitive to use unless.

unless current_user.is_legal?
  puts “I can’t buy alcohol”

This sounds closer to English and is far more readable. Instead of "If current user is not legal, do this", it takes a little less mental energy if I were to say "Unless current user is legal, do this".

Same thing with while loops. Instead of writing this kind of code:

while !fruit.is_ripe?
  puts “This fruit isn’t ripe yet.”

You could make the code much easier to mentally process by rewriting it like this:

until fruit.is_ripe?
  puts “This fruit isn’t ripe yet.”

It takes less mental energy to read and understand code if we use unless and until in these kinds of situations.

One Line Statements

Let's say we have code written like this:

if current_user.is_legal?
  puts “I can buy alcohol”

Instead of using multiple lines, we can rewrite this code in one line like this:

puts “I can buy alcohol” if current_user.is_legal?

This applies to things like unless as well:

puts “I can’t buy alcohol” unless current_user.is_legal?

When writing these one-liners, make sure that the line isn't too long. The point here is to make the code more readable, so you don't want to defeat the purpose by cramming a bunch of code into one line just because you can.

Just implementing these simple techniques will make you a better ruby programmer. Ruby offers many elegant solutions like these and learning them are really worth the time.

Happy coding :)