Fork me on GitHub

What are people’s preference for “Business Logic” functions return values. For example:

=> [{,,,prod} {,,,prod} {,,,prod}] 

=> [true [{,,,prod} {,,,prod} {,,,prod}]] 
=> [false {,,,error}] 

=> {:status true :results [{,,,prod} {,,,prod} {,,,prod}]] 
=> {:status false :results {,,,error}]


I use left/right from and functions like >>=


"It Depends" 🙂 Under various different circumstances I might have a function return nil to mean I failed (and thus [] to mean I succeeded but found no products). If the scope of use is very small, I might use the second approach

(let [[success products] (get-products)]
  (if success
    (process products)
but if the scope is larger (e.g., get-products is part of a more widely used API) then I'll use the hash map approach since it is more readable.


Are there ever scenarios where your using all three and have you found that can introduce its own challenges? By challenge, i’m thinking just difficult achieving a flow state because one might not know the possible return type.


Or the Either monad from, and you get either a result or a bunch of errors.


If you're composing functions that consistently return results (success) or errors (failure) then, yes, the either monad or something similar is probably worthwhile. But having to reach for monads would make me consider the flow of that code: I'd want to see if I could simplify the pipeline of operations at that point first.


Often, when I return a hash map like that, I will also return :errors for the failure case (and sometimes I'll omit the Boolean since you can just do:

(let [products (get-products)]
  (if-let [errors (:errors products)]
    (handle-those errors)
    (process (:results products))))


If you go with the hash map approach -- be consistent and use the same keys everywhere you do it. Write a Spec for it. Maybe even write predicates for succeeded? / failed?

👌 6
💯 6

> “It Depends” 🙂 hehe my fav’ corfield response.


Probably worth noting for their approach of marking an error (anomalous?) result with a particular keyword existing in a map


Noted! interesting. Is this lib actively in use? It seems to only be specs. First time seeing this for me.


We use it at work. The exoscale folks have a variant of it with more code support around it.


Thanks for sharing! I had not seen ex before 🙇


So, the idea of anomalies and ex is to establish an error state convention, yes?


Yes, standardized failure categories.


It's a bit biased toward HTTP status style failures but they can be used more broadly (in particular: note whether the failure indicates caller or callee and whether a caller could expect success on the same call later on).