Clojure is in fact a trinity - of language, interactive development, and structural editing

You can never really learn Clojure if you only focus on the language. Why? Because Clojure is in fact a synergetic combination of three things: the language itself, a way of working centered on interactive development, and structural editing support in editors for manipulating the code safely and efficiently. You cannot get the full benefits of Clojure unless you embrace all three.

The language

Clojure syntax

Learning the language of Clojure - its syntax and the core library - is the easy part. Learning to write functional code and create functional systems is a bigger challenge but still something newcomers are aware of. (And something 📖 Grokking Simplicity can help with.)

Interactive development

Interactive development demo video

Understanding the productivity benefits of interactive development and adopting it as you primary way of working is harder. Many beginners fail to see it for what it is and consequently fail to adopt and leverage it. They think "I know REPL from Python" and stop there. But interactive development is the opposite of the write-compile-test cycle from mainstream languages. It is about developing - or more accurately "growing" - your application while it is running, without ever restarting it, without ever losing its state, and with full access to said state, database connections etc. It is about trying every tiny piece of code immediately in the REPL. If you are new to this, REPL means Read-Eval-Print-Loop, and it is kind of a "programming terminal" connected to your application, where you send new code, run functions, and inspect data. It is also about "productivity hacks" such as capturing the arguments of a function so that you can then invoke it in isolation directly from the REPL, without having to go through all the surrounding code.

Check out my video Demonstrating the Clojure way of developing functionality: REPL-driven, from inside out to see how this works. Feel free to play it at 1.25 or 1.5 speed :-).

Structural code editing

Paredit Slurp Forward (from Calva docs)

Last but not least is structural code editing support, represented by the older Paredit and newer Parinfer. They turn parentheses from something confusing to a productivity boost. Essentially, they provide a few commands, bound to hotkeys, for the most common code edit operations. The most used ones are selecting the next surrounding unit of code, moving the closing parenthesis one expression left to throw the last expression out of it ("barf") or moving it right to get it in ("slurp"), and "raising" an expression so that it replaces its parent. There are more but these four are all you really need (perhaps with an occasional use of "split"). Here are my top 6 VS Code keybindings that include structural editing and REPL evaluation commands.

You may think that you might wait with learning this until you master the language and the REPL. Don’t. Just stop whatever you are doing and spend the next 10 - 20 minutes learning these 4 commands and shortcuts.

You don’t want to be messing with characters and to be responsible for maintaining the balance of your parentheses and quotes manually. Save yourself a world of pain, invest a little time now in learning structural editing, and get it back many times over in increased productivity.

Warning: Don’t try to learn all of Paredit, it would be overwhelming. Learn just these 4 commands and cut + paste, and perhaps how to turn Paredit off for when you messed up your parentheses and need to fix them manually.

Note: In the name of honesty I must confess that some Clojure developers disagree with me:

Well, it was just too many things to learn at once. I’m not at all saying one shouldn’t learn it straightaway. But I do want to suggest things like Paredit (or Parinfer) in a way that doesn’t make them feel they need to learn it. So, although I agree with your "You cannot get the full benefits of Clojure unless you embrace all three" going by the letters of the sentence, it doesn’t feel right to say this to a beginner in-particular.

some other (experienced, i.e. very far from a beginner) cannot live without strucutral editing:

Paredit is clearly one of Clojure’s super-powers that is not talked about as much. I think it might be more important to me than immutability. Many struggle with parentheses when they start with Clojure. For me it is the opposite precisely because of paredit.

Conclusion

To really understand and master Clojure you need to learn the language, embrace interactive development, and adopt structural editing. And you need to do all three in parallel. Do it and you will thank me.


Tags: clojure productivity


Copyright © 2024 Jakub Holý
Powered by Cryogen
Theme by KingMob