Created
April 3, 2017 06:07
-
-
Save Engelberg/db9bd2fc540f07ba0a64197aa3924f26 to your computer and use it in GitHub Desktop.
refactoring with better-cond
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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