Fork me on GitHub
#beginners
<
2017-08-01
>
pmonks23:08:17

G'day gurus - I have a couple of hopefully simple questions on style: I have a little fn (basically a predicate) that has three possible outcomes (return values) - true, false, and "the argument you provided is invalid" (which can happen through no fault of the caller's, since the argument is used to lookup a shared mutable data source). Right now I'm using true, false, and nil to identify these three cases, but the use of nil has me feeling a bit uncomfortable (due to nil punning etc.). What would you suggest? FWIW I'm very reluctant to use exceptions (this is not an "exceptional" situation, and they're a pain to handle), but I'm not sure what else would be idiomatic - perhaps an :invalid-argument keyword? Related: right now my fn has a name with a ? at the end, since it's basically a predicate, but I'm a little concerned about this third state it might return. Should I be ditching the ? suffix in the interests of clearly communicating intent?

dpsutton23:08:15

why not three keywords that more mimic what your domain is about?

dpsutton23:08:49

ie, if you were looking up if a check could clear you could return :sufficient (funds) :insufficient and :no-account

pmonks23:08:45

The use case here is the question "is this user (identified with an id of some kind) in my company or not".

dpsutton23:08:47

then handle the three cases. you could then have an easy predicate based on these with (defn is-sufficient? [check account] (= (clear-check check account) :sufficient) or whatever your domain needs

pmonks23:08:04

The issue is that "user identified by id" part - it's entirely possible for an identifier to no longer be valid.

dpsutton23:08:05

and what separates nil from false in this case?

pmonks23:08:12

Right - that's exactly my question.

dpsutton23:08:22

but wouldn't that answer user in your company the same?

dpsutton23:08:32

the predicate of yours sounds like a binary predicate to me

pmonks23:08:34

I also have an inverse fn "is this user not in my company", and it also returns nil for that third case.

dpsutton23:08:42

yes they are in teh company or no, they are not

dpsutton23:08:56

no longer active versus not valid employee number sounds separate to me

pmonks23:08:08

It seems "wrong" to me to have two fns that should be logical not, not actually be so in some cases (i.e. when the user identifier is invalid)

dpsutton23:08:06

then change your question? But if the question is "this id is an employee in our company" has two answers to me

pmonks23:08:06

Right - in fact the fn (deliberately) can't tell the caller why the identifier is invalid, just that it is.

dpsutton23:08:36

well you can make another function to do that

pmonks23:08:38

The trick here is that the identifier is "resolved" via a multi-method that can take all sorts of types.

pmonks23:08:04

In some of those cases, it's not possible for the identifier to be invalid (i.e. the identifier is, in fact, the object we're looking up, having previously been loaded from the data source).

dpsutton23:08:04

does the object satisfy a protocol or record or anything? can you ask some type of isa?

pmonks23:08:28

Nah - I greatly dislike protocols, though I'm not entirely sure why. 😉

pmonks23:08:03

The two fns in question are same-pod? and cross-pod?.

dpsutton23:08:13

(defn same-pod?
      "Returns true if the given user is in the same pod as the authenticated connection user, or nil if the user doesn't exist."
      [connection user-identifier]
      (let [them (if (:company user-identifier)
                   user-identifier
                   (user connection user-identifier))
            me (user connection)]
        (same-company? me them)))

dpsutton23:08:17

this is what i am thinking

dpsutton23:08:35

and here same-company is (= (:company me) (:company them))

dpsutton23:08:52

so use them if its already the object (which seems to be identified by having a :company keyword entry or else look it up

pmonks23:08:11

Yeah - that's a nice optimisation on not looking up the user details if we already have them! :+1:

dpsutton23:08:35

the overwrite the variable with a version of itself is a common pattern.

pmonks23:08:42

Though it doesn't answer my original question about having a "predicate" that returns three states, and whether using nil is appropriate for the third state.

dpsutton23:08:30

oh i thought this worked around that. I understood nil would come up when you had the object you were already looking up. So my goal was to collapse this down into a true predicate

pmonks23:08:52

Nope - (user ...) can return nil.

dpsutton23:08:12

but my suggestion would be figure out how to use two states or get a type that has three values. In other words, only use boolean types if you have boolean states. if you have three make your own descriptive keywords for the state

dpsutton23:08:04

and if you do that you can return a map that includes the reason why something is invalid along with the fact that it is invalid

pmonks23:08:32

Yeah I wasn't planning on providing any information on why the identifier is invalid - as you said above that can be provided by some other fn.

pmonks23:08:43

(e.g. (user ...))

pmonks23:08:24

:+1: Anyhoo this has been very useful - thanks @dpsutton ! :+1:

dpsutton23:08:37

no problem. best of luck