It’s been a while since we’ve rolled out our previous article about the most common Rails mistakes that beginner developers usually make. In this regard, we aren’t going to bother you with too much of introduction and get straight to the point. This article will be dedicated to the standard approaches to Ruby development which are also called “the Ruby way”. As usual, the most critical mistakes are highlighted with RED. So, let’s find out what exactly newbies do wrong/forget to do or don’t do at all.
They misuse predicates
A predicate is a method that answers a specific question and always returns either “yes” or “no”. It’s not supposed to affect any other piece of code or perform any additional actions. According to the Ruby on Rails style guides, predicates are followed by a question mark at the end of the method name.
They forget to explicitly use “present?”, “blank?”, and “nil?”
Using “present?”, “blank?”, “nil?” etc. for validation is considered a good practice even you’re totally sure the code is going to work anyway. Keep that in mind.
They don’t use memoization
An instance variable is considered “nil” if it’s not declared. With that in mind, you can leverage memoization. Using the first option in the code sample below will perform four requests: two for the user selection, one for the articles selection, and one for the courses selection. However, if you look at the second option, you will see that the number of requests is now down to three: one for the user selection, one for the articles selection, and one for the courses selection.
They forget that first come symbols, then single quotes, and then double quotes
Ruby on Rails guides insist on using minimum syntax. However, following this notion is not a must. If there should be single quotes – we recommend using double quotes, not screening.
They use some random numbers and lines
One of the rules you need to remember at all times is that there shouldn’t be any random numbers or lines in your code.
They often choose short or confusing words to name constants
Using short names for constants leads to poor readability and may even cause conflicts. Moreover, the code eventually becomes completely illegible. The only exception may be when creating migration in a table or generating a form in a view.
They excessively use postconditions
Postconditions allow you to make your code even more laconic and readable. However, in some cases, it’s better to avoid using them. For example:
- when you know the postcondition can be lost in the current code line;
- when it’s the main condition and, thus, has to be as visible as possible.
They forget to remove unused parameters and variables, and other unused code
It’s extremely important remove unused code. Why? Because doing that becomes much harder over time especially for other developers.
They don’t use “public_send”
Nearly 90% of all the examples on the Internet refer to the “send” method when invoking a method dynamically. But there is also a similar method – “public_send”– which is even more preferable in some cases.
They write long and illegible conditions
Remember: long conditions – especially the ones starting with “unless” – are very close to unreadable. You can give them proper names by placing them into methods.
They think less code is always better
Ruby code should be easy to read and look into. That’s why you should always write your code with the readability aspect in mind. For example, you can outline an important condition by breaking it down to few more lines.
They use obsolete syntax
Despite there are still quite many guides in which you can find old syntax, we recommend sticking to its new version. Firstly, it’s shorter and easier to read. Secondly, it’s better to adhere to a single code style.
They excessively use “when” and “if”
They don’t use rubocop and other tools for code analysis
Rubocop is a great tool that allows you to search for syntactical and other kinds of mistakes.
They don’t fully understand the point about commenting
Ruby is considered a very high-level language that doesn’t need commenting. Instead of writing another comment to your code, try making it more readable.
This was the third part in the series of articles about the most common Rails mistakes that many beginners face on a regular basis. Hope you’ve enjoyed our tips and pieces of advice. If so, don’t forget to tell your friends about the article by clicking these fancy little buttons below! And if you have any development-related questions, you can always ask our experts in the comment section. Stay tuned!
P.S. You can find the Russian version of the article here.