Common Ruby mistakes in Rails





  1. Misuse predicates
  2. ############
    ## 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
  3. Forget to explicitly use “present?”, “blank?”, and “nil?”
  4. ############
    ## WRONG ##
    ############
    if course
    render '...'
    end
    raise NotAuthorized unless auth_token
    ##############
    ## RIGHT ##
    ##############
    if course.present?
    render '...'
    end
    raise NotAuthorized unless auth_token.present?
  5. Don’t use memorization
  6. ############
    ## 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
  7. Forget that first come symbols, then single quotes, and then double quotes
  8. ############
    ## WRONG ##
    ############
    render ‘new’
    social_network = “facebook”
    message = “Can’t do this”
    ##############
    ## RIGHT ##
    ##############
    render :new
    social_network = ‘facebook’
    message = “Can’t do this”
  9. Use some random numbers and lines
  10. ############
    ## 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
  11. Often choose short or confusing words to name constants
  12. ############
    ## 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
  13. Excessively use postconditions
  14. ############
    ## 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
  15. Forget to remove unused parameters and variables, and other unused code
  16. ############
    ## 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
  17. Don’t use “public_send”
  18. 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.
  19. Write long and illegible conditions
  20. ############
    ## 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
  21. Think less code is always better
  22. ############
    ## 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
  23. Use obsolete syntax
  24. ############
    ## WRONG ##
    ############
    a = { :bad_code => ‘Some smell’ }
    ##############
    ## RIGHT ##
    ##############
    a = { good_code: ‘No smells’ }
  25. Excessively use “when” and “if”
  26. ############
    ## 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
  27. Don’t use rubocop and other tools for code analysis
  28. Rubocop notice almost all the errors described here
  29. Don’t fully understand the point about commenting
  30. 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.

https://jetruby.com/

Comments