Is it bad form to stage a function's steps in intermediate variables (let bindings)?
Posted
by
octopusgrabbus
on Programmers
See other posts from Programmers
or by octopusgrabbus
Published on 2012-06-18T17:32:12Z
Indexed on
2012/06/18
21:23 UTC
Read the original article
Hit count: 246
clojure
I find I tend to need intermediate variables. In Clojure that's in the form of let bindings, like cmp-result-1
and cmp-result-2
in the following function.
(defn str-cmp
"Takes two strings and compares them. Returns the string if a match; and nil if not."
[str-1 str-2 start-pos substr-len]
(let [cmp-result-1 (subs str-1 start-pos substr-len)
cmp-result-2 (subs str-2 start-pos substr-len)]
(compare cmp-result-1 cmp-result-2)))
I could re-write this function without them, but to me, the function's purpose looks clearer. I tend to do this quite in a bit in my main, and that is primarily for debugging purposes, so I can pass a variable to print out intermediate output.
Is this bad form, and, if so, why?
Thanks.
© Programmers or respective owner