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.
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)
Ever write code like this?
This makes perfect sense, except we can make it much shorter with ternary operators. Ternary operators work like such:
So in this example, we can re-write the code like this:
When you are using ruby, you may have noticed that a lot of the built in methods use
!. For example
! aren't special symbols in method names, but rather just naming conventions in the ruby community.
?s are used when the method returns a
false boolean. So for example instead of this:
You want to write add a
? to the method like this:
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:
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
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.
When you want to write logic when something is not true, you might be tempted to write code like this:
Instead of combining
!, it is often times more intuitive to use
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:
You could make the code much easier to mentally process by rewriting it like this:
It takes less mental energy to read and understand code if we use
until in these kinds of situations.
Let's say we have code written like this:
Instead of using multiple lines, we can rewrite this code in one line like this:
This applies to things like
unless as well:
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 :)