Skip to content

Coding Conventions

Pascal Reinhardt edited this page Jun 12, 2014 · 8 revisions

Naming Conventions

  • no abbreviations in names
  • camelCase for Classes and Modules, snake_case for variables and methods, SKREAMING_SNAKE_CASE for constants
  • hyphen-case for css classes and ids
  • use ! suffix for dangerous methods (modify self, exception)
  • use ? suffix for boolean returning methods

Code Style

  • use implicit return only for single liners
  • avoid superfluous parentheses
  • use parentheses when defining methods with parameters
  • don’t use unless with else
  • use unless instead of if not or if !
  • use || and && for boolean expressions
  • use ‘and’ or ‘or’ to connect statements
  • use single line statements if possible
  • use ||= and a = b or c
  • use {} for single line blocks but never for multiline blocks
  • use ‘ for strings in ruby or js, “ for html
  • def self.method for class methods
  • use 2 spaces for indentation for ruby code and html, 4 for javascript and css
  • use symbol: instead of :symbol =>
  • leave 1 space between operators
  • leave 1 new line before classes and methods
  • no useless comments

Best Practices

  • heavy models, light controllers
  • :only before :except, but whatever is shorter
  • limit number of instance variables shared between controller & view
  • order of controller actions: index, show, new, create, edit, update, destroy, custom actions
  • order of model contents: constants, attributes, associations, nested attributes, delegates, validations, named scopes, callbacks, public methods, private methods
  • annotate models after migrations
Clone this wiki locally