Skip to content

Latest commit

 

History

History
 
 

best-practices

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Best Practices

A guide for programming well.

General

Object-Oriented Design

  • Avoid global variables.
  • Avoid long parameter lists.
  • Limit collaborators of an object (entities an object depends on).
  • Limit an object's dependencies (entities that depend on an object).
  • Prefer composition over inheritance.
  • Prefer small methods. One line is best.
  • Prefer small objects with a single, well-defined responsibility.
  • Tell, don't ask.

Ruby

  • Avoid optional parameters. Does the method do too much?
  • Avoid monkey-patching.
  • Prefer classes to modules when designing functionality that is shared by multiple models.
  • Prefer private when indicating scope. Use protected only with comparison methods like def ==(other), def <(other), and def >(other).

Ruby Gems

  • Declare dependencies in the <PROJECT_NAME>.gemspec file.
  • Reference the gemspec in the Gemfile.
  • Use Appraisal to test the gem against multiple versions of gem dependencies (such as Rails in a Rails engine).
  • Use Bundler to manage the gem's dependencies.
  • Use Travis CI for Continuous Integration, indicators showing whether GitHub pull requests can be merged, and to test against multiple Ruby versions.

Rails

  • Avoid bypassing validations with methods like save(validate: false), update_attribute, and toggle.
  • Avoid naming methods after database columns in the same class.
  • Don't change a migration after it has been merged into master if the desired change can be solved with another migration.
  • Don't reference a model class directly from a view.
  • Don't use SQL or SQL fragments (where('inviter_id IS NOT NULL')) outside of models.
  • If there are default values, set them in migrations.
  • Keep db/schema.rb or db/development_structure.sql under version control.
  • Use the .ruby-version file convention to specify the Ruby version and patch level for a project.
  • Use SQL, not ActiveRecord models, in migrations.
  • Use _url suffixes for named routes in mailer views and redirects. Use _path suffixes for named routes everywhere else.
  • Validate the associated belongs_to object (user), not the database column (user_id).

Testing

  • Avoid its, let, let!, specify, before, and subject in RSpec.
  • Avoid using instance variables in tests.
  • Use an it example or test method for each execution path through the method.
  • Use stubs and spies (not mocks) in isolated tests.
  • Use a single level of abstraction within scenarios.

Bundler

  • Specify the Ruby version to be used on the project in the Gemfile.
  • Use a pessimistic version in the Gemfile for gems that follow semantic versioning, such as rspec, factory_girl, and capybara.
  • Use a versionless Gemfile declarations for gems that are safe to update often, such as pg, thin, and debugger.
  • Use an exact version in the Gemfile for fragile gems, such as Rails.

Postgres

Background Jobs

  • Store IDs, not ActiveRecord objects for cleaner serialization, then re-find the ActiveRecord object in the perform method.

Email

  • Use SendGrid or Amazon SES to deliver email in staging and production environments.
  • Use a tool like mail_view to look at each created or updated mailer view before merging.

Testing

  • Disable real HTTP requests to external services with WebMock.disable_net_connect!.
  • Test background jobs with a Delayed::Job matcher.
  • Use a Fake to stub requests to external services.
  • Use integration tests to execute the entire app.
  • Use non-SUT methods in expectations when possible.

JavaScript

  • Use CoffeeScript.

HTML

  • Don't use a reset button for forms.
  • Prefer cancel links to cancel buttons.

CSS

  • Use Sass.

Browsers

  • Don't support clients without Javascript.
  • Don't support IE6 or IE7.

Objective-C

  • Prefer categories on Foundation classes to helper methods.
  • Prefer string constants to literals when providing keys or key paths to methods.