A guide for programming in style.
- 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 misspell.
- Don't vertically align tokens on consecutive lines.
- If you break up an argument list, keep the arguments on their own lines and closing parenthesis on its own line.
- If you break up a hash, keep the elements on their own lines and closing curly brace on its own line.
- 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 newlines around multi-line blocks.
- Use spaces around operators, after commas, after colons and semicolons, around
{and before}. - Use Unix-style line endings (
\n). - Use uppercase for SQL key words and lowercase for SQL identifiers.
- 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). - Name variables holding a factory with
_factory(user_factory). - Name variables created by a factory after the factory (
user_factorycreatesuser).
- Use Sass.
- Use the Scss syntax.
- Use dashes when naming mixins, extends, classes or IDs:
span-columnsnotspan_columnsorspanColumns. - Use descriptive names and write them in full-words:
$visual-grid-colornot$coloror$vslgrd-clr. - Use space between property and value:
width: 20pxnotwidth:20px. - Order properties within rule sets alphabetically.
- Leave a blank line between rule sets.
- Use CoffeeScript.
- Initialize arrays using
[]. - Initialize empty objects and hashes using
{}. - Use
CamelCasefor classes,lowerCamelCasefor variables and functions,SCREAMING_SNAKE_CASEfor constants,_single_leading_underscorefor private variables and functions. - Prefer
isto==or=== - Prefer
orandandto||and&&
- Avoid conditional modifiers (lines that end with conditionals).
- Avoid organizational comments (
# Validations). - Avoid ternary operators (
boolean ? true : false). Use multi-lineifinstead to emphasize code branches. - Avoid explicit return statements.
- 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 ActiveRecord associations alphabetically by attribute name.
- Order ActiveRecord validations alphabetically by attribute name.
- Order controller contents: filters, public methods, private methods.
- Order model contents: constants, macros, public methods, private methods.
- Order resourceful routes alphabetically by name.
- 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.
- Avoid scenario titles that add no information, such as "successfully."
- Avoid scenario titles that repeat the feature title.
- Avoid the
privatekeyword in specs. - Don't prefix
itblock descriptions with 'should'. - Include the Features module in RSpec scenarios with
:type => :featureset. - Name outer
describeblocks after the method under test. Use.methodfor class methods and#methodfor instance methods. - Order ActiveRecord association tests alphabetically by attribute name.
- Order ActiveRecord validation tests alphabetically by attribute name.
- Order
factories.rbcontents: 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.
- Place helper methods for feature specs directly in a top-level
Featuresmodule. - 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.
- Use a single level of abstraction within scenarios.
- Use names like
ROLE_ACTION_spec.rb, such asuser_changes_password_spec.rb, for feature spec file names. - Use only one
featureblock per feature spec file. - Use scenario titles that describe the success and failure paths.
- Use spec/features to store feature specs.
- Use spec/support/features for support code related to feature specs.
#importlinked frameworks in the prefix header (ProjectName-Prefix.pch).- Keep
.xibfiles grouped with their associated view class. - Order
#importstatements alphabetically. - Order
@classdirectives alphabetically. - Order
@propertymodifiers: memory management, atomicity, writability. - Organize classes into
models,views,controllers,categories, andservicesdirectories. - Prefer
@classto#importwhen referring to external classes in a public@interface. - Prefer
@propertyto declaring instance variables. - Prefix class names with a 2-letter project acronym.
- Prefix string constants being used as keys with 'k'.
- Remove
#importstatements forFoundationandUIKitin new project templates. - Separate methods by function using
#pragma mark - <Section Name> - Separate sections into subsections using
#pragma mark <Subsection Name> - Use
@[arrayObject],@{@"key" : value},@(YES or NO), and@5.0literals. - Use
@interface ClassName ()to declare private properties. - Use
lowerCamelCasefor method names. - Use
NSAssertin methods that require the presence of certain arguments. - Write methods using the happy path. Indent the exceptional cases. Keep the optimal case in the left-most column.
- Follow PEP 8.