r/Clojure • u/robotdragonrabbit • Sep 29 '25
is "working only by accident" a common feeling in clojure codebases?
I have joined a clojure team 6 months ago. Coming from an elixir project, where we valued being explicit in our code (e.g. never ignoring errors, returning the response code, using descriptive function names, matching on expected values exactly). I have learned clojure prior to joining my current team, but this is my first big project and I am surprised to see how often the code relies on implicit truthiness of values and similar constructs. This often makes me feel like the code only works by accident and if I slightly modify a function, I can't predict what will break
One good example is the use of `(seq ...)` over `(not (empty? ...))`. From what I understand the original purpose of `seq` isn't to check for non-emptiness, and I always have to double check the edge cases. I know this is considered idiomatic, and this is exactly what makes me wonder if similar patterns are common in the clojure community
At my previous (elixir) job we would compare to `[]` directly, and would not accept `nil`. This might sound more brittle, but actually gave me more confidence in whether e.g. returning a string is correct in this situation.
Of course we have much more complex values, and some logic may be applied via `(when (:some-field data) ...)` but data comes with some-field set to nil, false and without some-field at all. This is when I feel like some code paths are only working by accident and not by design.
Is this a common phenomenon in clojure project or is it just my team?
----
EDIT
Thanks everybody who took the time to answer! There was a few clarifying question, but I wouldn't waste your time answering them, as I'm not trying to solve a specific problem, just checking the vibes
Worth highlighting that elixir is also a dynamically typed language with the same truthiness rules, but it used differently in my experience
My conclusion is that there is some philosophical differences between people using these languages (but it's always a tradeoff of course!) as well as pattern matching being more prominent in elixir, whereas some people recommended libraries like malli to solve the same problems in clojure
24
u/JoostDiepenmaat Sep 29 '25
There are no edge cases to using
seqfor testing emptyness --empty?is implemented as(not (seq x)), ignoring an optimization for counted collections.Clojure has a very simple and clear rule for what is considered "logically true/false" -- anything that isn't
nilorfalseis considered true. See the docs for if. Some of the problems you perceive may be just because of unfamiliarity.The feeling you're describing can also be caused by being unclear about what the expectations are:
(when (:something data) ...)is perfectly fine, if the intention is that nil, false and missing key should be treated the same. Where it gets tricky is when some of these cases are not expected. Idiomatic clojure tends to take a "garbage in, garbage out" policy for brevity and performance reasons, using assertions, preconditions and/or specs (all of which can be disabled in production) to ensure correct behaviour. This means you have to make sure that garbage does not go in, by verifing unpredictable input (in some eariier processing step) or your have to be very explicit about how input is handled (i.e. usecontains?orfindinstead ofgetor(:key m)to test for existence of a key.8
u/PoopsCodeAllTheTime Sep 30 '25
Here you go, the definitive answer to your inquiry:
https://ericnormand.me/podcast/what-is-nil-punning
18
u/Simple1111 Sep 29 '25
I’ve been using clojure for 10 years and on a decent sized project with it in my day job for 5. What you are expressing really sounds like an unease with the paradigm shift from typed to dynamic. It can be jarring and trigger intuitive warnings you might have built up but it’s not wrong.
It’s a trade off. The possibility of the failures you are concerned about exist but in practice I find they are rare. The ease of writing and reading code is worth it imo. Clojure also has utilities for and a general ethos around structure ingestion and schema enforcement at boundaries. This really seems to do the job.
10
u/p-himik Sep 29 '25
What does it mean? How can a value implicitly be truthy?
It's a dual-purpose function. So checking for non-emptiness is one of the original purposes. That being said, nothing stops you from using
(not (empty? ...))- while the docstring ofempty?recommends against it, it's just a docstring, not police. The worst that could happen is that someone else would be a bit stumped by it and might rewrite it as the idiomatic(seq ...).What are the edge cases with
(seq ...)?Different code paths will care about different specifics. If a particular path requires for
(:some-field data)to not be falsey, then(when (:some-field data) ...)is a perfectly fine check. Some other code path might care that(:some-field data)is specifically notnil. Yet another might care whether:some-fieldis present indata, regardless of the value. But in general, if encoding stuff like this can be made via additional fields or structures without making anything worse, it's probably worth it to rely on them instead of relying on a much more implicit mix ofcontains?,nil?,false?, etc. In other words, if there's a conceptual enumeration with more than two states, it makes sense to make it an explicit enumeration of states instead of an implicit combination of sentinel values and presence.5
u/[deleted] Sep 29 '25
Clojure is a lisp and has inherited it’s philosophy. In lisps an empty list actually is nil. So there’s nothing implicit here. If thats seems not explicit enough you might want to read about nil-punning. It‘s a very stringent design
8
u/Marutks Sep 29 '25
Elixir is dynamically typed and it has the the same “truthiness” concept. 🤷♂️ Clojure developers often use some schema to validate data.
4
u/UnitedImplement8586 Sep 29 '25
Maybe reading about nil-punning makes you think differently. I would say give it a try.
4
u/drcforbin Sep 29 '25
Using
seqthat way is idiomatic in clojure. It's even in the guidelines7
u/lgstein Sep 29 '25
There is no problem with any of that. What you describe is not "working by accident", but very much considered by any experienced Clojure programmer.
4
u/eraserhd Sep 29 '25
I think it may be that you are missing something really neat about Clojure. In most languages, dynamic or typed, you have to write separate functions for lists, arrays, vectors, for doubles or bigints, etc. In Clojure there are protocols that allow us to have far fewer functions.
In order to do this, we need to understand what the abstractions are, e.g. seqable? So we don’t think, “We have a list or vector or nil or omg what if they pass a tugboat,” we think, “We have a seqable?.”
When you have a seqable?, checking with seq seems natural.
2
u/Dead_Earnest Sep 29 '25
You are just used to types. Correctness should be checked with tests, type safety is a superficial check.
If your data is complex enough that there are bugs, you should use schema lib - malli. It's much more powerful / flexible than a type system.
In short, good coverage with schemas + tests = high reliability. Otherwise a program is unreliable in any language. Static typing only enforces superficial reliability, at the cost of tons of boilerplate and puzzle-solving.
1
u/didibus Sep 30 '25
Clojure does nil punning, where
nilis like a languagepun:So functions that expect false also accept nil. Those that expect seq also accept nil. And those that expect coll also accept nil, and treat it for the puns I mentioned above.
In turn, when people implement functions that return boolean, seq, or coll, they'll tend to be ok with it returning nil as well, and continue the nil punning.
I agree this can feel a bit less explicit about nil, I'm not sure which ends up being nicer in the overall.
Beyond that though, I think Elixir and Clojure differ less than it appears, what makes it feel more different is that in Clojure collection functions and sequence functions are in the same namespace (aka module). Where-as Elixir puts them in
EnumandStreammodule, and then map functions inMap, and so on. In Clojure all that is just mixed together onclojure.core.For example in Elixir:
And in Clojure:
All the
EnumandStreamfunctions in Elixir also accept most things, evenstrings(aka charlist). It's just that Elixir also has a binary string that is nicely unicode compliant and all, and that one is not treatable as a list. So the difference with string is more a matter of the underlying platform and string handling. Java has only string as an array of chars, but Beam has both charlists (which are similar), or a binary unicode representation which is lacking in Java.So here I think it's because the Elixir pattern matching is over concrete types, that you end up feeling it's more explicit. In Clojure two things happen:
Elixir couldn't match over Enumerables generically, because the pattern matching is at the VM level, and matches aginst the concrete memory layout, so it has to be type specific to benefit from that super fast VM pattern matching.
0
u/partosq Oct 04 '25
"Collections or other things that can produce a sequence are seqable. Calling
seqon a seqable will return either nil or a sequence with at least one element. Because of this behavior, it is idiomatic to useseqas a terminating condition check (aka nil-punning) while iterating a sequence:..."https://insideclojure.org/2015/01/02/sequences/