The library coding standards[1] say: * Use '?' suffix for predicates. - N.B. - predicates return booleans
and the community Clojure style guide[2] says: * The names of predicate methods (methods that return a boolean value) should end in a question mark. (i.e.even?). Both of these imply that if you have a function that returns a boolean (and that is intended for use as a predicate), it should be named to end in '?'. Fair enough. My question is about the reverse implication: * Should a function whose name ends in '?' return a (strict) boolean value? Looking at the docstrings of a random selection of functions found by (apropos "?"), they all seem to return specifically true or false. I did not do an exhaustive check. Is the intent that foo? implies a result of true or false - or could foo? return any truthy / falsey value (and therefore any Clojure value). Concrete example that spurred this discussion from some code at work: (defn is-nsf-code? "Given an error code, return truthy if it is NSF." [code] (#{"BE1" "BE2"} code)) Clearly the result here could be nil or a string but it's definitely meant to be used as a predicate. Similarly: (defn nsf? "Given the result of an SBW sale, return true if it failed with NSF." [result] (and (= "failure" (:result result)) (some is-nsf-code? (:errors result)))) Again, the result could be false or nil or a string but is meant to be used as a predicate. As an aside, for core.typed, we annotate the first as [String -> Boolean] with ^:no-check so it type checks as a true/false predicate and then we annotate the second as [SBWResult -> (Nilable Boolean)] and that's all fine... but is it "good style"? [1] http://dev.clojure.org/display/community/Library+Coding+Standards [2] https://github.com/bbatsov/clojure-style-guide#naming Sean Corfield -- (904) 302-SEAN An Architect's View -- http://corfield.org/ "Perfection is the enemy of the good." -- Gustave Flaubert, French realist novelist (1821-1880)
signature.asc
Description: Message signed with OpenPGP using GPGMail