Clojure is a dynamic, functional programming language that runs on the Java Virtual Machine (JVM). It emphasizes immutability and provides robust concurrency primitives.
Clojure Anti-Patterns Overview
Using Mutable State Unnecessarily
Not Using Threading Macros
->
, ->>
, as->
, etc.) to make code more readable by avoiding deeply nested function calls. Threading macros make the data flow explicit and easier to follow.Using Java Collections Instead of Clojure Ones
Using recur Instead of Higher-Order Functions
map
, filter
, reduce
, etc., over manual recursion with loop
/recur
. Higher-order functions are more declarative and often more readable.Not Using Destructuring
Using Reflection
Not Using Transducers for Composition
Using Dynamic Vars Unnecessarily
^:dynamic
) for configuration or state that could be passed explicitly. Dynamic vars make code harder to reason about and test.Not Using Proper Exception Handling
try
/catch
/finally
for operations that might fail, especially I/O operations. Consider using libraries like ex-info
and ex-data
for structured error handling.Not Using Specs for Validation
clojure.spec
for data validation and documentation. Specs provide a declarative way to define the shape and constraints of your data.Using Keywords as Functions Excessively
:name user
) is idiomatic in Clojure, excessive use can make code less readable. Consider using destructuring for clarity.Not Using Proper Namespaces
Using nil Punning
Not Using Proper Testing
clojure.test
or other testing libraries. Tests help ensure your code works as expected and catch regressions.Not Using Proper Documentation
Not Using Component Lifecycle Management
Not Using Proper Error Messages
ex-info
to include structured data with exceptions. This makes debugging easier and allows for programmatic handling of specific error conditions.Using def Inside Functions
def
inside functions to create or modify global variables. This leads to side effects and makes code harder to reason about and test.Not Using Proper Dependency Management
Not Using Proper Concurrency Primitives
Not Using Proper Performance Optimization
criterium
for benchmarking and YourKit or VisualVM for profiling.