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)

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:

defis_legalself.age>=21end

current_user.is_legal

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

defis_legal?self.age>=21end

current_user.is_legal?

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]

printa.reverse

printa# 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]

printa.reverse!

printa# 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”end

or alternatively

ifnotcurrent_user.is_legal?puts“I can’t buy alcohol”end

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

unlesscurrent_user.is_legal?puts“I can’t buy alcohol”end

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.”end

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

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

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:

ifcurrent_user.is_legal?puts“I can buy alcohol”end

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

puts“I can buy alcohol”ifcurrent_user.is_legal?

This applies to things like unless as well:

puts“I can’t buy alcohol”unlesscurrent_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 :)

If you found this post useful, sign up for my personal mailing list below. Every now and then, I share coding articles that I've found or written that you might find to be useful or interesting.