REPL Based Debugging in Clojure
--
We’ve spoken in previous posts, about why we have chosen Clojure and how important it is to our engineering culture. In this post I’d like to focus on some of the basics of Clojure — debugging. Clojure is a dynamic and functional programming language that provides some powerful tools out of the box for debugging. One such tool is called REPL (Read Evaluate Print Loop) that enables developers more code clarity by making it possible to find the source of bugs much more quickly, and ultimately understand the code and flow better.
By using the REPL, it’s much easier to interact with a running Clojure project. In fact, Clojure as a community promotes a REPL First Development mentality as a best practice (AKA REPL Driven Development).
As a prerequisite to this post, you will need to get REPL setup.
Once you have your REPL up and running, you can start to play around with your code.
One of the bonuses with using REPL is that it enables you a number of different ways to debug your code more efficiently, some of these include:
- The Stack Trace
- Code Inspection with Prints & Logs
This post will dive into how to best leverage REPL in these different scenarios, with real code examples.
Let’s get funky! Lisp, Clojure and Scala are just some of our functional jive. Join us! >>Learn More
Stack Trace Debugging using REPL:
Since Clojure is compiled to Java bytecode and runs on the JVM, many times, if an exception is thrown, it will be a Java style exception. The stack trace provides valuable information for you to be able to identify the source of the exception, including where the exception occurred, why it occurred, what was the flow that led to this exception (among other data points).
Reading the exceptions, and probably even more importantly, actually understanding them (while this can be a tedious process) is the key for tracing and tackling the actual issue.
And all that said, it is quite common for exceptions in Clojure to not really “point” to where the actual issue is.