My personal preference is that fns ending in ? return a strict boolean, to
be consistent with clojure.core. If I need the value I create another fn.
So I would have nsf-code? *and* get-nsf-code. Plus for more complicated
values, if you change their representation you don't have to change the
code calling the predicate, only the getter fn (if that),
--Leif
On Thursday, April 17, 2014 12:33:42 PM UTC-4, Sean Corfield wrote:
>
> 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)
>
>
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.