Last active
October 10, 2017 14:41
-
-
Save anoobbava/0f9be86a0bbd667f412f3c2efcc7287f to your computer and use it in GitHub Desktop.
Ruby Mistakes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dont misuse predicate methods: | |
------- | |
predicate methods used to return either true or false. they ends with "?" | |
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. | |
############ | |
## WRONG ## | |
############ | |
def author? | |
if current_user.id == course.user.id | |
current_user.add_role(‘author’) | |
end | |
end | |
def has_any_courses? | |
current_user.courses.count | |
end | |
############## | |
## RIGHT ## | |
############## | |
current_user.add_role(‘author’) if author? | |
def author? | |
current_user.id == course.user.id | |
end | |
def has_any_courses? | |
current_user.courses.size > 0 | |
end | |
use present nil , blank meaningfully: | |
---------------------- | |
############ | |
## WRONG ## | |
############ | |
if course | |
render '...' | |
end | |
raise NotAuthorized unless auth_token | |
############## | |
## RIGHT ## | |
############## | |
if course.present? | |
render '...' | |
end | |
raise NotAuthorized unless auth_token.present? | |
Use "||" when required: | |
----------------------------- | |
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. | |
############ | |
## WRONG ## | |
############ | |
def user | |
User.find(params[:id}) | |
end | |
user.articles | |
user.courses | |
############## | |
## RIGHT ## | |
############## | |
def user | |
@user ||= User.find(params[:id}) | |
end | |
user.articles | |
user.courses | |
Use single quotes when necessory: | |
------------------------ | |
############ | |
## WRONG ## | |
############ | |
render ‘new’ | |
social_network = “facebook” | |
message = “Can’t do this” | |
############## | |
## RIGHT ## | |
############## | |
render :new | |
social_network = ‘facebook’ | |
message = “Can’t do this” | |
Use of constants: | |
--------- | |
when checking in a common value , then use CONSTANTS | |
like | |
PI = 3.14 | |
the value of the PI is always 3.14. so dont need to add the value as 3.14 | |
always use constants with meaning full words. | |
always use multiple conditions as methods: | |
-------------- | |
############ | |
## WRONG ## | |
############ | |
if course.courses_users.where(ban: true).exists?(user_id: id) | |
if course.user_id == id | |
if #something course.part.exists?(self) | |
end | |
end | |
end | |
############## | |
## RIGHT ## | |
############## | |
if part_in?(course) && banned?(course) && author?(course) | |
# do something | |
end | |
def part_in?(course) | |
course.part.exists?(self) | |
end | |
def banned?(course) | |
course.courses_users.where(ban: true).exists?(user_id: id) | |
end | |
def author?(course) | |
course.user_id == id | |
end | |
limit the use of case and if | |
----------------------------- | |
############ | |
## WRONG ## | |
############ | |
def somethod(name) | |
case name | |
when ‘a’ | |
‘A’ | |
when ‘b’ | |
‘B’ | |
when '…' | |
'...' | |
end | |
end | |
############## | |
## RIGHT ## | |
############## | |
CASES = { | |
‘a’ => ‘A’, | |
‘b’ => ‘B’, | |
} | |
def somethod(name) | |
CASES[name] | |
end | |
# or you could go with this option: | |
def somethod(name) | |
send(:”process_#{name}”) | |
end | |
def process_a | |
‘A’ | |
end | |
def process_b | |
‘B’ | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment