Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Nah. For arbitrary functions composed of N intermediary variables, the maximum complexity is exponential in N, if not higher. Intermediate variables most definitely allow for highly complex functions to be expressed cleanly.

share|improve this answer
add comment

Side note: It's a shame that there isn't any Clojure highlighting here.

I would consider this purely a matter of preference. Never would I have considered it bad form. It doesn't always promote readability, but rarely does it harm it.

However, I have no idea what rc means, so if you're going to use auxiliary variables, they might as well promote readability by revealing some intent with a better choice of words.

share|improve this answer
    
Good point. I fixed the variable names. –  octopusgrabbus Jun 18 '12 at 17:51
    
@octopusgrabbus Wouldn't substr1, substr2 be more appropriate? Since they aren't really the result of a comparison, but the result of a substring. –  Yam Marcovic Jun 18 '12 at 18:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.