1. Indentation
Use two spaces per indentation level (aka soft tabs). No hard tabs:# bad - four spaces def some_method do_something end # good def some_method do_something endit is one line in the VIM configuration file .vimrc:
set shiftwidth=2and you are done.
2. Spaces
Use spaces around operators, after commas, colons and semicolons, around { and before }. Whitespace might be (mostly) irrelevant to the Ruby interpreter, but its proper use is the key to writing easily readable code:# bad - missing whitespaces sum=1+2 hash = {one:1,two:2,three:3} a,b = 1,2 [1,2,3].each {|number| puts number} class FooError<StandardError;end # good sum = 1 + 2 hash = { one: 1, two: 2, three: 3 } a, b = 1, 2 [1, 2, 3].each { |e| puts e } class FooError < StandardError; endThere are some exceptions:
# the exponent operator: result = 2 ** 2 # instead of 2**2 # embedded expressions: "string#{expr}" # instead of "string#{ expr }" # array brackets: [1, 2, 3] # instead of [ 1, 2, 3 ] # method call: do_something(:special) # instead of do_something( :special ) # negation: !number? # instead of ! number? # ranges: 1..5 # instead of 1 .. 5
3. Limited line length
Limit lines to 80 characters.4. Parameter alignment
Align the parameters of a method call if they span more than one line. When aligning parameters is not appropriate due to line-length constraints, single indent for the lines after the first is also acceptable:# starting point (line is too long) def send_mail(source) Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text) end # bad (double indent) def send_mail(source) Mailer.deliver( to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text) end # good (normal indent) def send_mail(source) Mailer.deliver( to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text ) end
5. Large numeric literals
Add underscores to large numeric literals to improve their readability:# bad - how many 0s are there? num = 1000000 # good - much easier to parse for the human brain num = 1_000_000
6. Method arguments definition
Use def with parentheses when there are arguments. Omit the parentheses when the method doesn't accept any arguments:# bad def do_somthing() # do something end # good def do_somthing # do something end # bad def do_somthing_with_arguments arg1, arg2 # do something end # good def do_somthing_with_arguments(arg1, arg2) # do something end
7. Expression result
Leverage the fact that if and case are expressions which return a result:# bad if condition result = x else result = y end # good result = if condition x else y end
8. Assignment operators
Use shorthand self assignment operators whenever applicable# bad x = x + y x = x**y x = x / y x = x && y # good x += y x **= y x /= y x &&= y
9. Variable initialization
Use ||= to initialize variables only if they are not already initialized:# bad language = language ? language : 'Ruby' language = 'Ruby' unless language # good - set language to Ruby, only if it's nil or false language ||= 'Ruby'But beware using ||= for initializing boolean variables. Consider what would happen if the current value happened to be false. Take a look:
# bad - would set enabled to true even if it was false enabled ||= true # good enabled = true if enabled.nil?
10. Hashes
Use the Ruby 1.9 hash literal syntax when your hash keys are symbols:# bad hash = { :one => 1, :two => 2, :three => 3 } # good hash = { one: 1, two: 2, three: 3 }
Further articles of interest:
Supported by Ruby 2.1.1
Keine Kommentare:
Kommentar veröffentlichen