r/Clojure • • Dec 14 '19

Hashp is a better prn for debugging

https://github.com/weavejester/hashp
71 Upvotes

12 comments sorted by

4

u/kynde Dec 15 '19 edited Dec 16 '19

Damn, that's marvelous. And works like a charm, too!

Here's and idea that came to my mind while trying that out and reaching the obvious conclusion that this will replace not only some prns and printlns, but also some defs, too, that I like to sprinkle in sometimes to snatch intermediate results for repl inspection:

How about a #d that would maybe print and def that to something clever and easy to access?

Edit:

  • #t to print some timing information (idea from a colleague)

  • #s or #f to spit it to /tmp file

4

u/mike_loughlin Dec 16 '19

Could it be integrated with tap>?

3

u/Bob_la_tige Dec 15 '19

So the library is being injected to work without requiring it, does it make it available in every namespace ? Can you use tool.deps to inject ?

5

u/weavejester Dec 15 '19

As long as you load the hashp.core namespace before the #p data reader is used, it should work in any namespace

I don't believe tools.deps has a mechanism for globally injecting code in the same way that Leiningen or Boot can do, so you would have to add (require 'hashp.core) somewhere in your code to ensure that the data reader works.

3

u/dustingetz Dec 15 '19 edited Dec 15 '19

How it works is data_readers.clj is a magic file, detected and installed into the compiler in a pre-compile phase. That's how the Clojure reader knows to expand #p foo into (hashp.core/p* foo) during read phase. But then your code will crash at runtime unless 'hashp.core is loaded. Or something like that, getting this stuff working for all possible ways it can be hit (compiler vs eval, clj vs cljs, etc) is a little complicated.

2

u/Bob_la_tige Dec 16 '19

In a deps.edn file you can eval some code, add this to inject hashp into the project.
:main-opts ["-e" "(require,'hashp.core)"]

3

u/Eno6ohng Dec 15 '19

Perfect, thanks!

3

u/[deleted] Dec 16 '19

if anyone is using timbre for logging, that library also has a spy macro that is very helpful.

1

u/stingraycharles Dec 16 '19

Been using it for ages! It’s such a pragmatic debugging tool.

3

u/ertucetin Dec 16 '19

Is it going to support ClojureScript in near future?

2

u/jvick3 Dec 14 '19

Totally gonna try this!

2

u/la-rokci Dec 16 '19

As noted in another comment this is similar to timbre/spy, which is indeed very valuable.

In general I see 2 problems that we debug

  • exceptions

  • unexpected results

Unexpected results are hard and that's where a debugger like sayid can help because it captures a lot of the surroundings and you can sift through it. I try to not get into these situations because it means you either overengineered your code and you no longer understand it or you failed to test the underlying pieces well enough. So instead of pulling out the debugger I try to improve my code to get rid of the whole situation.

Exceptions are the better case because they usually blow up closer to the problem. In this scenario a post-mortem debugger is the fastest way to get your hands on the problematic code. A new kid on the block is postmortem which I'll be trying out soon. The problem with simple debuggers I've seen is that they only capture arguments and return value but the logic usually breaks somewhere in the local variables. That's when you start printf-debugging, which is, let's be honest, clumsy.