I would prefer not throwing an exception in the case of a validation.

Say your validation function returns a vector or the first argument
contains validation errors if any and the second contains the original map:

(validate my-map) ;; returns [{:errors '("blah")} original-map]


Then a neat way to write your function, taking advantage or core.match[1],
would be this:

(defn my-fn [my-map]
  (match (validate my-map)
      [{:errors errors} original-map] (handle-errors)
      [_ original-map] (save original-map)))

This is my personal preference. It's concise, elegant and idiomatic in
languages that support pattern matching.

If you'd rather not use core.match for whatever reason, I'd still opt out
of throwing an exception. The function would just need a little bit more
code to simulate the behaviour above.

[1]: https://github.com/clojure/core.match

Leonardo Borges
www.leonardoborges.com


On Sat, Aug 17, 2013 at 12:08 PM, Alexandr Kurilin <a...@kurilin.net> wrote:

> Let's hypothetically say I have a Ring application and I'm performing some
> validation on the input map I receive as part of a POST request. The
> validate function throws an exception if anything's wrong with the input.
> The exception is caught in the middleware and turned into a 400 code
> response.
>
>  I could do either:
>
> (-> input
>      validate
>      save)
>
> or I could do:
>
> (do
>   (validate input)
>   (save input))
>
> The former doesn't really buy me anything, since a new version of input
> would never be returned by validate. It is however "side-effect free" in a
> way?
> The latter is probably less idiomatic, but makes it obvious that input is
> not changing as part of the evaluation.
>
> What would be the more elegant way of handling this? Is the do block
> approach somehow inferior?
>
> Thanks!
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 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 clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to