Is there any code smell in this use of let

I often end up with code like this in the place where I create temporary variables, just to save state. What is the best way to do this?

The thrush statement will not help, because although e is temporary, I still need c and d in sequential let forms.

(let [[cd] (sum [ab]) e (if (even? c) c 0) f (+ se)] ..... ) 
+4
source share
4 answers

I see a couple of ways you could reorganize it. I do not think that this is objectively better than what you wrote, but I will send it as food for thought.

 (let [c (first (sum [ab])) f (+ s (if (even? c) c 0))] ...) 

Or that

 (defn evenz [n] (if (even? n) n 0)) (let [f (+ s (evenz (first (sum [ab]))))] ...) 
+1
source

It looks good to me. The "state" is not really a state - there are no side effects. This can become bad if many of the let bindings and their dependencies get complicated, but this is a common property of complex code (duh) that is not related to this pattern.

+3
source

I do not see any problems with this template. You will need to create temporary vars in this case, because the various operations cannot be directly linked, and therefore you use temp vars to make them work.

+2
source

From my point of view, this looks like pretty understandable "functional style" code. I see nothing wrong with this, except that the names are a bit short ;-)

  • giving the whole name with let can make the code larger, but easier to learn
  • using conditional expressions in let is common even in clojure source
  • formatting is normal for let statements.

perhaps more context leads to constructive suggestions?

+1
source

All Articles