A guide for programming well.
- Don't duplicate the functionality of a built-in library.
- Don't swallow exceptions or "fail silently."
- Don't write code that guesses at future functionality.
- Exceptions should be exceptional.
- Keep the code simple.
- 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.
- 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.
privatewhen indicating scope. Use
protectedonly with comparison methods like
def <(other), and
- Declare dependencies in the
- Reference the
- 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.
- Avoid bypassing validations with methods like
- Don't change a migration after it has been merged into master if the desired change can be solved with another migration.
add_index :table, :column, unique: trueto enforce uniqueness constraints and avoid race conditions.
- Validate the associated
user), not the database column (
- Specify the Ruby
version to be used on
the project in the
- Use a pessimistic
Gemfilefor gems that follow semantic versioning, such as
- Use a
Gemfiledeclarations for gems that are safe to update often, such as pg, thin, and debugger.
- Use an exact
Gemfilefor fragile gems, such as Rails.
--binstubs=bin/stubsto avoid typing
- Avoid multicolumn indexes in Postgres. It combines multiple indexes efficiently. Optimize later with a compound index if needed.
- Consider a partial index for queries on booleans.
- Constrain most columns as
- Index column(s) used as foreign keys.
- Index column(s) often used in
- Notify Airbrake about errors.
- Store IDs, not
ActiveRecordobjects for cleaner serialization, then re-find the
ActiveRecordobject in the
- 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.
- Disable real HTTP requests to external services with
- Test background jobs with a
- 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.
- Don't support IE6.
- Prefer categories on
Foundationclasses to helper methods.
- Prefer string constants to literals when providing keys or key paths to methods.