clojure - Is there a good way to check return types when refactoring? -


currently when i'm refactoring in clojure tend use pattern:

(defn my-func [arg1 arg2]   (assert (= demo.core.record1 (class arg1) "incorrect class arg1: " (class arg1))   (assert (= demo.core.record1 (class arg2) "incorrect class arg2: " (class arg2))   ... 

that is, find myself manually checking return types in case downstream part of system modifies them don't expect. (as in, if refactor , stack-trace don't expect, express assumptions invariants, , step forward there).

in 1 sense, kind of invariant checking bertrand meyer anticipated. (author of object oriented software construction, , proponent of idea of design contract).

the challenge don't find these out until run-time. nice find these out @ compile-time - stating function expects.

now i know clojure dynamic language. (whilst clojure has 'compiler' of sorts, should expect application of values function come realisation @ run-time.)

i want pattern make refactoring easier. (ie see flow-on effects of changing argument function, without seeing break on first call, moving onto next, moving onto next breakage.)

my question is: is there way check return types when refactoring?

you have couple of options, 1 of "compile" time:

tests

as clojure dynamic language, tests absolutely essential. safety net when refactoring. in statically typed languages tests still of use.

pre , post conditions

they allow verify invariants adding metadata functions such in example michael fogus' blog:

(defn constrained-fn [f x]   {:pre  [(pos? x)]    :post [(= % (* 2 x))]}   (f x))  (constrained-fn #(* 2 %) 2) ;=> 4 (constrained-fn #(float (* 2 %)) 2) ;=> 4.0 (constrained-fn #(* 3 %) 2) ;=> java.lang.exception: assert failed: (= % (* 2 x) 

core.typed

core.typed option in list give compile time checking. example expressed so:

(ann  my-func (fn [record1 record1 -> resulttype])) (defn my-func [arg1 arg2] ...) 

this comes @ expense of running core.typed seperate action, possibly part of test suite.

and still on realm of runtime validation/checking, there more options such bouncer , schema.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -