Code style guide

benlangfeld edited this page Mar 27, 2013 · 2 revisions

Adhearsion Code Style Guide

Ruby is very flexible and will allow you to butcher all of the good things about its syntax in a large variety of ways. Here we'll present, by example, how to keep things nice.

Parentheses

Bad

foo.bar(a)

Good

foo.bar a

Bad

def bar a = {}
  ...
end

Good

def bar(a = {})
  ...
end

Statement modifiers

Bad

if foo
  a = :bar
end

Good

a = :bar if foo

Conditional assignment

Bad

if foo
  a = :bar
else
  a = :doo
end

Good

a = if foo
  :bar
else
  :doo
end

Better

a = foo ? 10 : 100

Whitespace

Bad

foo.bar a,b,c
foo.bar( a,b,c )
foo.bar a ,b ,c

Good

foo.bar a, b, c

Bad

a=0

Good

a = 0

Bad

def foo(a=nil)
end

Good

def foo(a = nil)
end

Bad

{foo:'bar'}
{foo: 'bar'}
{ foo:'bar' }

Good

{ foo: 'bar' }

Boolean conditions

Bad

puts "Hello" unless mary == nil
puts "Tiny" if foo == 0

Good

puts "Hello" unless mary.nil?
puts "Tiny" if foo.zero?

Double negatives

Bad

foo = false
if !foo
  ...do something...
end

Good

foo = false
unless foo
  ...do something...
end

Bad

foo = false
if !foo
  ...do something...
else
  ...do something else...
end

Equally Bad

foo = false
unless foo
  ...do something...
else
  ...do something else...
end

Good

foo = false
if foo
  ...do something else...
else
  ...do something...
end

Bad

while !foo
  ...do something...
end

Good

until foo
  ...do something...
end

Many requires

Bad

require 'foo'
require 'bar'
require 'doo'
require 'dah'

Good

%w{
  foo
  bar
  doo
  dah
}.each { |f| require f }

Empty classes

Bad

class MyNewException < StandardError; end

Good

MyNewException = Class.new StandardError

Unnecessary Braces

Bad

foo.bar 1, { foo: 'bar' }
foo.bar({ foo: 'bar' })

Good

foo.bar 1, foo: 'bar'
foo.bar foo: 'bar'

Unnecessary returns

Bad

def blah(foo)
  if foo
    ...do something...
    return 1
  else
    return 2
  end
end

Good

def blah(foo)
  if foo
    ...do something...
    1
  else
    2
  end
end

Bad

def foo
  ...do something...
  return nil
end

Good

def foo
  ...do something...
  nil
end

Unnecessary conditionals

Bad

def blah(foo)
  false if foo
end

Good

def blah(foo)
  !foo
end

Returning nil

Bad

def foo
  return nil if something
  ...do something
end

Good

def foo
  return if something
  ...do something...
end

while true

Bad

while true
  ...do something...
end

Good

loop do
  ...do something...
end

Consistent indenting

Bad

a = :foo
a1 = :bar
doo = :dah

Good

a   = :foo
a1  = :bar
doo = :dah

Parallel assignment

Bad

def initialize(a, b, c)
  @a = a
  @b = b
  @c = c
end

Good

def initialize(a, b, c)
  @a, @b, @c = a, b, c
end

JSON-style hash syntax

Preferred where it is readable. Don't mix syntax in a single hash.

Good

foo bar: 'baz', doo: dah
foo :bar => 'baz', 'doo' => dah

Bad

foo bar: 'baz', 'doo' => dah
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.