Skip to content
Kevin edited this page Jan 26, 2017 · 6 revisions

Ruby

  • Avoid inline conditionals:
return 5 if something? 
  • Avoid multiple variable assignments per line
one, two = 1, 2
  • Avoid ternary operators (boolean ? true : false)
  • Don't use self explicitly anywhere except class methods (def self.method) and assignments (self.attribute =).
  • Prefer select over find_all.
  • Prefer map over collect.
  • Prefer reduce over inject.
  • Prefer single quotes for strings.
  • Prefer && and || over and and or.
  • Prefer ! over not.
  • Prefer &:method_name to { |item| item.method_name } for simple method calls.
  • Don't use unless. Use if.
  • Use _ for unused block parameters.
  • Prefix unused variables or parameters with underscore (_).
  • Always use do..end.
  • Use ? suffix for predicate methods.
  • Use CamelCase for classes and modules, snake_case for variables and methods, SCREAMING_SNAKE_CASE for constants.
  • Use def self.method or class << self for static methods but they should generally be avoided.
  • Use def with parentheses when there are arguments. Same for function calls.
  • Use each, not for, for iteration.
  • Use a trailing comma after each item in a multi-line list, including the last item.
[
  'a',
  'b',
]
  • Prefer private over protected for non-public attr_readers, attr_writers, and attr_accessors.
  • Order class methods above instance methods.
  • Prefer method invocation over instance variables.
  • Avoid at all cost to make a method longer than 10LOC.

Rails

  • Don't use before_action especially multiple actions, use explicit functions.
  • For configuration, create a yaml file in config/ and parse it in an initializer.
  • Place logic in services/. Please use Interactor.
  • Avoid where("x > ?", num_var) placeholders, use where("x > :num", { num: num_var }). Especially for larger numbers of parameters.

CSS

Clone this wiki locally