Code is a poetry

Pre and post conditions in Clojure

Posted at — Jul 1, 2021

Today we will discuss a topic of pre and post assertions (conditions) in Clojure. The language gives programmers an option to set certain conditions we want to constrain our function with. We can define the limitations on the input parameters and the return value of the function.

Pre and post assertions are defined in a vector and can be saved in a function’s metadata. The assertions we define in pre and post condition vectors must return logical true for the constrains to pass. If any of them evaluate to false we will receive an AssertionError with the specific constrain assertion returned. Lets try it in the REPL.

(defn func ^{:pre [(pos? x)]
             :post [(< % 100) (> % 1)]}
  (+ 1 x))

;; Our function fails on pre condition: function argument should be a positive number.
;; Post condition is not checked in this case.
(func -1) =>	Execution error (AssertionError) at user/func (REPL:1).
				Assert failed: (pos? x)

;; Our function fails on post condition: output should be less then 100.
(func 100) =>	Execution error (AssertionError) at user/func (REPL:1).
				Assert failed: (< % 100)