r/Clojure • u/bowbahdoe • Dec 08 '22
How to Structure a Clojure App 101
https://mccue.dev/pages/12-7-22-clojure-web-primer3
u/pihkal Dec 09 '22
I like how this dives into the distortions necessary to support IoC initialization and testing.
A suggestion: you can avoid the need for ^:dynamic, binding, a WikipediaClient, or cluttering up your component/integrant system just for tests by using dynamic-redef. It temporarily replaces the root binding of a var with its own machinery, so it's like a thread-safe with-redefs.
It's very handy for when you want to redefine a fn for testing, but not in production, and don't yet have state concerns (otherwise you'd use a record). I talk about it more in this blog post.
It has some limitations, but when it fits, it's much simpler than the state-based alternatives.
1
u/bowbahdoe Dec 09 '22 edited Dec 09 '22
I mean
dynamic-redefdoes seem to solve the multi-threaded issues withwith-redefs. Thats pretty cool.Those really weren't the issues I cared about though. Its just the "stubbing at a distance" - where you need to read all the way down the code to know what to redef - that makes it unappealing.
I think the wikipedia example might be too simple for its own good. For most "service clients" that you would actually want to avoid calling in tests, there is often something you could reasonably put into an object (like the api key or whatever.)
Actually making a protocol/fake impl is something that can be done "on demand" with just that foundation.
(And like I imply in the sidenotes - "its visible at the callsite what is used further down" is the benefit I care the most about)
2
u/pihkal Dec 09 '22
Those really weren't the issues I cared about though. Its just the "stubbing at a distance" - where you need to read all the way down the code to know what to redef - that makes it unappealing.
That's true, but if I have to have ugliness, I'd rather have it in the tests than the main code. I prefer mocking out a fn low down on the test side over complicating the main code with test-driven protocols/records/components/parameters.
It makes the tests more fragile, but makes main code more robust, which is a trade-off I'll take any day.
1
u/aHackFromJOS Dec 11 '22
Title should really say “web app.” I know web development is popular but there are various sorts of other apps, and this post is specifically about web apps.
2
u/bowbahdoe Dec 11 '22
Can you elaborate more on how it would be different for other kinds of apps? I don't doubt it would be (and regardless my title is bad in exactly the way you describe), but I don't have much experience outside of web.
2
u/progrummer Dec 11 '22
I'm very new to Clojure. I'm at the point where I'm moving from fundamentals to trying to create the kind of applications I know how to make in Go/JavaScript/Python.
This article was incredibly helpful. It was well written, had an excellent pace, and assumed very little knowledge. I hope you have more of these 101 style articles planned!
Thanks again!
4
u/Hrothehun Dec 08 '22
Hey, nice article!
My experience as a student and as an intern during summer was mostly with JavaScript, and when I started learning Clojure, this reliance on these "state management" systems seemed overcomplicated and alien to me.
But yeah, showing each problem as they arise during a construction of a working system and the possible solutions to mitigate these problems is actually eye opening. I think we need more of these 101 type posts to be more inviting to new and inexperienced developers such as myself.
Good job!