Browse files

adds a section about booleans in the API guidelines [ci skip]

  • Loading branch information...
fxn authored and zzak committed Jan 26, 2014
1 parent 605c81b commit 7afd92e60b38b34404a38592d6e3e652c1623b6a
Showing with 47 additions and 0 deletions.
  1. +47 −0 guides/source/
@@ -128,6 +128,53 @@ On the other hand, regular comments do not use an arrow:
# polymorphic_url(record) # same as comment_url(record)
+In predicates and flags prefer documenting boolean semantics over exact values.
+When "true" or "false" are used as defined in Ruby use regular font. The
+singletons `true` and `false` need fixed-width font. Please avoid terms like
+"truthy", Ruby defines what is true and false in the language, and thus those
+words have a technical meaning and need no substitutes.
+As a rule of thumb, do not document singletons unless absolutely necessary. That
+prevents artificial constructs like `!!` or ternaries, allows refactors, and the
+code does not need to rely on the exact values returned by methods being called
+in the implementation.
+For example:
+`config.action_mailer.perform_deliveries` specifies whether mail will actually be delivered and is true by default
+the user does not need to know which is the actual default value of the flag,
+and so we only document its boolean semantics.
+An example with a predicate:
+# Returns true if the collection is empty.
+# If the collection has been loaded
+# it is equivalent to <tt></tt>. If the
+# collection has not been loaded, it is equivalent to
+# <tt>collection.exists?</tt>. If the collection has not already been
+# loaded and you are going to fetch the records anyway it is better to
+# check <tt></tt>.
+def empty?
+ if loaded?
+ else
+ @target.blank? && !scope.exists?
+ end
+The API is careful not to commit to any particular value, the predicate has
+predicate semantics, that's enough.

0 comments on commit 7afd92e

Please sign in to comment.