- Misuse predicates
- Forget to explicitly use “present?”, “blank?”, and “nil?”
- Don’t use memorization
- Forget that first come symbols, then single quotes, and then double quotes
- Use some random numbers and lines
- Often choose short or confusing words to name constants
- Excessively use postconditions
- Forget to remove unused parameters and variables, and other unused code
- Don’t use “public_send” Invokes the method identified by symbol, passing it any arguments specified. Unlike send, #public_send calls public methods only. When the method is identified by a string, the string is converted to a symbol.
- Write long and illegible conditions
- Think less code is always better
- Use obsolete syntax
- Excessively use “when” and “if”
- Don’t use rubocop and other tools for code analysis Rubocop notice almost all the errors described here
- 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 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
############ | |
## 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 |
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
############ | |
## WRONG ## | |
############ | |
if course | |
render '...' | |
end | |
raise NotAuthorized unless auth_token | |
############## | |
## RIGHT ## | |
############## | |
if course.present? | |
render '...' | |
end | |
raise NotAuthorized unless auth_token.present? |
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
############ | |
## 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 |
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
############ | |
## WRONG ## | |
############ | |
render ‘new’ | |
social_network = “facebook” | |
message = “Can’t do this” | |
############## | |
## RIGHT ## | |
############## | |
render :new | |
social_network = ‘facebook’ | |
message = “Can’t do this” |
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
############ | |
## WRONG ## | |
############ | |
@courses = Course.recent.page(params[:page]).per(3) | |
if team.players.count > 13 | |
# do something | |
end | |
############## | |
## RIGHT ## | |
############## | |
@courses = Course.recent.page(params[:page]).per(params[:per_page] || PER_PAGE) | |
if team.players.count > MAXIMUM_NUMBER_OF_PLAYERS | |
# do something | |
end |
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
############ | |
## WRONG ## | |
############ | |
u = User.last | |
CUS_NUM = 100 | |
############## | |
## RIGHT ## | |
############## | |
last_user = User.last | |
CUSTOMERS_NUMBER = 100 | |
# But: | |
form_for @article do |f| | |
= f.text_field :title | |
create_table :articles do |t| | |
t.integer :course_id | |
view raw |
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
############ | |
## WRONG ## | |
############ | |
def some_method | |
SomeLongClassName.new(params).perform_action if something_true? | |
# a lot of another code | |
end | |
def create | |
redirect_to(root_path) if @resource.save | |
end | |
############## | |
## RIGHT ## | |
############## | |
def some_method | |
if something_true? | |
SomeLongClassName.new(params).perform_action | |
end | |
# a lot of another code | |
end | |
def create | |
if @resource.save | |
redirect_to root_path | |
end | |
end |
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
############ | |
## WRONG ## | |
############ | |
def find_user( first_name, last_name, date_of birth ) | |
User.find_by(first_name: first_name) | |
end | |
############## | |
## RIGHT ## | |
############## | |
def find_user(first_name) | |
User.find_by(first_name: first_name) | |
end | |
view raw |
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
############ | |
## 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 |
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
############ | |
## WRONG ## | |
############ | |
def some_filter | |
return unless user_signed_in? || !(current_user.customer? || current_user.admin?) | |
redirect_to root_path | |
end | |
############## | |
## RIGHT ## | |
############## | |
def some_filter | |
return unless user_signed_in? | |
return if current_user.customer? || current_user.admin? | |
redirect_to root_path | |
end |
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
############ | |
## WRONG ## | |
############ | |
a = { :bad_code => ‘Some smell’ } | |
############## | |
## RIGHT ## | |
############## | |
a = { good_code: ‘No smells’ } |
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
############ | |
## 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 |
https://jetruby.com/
Comments
Post a Comment