понедельник, 29 мая 2017 г.

(= (+ clojure emacs) :happiness)

I started programming in Clojure quite long ago and despite all the great advice spread through the Internet kept using Sublime Text editor for it (it's an awesome thing, anyway). However some months ago I stumbled upon Emacs - maybe after talking to some Haskell-ists. Yes, it felt totally unfamiliar and awkward and I had to spend minutes trying to accomplish the simplest things that took me an instant to do in other editors. Still, I can't explain why, it felt quite likeable and more suitable for doing Clojure. The funny part though is that I'm only starting to learn the full power of Emacs as a Clojure programming environment. Literally, it took me almost half a year to get accustomed with the editor and its addons - mostly Cider - to understand how much one can do there.

Even though there are guides on the web explaining what a Clojurist can do in Emacs (for example here and here), I feel the need to list the basic tricks that I have learnt here. For some it may soften the learning curve, others will possibly provide some greate advice to me. And it will certainly help me if I somehow got struck by amnesia in these dangerous times.

First things first. Once I run Emacs to do some Clojure or ClojureScript I make sure to press C-c M-j or C-c M-J (for cljs) to launch a REPL and have it ready. Most of the cider's awesome stuff won't work without that, but it is also simply stupid to do Clojure without having a hot REPL nearby.

At first I used to copy and paste lines from code buffers to REPL and run them there, when I wanted to check whether something would work. Quite recently I discovered that this is stupid as well. One can simply place a cursor at the end of an expression and type C-c C-e - cider will evaluate the expression and show the result right next to it!

I had some problems with that though, because once I wanted to copy the result of that expression elsewhere and couldn't figure out a way to do that. Fortunately, one can get pretty close: C-c C-p will also evaluate the last expression, but the pretty-printed result will appear in a special buffer. Afterwards you can go to that buffer, skim through it and copy the required bits.

The last time I used that trick my goal was to select some values from the database (yes, with Clojure, because with Emacs it's closer that any other gateway to the DB) and use them as parameters to test my web-service. I made calls to that service through a plugin for Google Chrome (yes, not familiar with curl), but I should definitely change that to running requests right from Emacs itself - that should be pretty easy. Frankly speaking, I don't know any other development environment that allows to make a couple calls and check whether a webservice that you're hacking together yields the expected results without even leaving the window (correction: buffer) in which you write the service's code.

The last big thing on my list are tests. I used to run tests through the terminal, like lein test. Yes, that means spinning up a JVM, which is 15+ seconds at best. Try to imagine my feelings when I first discovered that I can run all tests in a namespace simply by pressing C-c C-t C-n, while there. Even better, I can run the tests that I have for one of my modules (i.e. namespaces) without leaving the buffer of that namespace - with those same keys. This one, however, requires that the namespace with tests is called <the thing that you're testing>-test - it took me some time to learn and get accustomed to, but that's not a big deal. Also, while the cursor is on a particular test case I can run it with C-c C-t C-t. Then change it and run again in an instant. As a C# programmer I love Visual Studio, but unit tests experience there is by no means close to what you see in Emacs with Cider.

Sometimes I would also type C-c C-x just to see that my code still compiles - that's cider refresh, which reloads everything.

And one more thing, that makes cider-ed Emacs a true development environment is the M-. hotkey, that navigates you to the definition of whatever you're looking at. It does have glitches from time to time, but it's there.

There's a ton of other greatness in Cider, I'm sure. Many bits are easy to find and learn through the hotkeys reference C-h C-m - I simply didn't internalize most of it yet. For example, there are keys and commands for macro expansion and multiple other things. Still, even with the few tricks that I mentioned above I get programming experience that simply seems out of reach for the other languages and environments that I worked with. If you do Clojure and your environment of choice doesn't give you the same capabilities, you should definitely check out Emacs!