forked from leahneukirchen/styleguide
-
Notifications
You must be signed in to change notification settings - Fork 24
/
RUBY-STYLE
168 lines (95 loc) · 4.38 KB
/
RUBY-STYLE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
= Dan Kubb's Ruby Style Guide
You may not like all rules presented here, but they work very well for
me and have helped producing high quality code. Everyone is free to
code however they want, write and follow their own style guides, but
when you contribute to my code, please follow these rules:
== Formatting:
* Use ASCII (or UTF-8, if you have to).
* Use 2 space indent, no tabs.
* Use Unix-style line endings.
* Use spaces around operators, after commas, colons and semicolons,
around { and before }.
* No spaces after (, [ and before ], ).
* Align `when` and `else` with `case`.
* Use an empty line before the return value of a method (unless it
only has one line), and an empty line between defs.
* Use YARD and its conventions for API documentation. Don't put an
empty line between the comment block and the def.
* Use empty lines to break up a long method into logical paragraphs.
* Keep lines fewer than 80 characters.
* Strip trailing whitespace.
== Syntax:
* Use def with parentheses when there are arguments.
* Never use for, unless you exactly know why.
* Never use then, except in case statements.
* Use when x then ... for one-line cases.
* Use &&/|| for boolean expressions, and/or for control flow. (Rule
of thumb: If you have to use outer parentheses, you are using the
wrong operators.)
* Avoid multiline ?:, use if.
* Use parentheses when calling methods with arguments.
* Use {...} when defining blocks on one line. Use do...end for multiline
blocks.
* Avoid return where not required.
* Avoid line continuation (\) where not required.
* Use ||= freely.
* Use OO regexps, and avoid =~ $0-9, $~, $` and $' when possible.
== Naming:
* Use snake_case for methods.
* Use CamelCase for classes and modules. (Keep acronyms like HTTP,
RFC, XML uppercase.)
* Use SCREAMING_SNAKE_CASE for other constants.
* Do not use single letter variable names. Avoid uncommunicative names.
* Use consistent variable names. Try to keep the variable names close
to the object class name.
* Use names prefixed with _ for unused variables.
* Do not use Enumerable#inject when the "memo" object does not change between
iterations, use Enumerable#each_with_object instead (in ruby 1.9,
active_support and backports).
* When defining a predicate method that compares against another object of
a similar type, name the argument "other".
* Prefer map over collect, detect over find, select over find_all.
== Comments:
* Comments longer than a word are capitalized and use punctuation.
Use one space after periods.
* Avoid superfluous comments.
== The rest:
* Avoid hashes-as-optional-parameters. Does the method do too much?
* Avoid long methods.
* Avoid long parameter lists.
* Use def self.method to define singleton methods.
* Add "global" methods to Kernel (if you have to) and make them private.
* Avoid alias when alias_method will do.
* Always freeze objects assigned to constants.
* Use OptionParser for parsing complex command line options and
ruby -s for trivial command line options.
* Write for 1.9, but avoid doing things you know that will break in 1.8.
* Avoid needless metaprogramming.
* Only give a method one purpose for existing. If you pass in a boolean
to a method, what you're saying is that this method has two different
behaviours. Just split it into two single purpose methods. If you have
to use the words "AND" or "OR" to describe what the method does it
probably does too much.
* If sections of a method are logically separate by blank lines, then
that's probably a sign that those sections should be split into separate
methods.
* Try to keep methods at no more than 10 lines long, and preferably
5 or less.
== General:
* Code in a functional way, avoid mutation when it makes sense.
* Try to have methods either return the state of the object and have
no side effects, or return self and have side effects. This is
otherwise known as Command-query separation (CQS):
http://en.wikipedia.org/wiki/Command-query_separation
* Do not mutate arguments unless that is the purpose of the method.
* Do not mess around in core classes when writing libraries.
* Do not program defensively.
(See http://www.erlang.se/doc/programming_rules.shtml#HDR11.)
* Keep the code simple.
* Don't overdesign.
* Don't underdesign.
* Avoid bugs.
* Read other style guides and apply the parts that don't dissent with
this list.
* Be consistent.
* Use common sense.