Skip to content

Commit e1e17a5

Browse files
committed
adds a section about booleans in the API guidelines [ci skip]
1 parent 225bcad commit e1e17a5

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

guides/source/api_documentation_guidelines.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,53 @@ On the other hand, regular comments do not use an arrow:
128128
# polymorphic_url(http://github.com/record) # same as comment_url(http://github.com/record)
129129
```
130130

131+
Booleans
132+
--------
133+
134+
In predicates and flags prefer documenting boolean semantics over exact values.
135+
136+
When "true" or "false" are used as defined in Ruby use regular font. The
137+
singletons `true` and `false` need fixed-width font. Please avoid terms like
138+
"truthy", Ruby defines what is true and false in the language, and thus those
139+
words have a technical meaning and need no substitutes.
140+
141+
As a rule of thumb, do not document singletons unless absolutely necessary. That
142+
prevents artificial constructs like `!!` or ternaries, allows refactors, and the
143+
code does not need to rely on the exact values returned by methods being called
144+
in the implementation.
145+
146+
For example:
147+
148+
```markdown
149+
`config.action_mailer.perform_deliveries` specifies whether mail will actually be delivered and is true by default
150+
```
151+
152+
the user does not need to know which is the actual default value of the flag,
153+
and so we only document its boolean semantics.
154+
155+
An example with a predicate:
156+
157+
```ruby
158+
# Returns true if the collection is empty.
159+
#
160+
# If the collection has been loaded
161+
# it is equivalent to <tt>collection.size.zero?</tt>. If the
162+
# collection has not been loaded, it is equivalent to
163+
# <tt>collection.exists?</tt>. If the collection has not already been
164+
# loaded and you are going to fetch the records anyway it is better to
165+
# check <tt>collection.length.zero?</tt>.
166+
def empty?
167+
if loaded?
168+
size.zero?
169+
else
170+
@target.blank? && !scope.exists?
171+
end
172+
end
173+
```
174+
175+
The API is careful not to commit to any particular value, the predicate has
176+
predicate semantics, that's enough.
177+
131178
Filenames
132179
---------
133180

0 commit comments

Comments
 (0)