Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Created April 14, 2012 00:54
Show Gist options
  • Save pbalduino/2381227 to your computer and use it in GitHub Desktop.
Save pbalduino/2381227 to your computer and use it in GitHub Desktop.
Refactoring with functional programming
; I need to write code to make this pass:
; https://github.com/pbalduino/north/blob/master/test/north/test/core.clj
; First I wrote that
(defn be-true [result]
(assert (= result true) (format "Assertion failed. %s was expected to be true" result)))
; So I added be-false and slightly changed be-true
; Then I noticed there's duplication of code there
(defn be-true [result]
(let [expectation true]
(assert (= result expectation) (format "Assertion failed. %s was expected to be %s" result expectation))))
(defn be-false [result]
(let [expectation false]
(assert (= result expectation) (format "Assertion failed. %s was expected to be %s" result expectation))))
; Now I added be-equals and refactored the code again
(defn be-equals [result expectation]
(assert (= result expectation) (format "Assertion failed. %s was expected to be %s" result expectation)))
(defn be-true [result]
(be-equals result true))
(defn be-false [result]
(be-equals result false))
; But when I added be-not-equals I saw duplication again
(defn be-not-equals [result expectation]
(assert (not= result expectation) (format "Assertion failed. %s was expected to be %s" result expectation)))
(defn be-equals [result expectation]
(assert (= result expectation) (format "Assertion failed. %s was expected to be %s" result expectation)))
(defn be-true [result]
(be-equals result true))
(defn be-false [result]
(be-equals result false))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment