Skip to content

Instantly share code, notes, and snippets.

@chespinoza
Forked from Engelberg/cond-example.clj
Created April 21, 2019 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chespinoza/e0cc2a34fc627da1f3d20cb4c2649527 to your computer and use it in GitHub Desktop.
Save chespinoza/e0cc2a34fc627da1f3d20cb4c2649527 to your computer and use it in GitHub Desktop.
refactoring with better-cond
; The sample to refactor
(if-let [x (foo)]
(if-let [y (bar x)]
(if-let [z (goo x y)]
(do
(qux x y z)
(log "it worked")
true)
(do
(log "goo failed")
false))
(do
(log "bar failed")
false))
(do
(log "foo failed")
false))
; The pattern of logging and returning a value happens frequently, should be a function, although it doesn't save characters.
(defn log-return [msg return] (do (log msg) return))
; Refactor using better-cond, see github.com/engelberg/better-cond
(cond
:let [x (foo)]
(not x) (log-return "foo failed" false)
:let [y (bar x)]
(not y) (log-return "bar failed" false)
:let [z (goo x y)]
(not z) (log-return "goo failed" false)
:do (qux x y z)
(log-return "it worked" true))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment