In a long-forgotten kingdom, there lived three people called Jackie and July along with their uncle.
Uncle being an old man, he always tries to teach Julie the art of living life. But Julie, on the other hand, thought that her uncle was an old man and he doesn't know anything about life.
But Julie respects Jackie. At least in our fantasy story.😝 Jackie always corrected whenever she does something that's not cool. This is what our 'lint' does when he was introduced into the ruby code.
Just like our uncle, the rubocop does his job in correcting the people whenever he finds a non-standard code.
Welcome to the fourth episode of the series: Are You Doing This❓. In case you haven't read the previous articles, I highly recommend you to have a look at those. Sure you'll love it 💙. Happy coding 👨🏻💻.
Previous week link :
Are You Doing This❓- Top 5 lint issues we fail to notice.
In today's series, I'll walk you through the most frequently occurring 5 lint issues and their explanation followed by the link, at the end, where you can learn more about the coding standards.
In the previous episode, I've mentioned a code snippet which had lint issues. I would like to recollect the same snippet here so that it would be easier for you to follow up.
1) Redundant 'return' detected
This is the most frequently occurring lint issue. which people often fail to address.
# Example method def first_omnitrix return true end
at the end of the function in ruby, you aren't expected to use the return keyword. You can use it, but it is not a good practice.
The snippet should look like this,
# Example method def first_omnitrix true end
To learn more about this, you can find a separate article that I have written over here. (https://dev.to/chintukarthi/redundant-return-detected---ruby-programming-gac)
2) Use snake_case for method names.
While defining a method, use snake_case for the method name. Snake case is nothing but all small case with a delimiter of '_'.
# Example snake_case method def snake_case true end
To learn more about the use of cases, you can find a separate article that I have written over here. (https://dev.to/chintukarthi/aydt-03-are-you-doing-this-programming-and-cases-1enj)
3) Don't use parentheses around the condition of an ' if '
If you are from c or c++ background and started learning and using ruby, sure you might have come across this,
# assume the value of a and b are already initialized and b is less than a. def first_omnitrix if (a>b) a = 5 end end
use of parentheses around the if condition is not recommended in ruby. So remove it. To avoid this lint, modify the code as shown in the below snippet.
def first_omnitrix if a>b a = 5 end end
4) Use a guard clause instead of wrapping the code inside a conditional expression.
Hmmm 🤔 sounds odd right?
Nothing odd, It's just simple. Look at the below code snippet,
# assume the value of a and b are already initialized and b is less than a. def first_omnitrix if a>b a = 5 end end
Here we used an ' if ' condition to set the value of 'a', to check if it is greater than b.
It might feel correct and is also valid. But the thing is, it's not a correct way to write this piece of code.
You might want to rewrite that to something like this,
# assume the value of a and b are already initialized, and b is less than a. def first_omnitrix a = 5 if a>b end
5) Trailing whitespace detected.
I can't wait to share this 😇. This one is my personal favorite Because I often make this 😂.
While writing code for some functionalities, we use enter/return to navigate to the next line. But what we don't realize is, it will introduce whitespace around the new line something like this.
def whitespace if a>b if a < c true end end.. (some extra code) end
what happens here is, you navigated to a new line to write some extra code over there and then deleted the code. But you might forget to delete the two extra space(denoted as '.' dots in the snippet) which will create this ' trailing white space ' lint issue.
After solving the lint, the above method will look like this,
def whitespace if a>b if a < c true end end end
to avoid this issue, just remove the extra spacing in the code which isn't necessary.
So, these are the Top 5 lint issues, that I wish to share with you. If you want to improve your coding skills, you can go ahead over here (https://rubocop.readthedocs.io/) to learn more things about ruby lint.
So that's a wrap for this week. See you people around 🧙🏻♂️.
Cover image courtesy: https://www.codeninja.com.sg/