A guide for programming in style.
High level guidelines:
- Be consistent.
- Don't rewrite existing code to follow this guide.
- Don't violate the conventions without a good reason.
A note on the language:
- "Avoid" means don't do it unless you have good reason.
- "Don't" means there's never a good reason.
- "Prefer" indicates a better option and its alternative to watch out for.
- "Use" is a positive instruction.
- Have Heroku remotes named "staging" and "production."
- Use a Procfile to configure your services.
- Use the Cedar stack.
- Avoid including files in source control that are specific to your development machine or process.
- Delete local and remote feature branches after merging.
- Perform work in a feature branch.
- Prefix feature branch names with your initials.
- Rebase frequently to incorporate upstream changes.
- Use a pull request for code reviews.
- Write a good commit message.
- Avoid inline comments.
- Break long lines after 80 characters.
- Delete trailing whitespace.
- Don't include spaces after
(,[or before],). - Don't vertically align tokens on consecutive lines.
- If you break up an argument list, keep the parenthesis on its own line.
- If you break up a hash, keep the curly braces on their own lines.
- Indent continued lines two spaces.
- Indent private methods equal to public methods.
- Use 2 space indentation (no tabs).
- Use an empty line between methods.
- Use spaces around operators, after commas, after colons and semicolons, around
{and before}.
- Avoid abbreviations.
- Avoid types in names (
user_array). - Name the enumeration parameter the singular of the collection.
- Name variables, methods, and classes to reveal intent.
- Treat acronyms as words in names (
XmlHttpRequestnotXMLHTTPRequest), even if the acronym is the entire name (class Htmlnotclass HTML).
- Use CoffeeScript.
- Initialize arrays using
[]. - Initialize empty objects and hashes using
{}. - Use
CamelCasefor classes,mixedCasefor variables and functions,SCREAMING_SNAKE_CASEfor constants,_single_leading_underscorefor private variables and functions.
- Avoid conditional modifiers (lines that end with conditionals).
- Avoid ternary operators (
boolean ? true : false). Use multi-lineifinstead to emphasize code branches. - Define the project's Ruby version in the Gemfile.
- Prefer
detectoverfind. - Prefer
injectoverreduce. - Prefer
mapovercollect. - Prefer
selectoverfind_all. - Prefer single quotes for strings.
- Use
_for unused block parameters. - Use
%{}for single-line strings needing interpolation and double-quotes. - Use
&&and||for Boolean expressions. - Use
{...}for single-line blocks. Usedo..endfor multi-line blocks. - Use
?suffix for predicate methods. - Use
CamelCasefor classes and modules,snake_casefor variables and methods,SCREAMING_SNAKE_CASEfor constants. - Use
def self.method, notdef Class.methodorclass << self. - Use
defwith parentheses when there are arguments. - Use
each, notfor, for iteration. - Use heredocs for multi-line strings.
- When wrapping long lines, keep the method name on the same line as the ERB interpolation operator and keep each method argument on its own line.
- Avoid
memberandcollectionroutes. - Avoid the
:exceptoption in routes. - Don't reference a model class directly from a view.
- Don't use protected controller methods.
- Don't use SQL or SQL fragments (
where('inviter_id is not null')) outside of models. - Keep the
db/schema.rbunder version control. - If there are default values, set them in migrations.
- Name initializers for their gem name.
- Order controller contents: filters, public methods, private methods.
- Order model contents: constants, macros, public methods, private methods.
- Put application-wide partials in the
app/views/applicationdirectory. - Use
_path, not_url, for named routes everywhere except mailer views. - Use
def self.method, not thescope :methodDSL. - Use SQL, not
ActiveRecordmodels, in migrations. - Use the default
render 'partial'syntax overrender partial: 'partial'. - Use the
:onlyoption to explicitly state exposed routes.
- Define a
PRIORITYconstant at the top of delayed job classes. - Define two public methods:
self.enqueueandperform. - Put delayed job classes in
app/jobs.
- Use one
ActionMailerfor the app. Name itMailer. - Use the user's name in the
Fromheader and email in theReply-Towhen delivering email on behalf of the app's users.
- Avoid
its,let,let!,specify,before, andsubject. - Avoid using instance variables in tests.
- Don't prefix
itblock descriptions with 'should'. - Name outer
describeblocks after the method under test. Use.methodfor class methods and#methodfor instance methods. - Order factories.rb: sequences, traits, factory definitions.
- Order factory attributes: implicit attributes, explicit attributes, child factory definitions. Each section's attributes are alphabetical.
- Order factory definitions alphabetically by factory name.
- Prefer
eqto==in RSpec. - Separate setup, exercise, verification, and teardown phases with newlines.
- Use an
itexample for each execution path through the method. - Use one factories.rb file per project.
- Use stubs and spies (not mocks) in isolated tests.