Arc Forumnew | comments | leaders | submitlogin
My favorite debug print macro
12 points by CatDancer 5847 days ago | 3 comments
I never end up using a debugger with dynamic languages. I have nothing against debuggers, and I'd use one if I found it useful to me, but when I'm in the middle of my code and wanting to find out what's going on I just stick in the plain old debug print statement:

  (prn "i: " i)
But, notice the redundancy, I type "i" twice. So I wrote a macro "erp" (short for "stderr print"), which prints to stderr its argument, both literally and what it evaluates to. And, it returns the result as well, so it can be inserted in the middle of some code.

For example, if I'm looking at

  (+ 3 (/ 4 2) 5)
and thinking, "wait, what is (/ 4 2) evaluating to again?", I stick in erp:

  arc> (+ 3 (erp (/ 4 2)) 5)
  (/ 4 2): 2
  10
  arc> 
I find that most often just seeing the literal expression that's being printed gives me enough context to tell me which one it is.

  (mac erp (x)
    (w/uniq (gx)
      `(let ,gx ,x
         (w/stdout (stderr)
           (pr ',x ": ") (write ,gx) (prn))
         ,gx)))


10 points by drcode 5847 days ago | link

I would also point out that you can use a colon, which means you don't have to muck with parenthesis when inserting/removing the debugging code:

  (+ 3 (erp:/ 4 2) 5)

-----

3 points by CatDancer 5847 days ago | link

Very nifty!

-----

1 point by projectileboy 5841 days ago | link

Thanks for this! Very handy.

-----