r/Clojure • u/yogthos • Dec 14 '19
Hashp is a better prn for debugging
https://github.com/weavejester/hashp4
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.corenamespace before the#pdata reader is used, it should work in any namespaceI 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
3
3
2
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.
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 andprintlns, but also somedefs, too, that I like to sprinkle in sometimes to snatch intermediate results for repl inspection:How about a
#dthat would maybe print and def that to something clever and easy to access?Edit:
#tto print some timing information (idea from a colleague)#sor#fto spit it to /tmp file