r/Clojure • • Dec 08 '22

How to Structure a Clojure App 101

https://mccue.dev/pages/12-7-22-clojure-web-primer
73 Upvotes

12 comments sorted by

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!

1

u/bowbahdoe Dec 08 '22

Thanks!

I take suggestions - what would you want a 101 type post on?

1

u/Affectionate-Bee6766 Dec 12 '22

A 101 on composing pedestal interceptors could be useful for many. Covering things like the right balance of plumbing vs business logic (eg the pedestal docs use it to add db connection but you can use for user transformation/validations or even everything up to returning your final response)

1

u/bowbahdoe Dec 12 '22

I mean - correct me if I'm wrong - but the benefits that pedestal interceptors have over regular function-composition middleware are:

  1. Dynamic Explicit Manipulation

  2. Async Friendliness

  3. (potentially?) easier debugging

While (1) is cool, I find it really hard to talk about (2) without going "well vthreads exist now so its probably a tad overengineered. peace" - yknow?

But yeah - I agree on the need. They confused the hell out of me for the longest time.

1

u/Affectionate-Bee6766 Dec 10 '22

Speaking as an ex-JS developer, even in JS you will start gravitating towards organizing your app like this if you want to avoid spaghetti dependencies all over the code base. Following a convention where every handle takes a ctx (or system as per this article) as first argument makes it straightforward and leaves a place to inject additional deps in future.

On the other hand, I can’t stand the Java Spring style auto importing dependencies style of injection, which you will surely come across at some point in your career :)

1

u/bowbahdoe Dec 10 '22 edited Dec 10 '22

I've written before about ways you can do this sort of injection in Java.

https://mccue.dev/pages/9-6-21-static-dependency-injection

It hasn't caught on because I'm just a lone voice in the crowd. You don't even see it listed in the options of more influential articles.

https://martinfowler.com/articles/injection.html

But I'm pretty confident you can pull it off even in that environment.

(sidenote, I think context is probably a better name than system in that ...context)

3

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-redef does seem to solve the multi-threaded issues with with-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!