Hi Clojurians, 

I'm relatively new to the language and am trying to get used to its idioms. 
One thing I'm accustomed to doing in things like java and C# is checking 
values for validity and then bailing out early if they don't make sense. 
For example, without this idiom in java you might do:
 
Object doSomething() { 

    Integer a = someComputation();
    if(a != null) {
        Integer b = anotherComputation(a, 42);
        if(b != null && b.intValue() >= 0) {
            return a / b;
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
} 
... which is really only desirable if you believe in the "one exit point" 
school of imperative programming. It is of course much better to do this:
Object doSomething() {
    Integer a = someComputation();
    if(a == null) { return null; } 

    Integer b = anotherComputation(a, 42);
    if(b == null || b.intValue == 0) { return null; }

    return a / b;
}



... which is much more literate. In Clojure, I have to write what is 
effectively the first form:

(let [a (some-computation)]
  (if (nil? a)
    nil
    (let [b (another-computation a 42)]
      (if (or (nil? b) (= b 0))
        nil
        (/ a b)))))

While more concise, it suffers the same readability problems as the first 
java version. I can easily imagine a macro to support this idiom:

(let-check [a (some-computation)
            :check (nil? a) nil
            b (another-computation a 42)
            :check (or (nil? b) (< b 0)) nil]
  (/ a b))


Which leads me to my question: does such a construct already exist? Or 
perhaps am I doing it wrong? I've googled around for this, but I'm not 
exactly sure what it's called. 

Cheers, 

Russell

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to