Version 4, last updated by Vitalie Lazu at July 12, 2011 16:09 UTC
Code style
Code Style Guidelines
Taken from merb dev site.
There are a few guidelines that your code should follow. First setup your editor:
- max 130 chars per line
- 2 space tabs
- convert tabs to spaces
- remove trailing spaces
- always add empty line at end of file
- Please also follow rails code style.
1. Parentheses around parameter lists for methods
# BAD!
def my_method param, arg
puts "OH NOEZ!"
end
# GOOD!
def my_method(param, arg)
puts "HOORAYZ!"
end
# Common use of poetry mode is OK
# GOOD!
raise ControllerExceptions::NotFound, "No routes match the request"
2. Two space indent
# BAD!
def tabby
puts "A fat tab!"
end
# GOOD!
def tabby
puts "Two spaces!"
end
3. Documentation is required
If you make a patch that lacks proper documentation, it will not be accepted (and yes we are aware some of the documentation is sub-par at this point :)).
# BAD!
# Output
def output(value, options)
# Do something really impressive and complex...
end
# GOOD!
# Output the value given to the method, with formatting options
# given in +options+.
#
# ==== Options
# +bold+:: Set to +true+ to make output bold.
# +italic+:: Set to +true+ to make output italics.
# +small+:: Set to true to make your output a small size.
#
# ==== Examples
# output("This is something normal.")
# # => This is something normal.
#
# output("This is important...", :bold => true)
# # => <b>This is important!</b>
#
# output("This is very important!", :bold => true, :italic => true)
# # => <b><i>This is very important!</b></i>
#
# output("This...not so much.", :small => true)
# # => <font style='font-size: 7pt;'>This...not so much.</font>
#
4. Block-types in different situations
Use the {} syntax for single-line blocks with one action. Use multi-line blocks for all other situations that use the do/end style block.
# BAD!
array.each do |me|; puts me; end
array.each { |me| puts me; and.me; }
array.each { |thing|
thing.do_it('2')
}
# GOOD!
array.each { |me| puts me }
array.each do |thing|
thing.do_it('2')
thing.wha?
end
5 . Rails templates
Every rails action page should have
- html.title, we have helpers method to assist you with this: *space_page_title* for space pages and *title* for other pages: user, home areas
- h1 title: <h1>page title</h1>
- it should follow design style: Style - Basics, Style - Forms, Style - Links and Buttons
6. Split long lines
Align code vertically by (), {}, []
def method1
long_method(param1, param2, param3,
space.tool.vcs_url("http://gerrit.example.com"))
end
CONSTANT = [:value1, :value2, :value3,
:new_line]