title: How do I get better feedback from Clojure errors?
body: <p>I find it very difficult to debug the Clojure errors I have in my code compared to all the other programming languages I've used.  My primary programming language is Java and I'm very new to Clojure.  The majority of my time writing Clojure is spent trying to figure out, "Why am I getting this error?" and I'd like to change that.  I'm using CounterClockWise as my primary IDE.  I don't know how to use Emacs (yet?).</p>
  
  <p>Here's an example:</p>
  
  <pre><code>(ns cljsandbox.core)
  
  (def l [1 2 3 1])
  
  (defn foo
    [l]
    (-&gt;&gt; l
      (group-by identity)
      ;vals  ;commented out to show my intent
      (map #(reduce + %))))
  </code></pre>
  
  <p>Here, I mistakenly thought that <code>group-by</code> returns a list of lists, but it actually returns a map of <code>&lt;key, list&lt;value&gt;&gt;</code> or however you'd phrase it in Java terms.  This gives an error message that says:</p>
  
  <blockquote>
    <p>ClassCastException clojure.lang.PersistentVector cannot be cast to java.lang.Number  clojure.lang.Numbers.add (Numbers.java:126)</p>
  </blockquote>
  
  <p>This isn't very helpful because there isn't a stack trace.  If I type <code>(e)</code> it says:</p>
  
  <pre><code>java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to java.lang.Number
   at clojure.lang.Numbers.add (Numbers.java:126)
      clojure.core$_PLUS_.invoke (core.clj:944)
      clojure.core.protocols/fn (protocols.clj:69)
      clojure.core.protocols$fn__5979$G__5974__5992.invoke (protocols.clj:13)
      clojure.core$reduce.invoke (core.clj:6175)
      cljsandbox.core$foo$fn__1599.invoke (core.clj:10)
      clojure.core$map$fn__4207.invoke (core.clj:2487)
      clojure.lang.LazySeq.sval (LazySeq.java:42)
  </code></pre>
  
  <p>I have no idea how I can go from this error message to understanding, "You thought you were passing in a list of lists into <code>map</code> but you were really passing in a map datatype".  The stack trace shows the problem was reported inside of <code>reduce</code>, not inside of <code>group-by</code>, but IMO, that is not where I as a human made my mistake.  That's just where the program discovered a mistake had been made.</p>
  
  <p>Issues like these can take me 15+ minutes to resolve.  How can I make this take less time?</p>
  
  <hr>
  
  <p>I know it's too much to expect a dynamic language to catch these errors. But, I feel like the error messages of other dynamic languages like javascript are much more helpful.</p>
  
  <p>I'm getting pretty desperate here, because I've been coding in clojure for 1-2 months now and I feel like I should have a better handle on figuring out these problems.  I tried using <code>:pre</code>/<code>:post</code> on functions but that has some problems</p>
  
  <ol>
  <li>The reporting on <code>:pre</code>/<code>:post</code> kind of sucks.  It only prints out literally what you test.  So unless you put a lot of effort into it, the error message isn't helpful.  </li>
  <li>This doesn't feel very idiomatic.  The only code I've seen that uses <code>:pre</code>/<code>:post</code> are articles that explain how to use <code>:pre</code>/<code>:post</code>.</li>
  <li>It's a real pain to pull out the steps of a threading macro into their own <code>defn</code>s so that I can put the <code>:pre</code>/<code>:post</code> in them.</li>
  <li>If I followed this practice religiously, I think my code could become as verbose as Java.  I'd be reinventing the type system by hand.</li>
  </ol>
  
  <p>I've gotten to the point where I pepper my code with safety checks like this:</p>
  
  <pre><code>(when (= next-url url)
              (throw (IllegalStateException. (str "The next url and the current url are the same " url))))      
  (when-not (every? map? posts-list)
              (throw (IllegalStateException. "parsed-html-&gt;posts must return a list of {:post post :source-url source-url}")))
  </code></pre>
  
  <p>Which only fixes that first bullet point.</p>
  
  <p>I feel like either</p>
  
  <ol>
  <li>I've got a development process that's very, very wrong and I don't know it</li>
  <li>There's some debugging tool/library out there that I don't know about that everyone else does</li>
  <li>Everyone else is having problems like this and it's Clojure's dirty little secret / Everyone else is used to dynamic languages and expects to go through the same effort I am going through to resolve errors</li>
  <li>CounterClockWise has some bug that's making my life way harder than it needs to be</li>
  <li>I'm supposed to be writing a lot more unit tests for my Clojure code than I do for my Java code.  Even if I'm writing throwaway code.  </li>
  </ol>