Hi, I was wondering if there is an idiomatic way to make if else statements clearer, particularly being able to easily see the "else" part of the branching?
I will often add an ;; else comment before the else statement 😅
How do you format your if forms?
I am not aware of anything widespread. You can use , to mark things since it is treated as whitespace
imo, part of it is just getting used to the fact that if takes those two forms as args and to style them as such:
(if foo?
bar
baz)
beyond that, keeping the forms (`bar` and baz in my example above) as concise as one can helps readability. if you have large forms that extend a few lines vertically, it gets much harder to tell which is which. so, if there’s any processing you can do before the if, allowing you to return a symbol or a very short form (like a small, single-line function call), you’re winning in my bookvery much agree with the above, the most idiomatic if-else is going to be a small if-else :^)
yep. sometimes, though, i find it very difficult to avoid making a larger if/else than i’d like. in those cases, I lean on what @smith.adriane said https://clojurians.slack.com/archives/C053AK3F9/p1758123724111819?thread_ts=1758123584.980169&cid=C053AK3F9:
I format them like @ebernard mentions.
The ;; else seems to work for me
I don't know if I'd really recommend this, but another alternative would be using cond, and then you have to include something truthy as an else...
(cond (the-condition) consequent
:else alternate)
would also mean that if the else drifted, stuff would start breaking (which isn't superficially ideal, but it also means that the else is more likely to stay in the right place than a comment that can drift freely with no behavioral repercussions).I use the :else cond a lot. But never in situations with only one if branch. I think, the syntax of the if construct is only a question of getting used to it. We don’t read code like imperative algorithms and don’t try to write it so.
There are also other constructs like if-let, for (with :let), cond. They all are quite unique and special. And so the if is also special, and we should start feeling comfortable with it as it is. I would not try to make it look differently. The idea that the if construct is a basic thing, comes from other languages. In Clojure the if is a form like many other forms, and we must make our brain comfortable with it as it is.
honestly, the best way for me to think of ifs in clojure, is that it more closely resembles what most languages call a "ternary operator"
Some other lisps by convention/tooling have different indentation for the if and else part. (4 vs 2 spaces)
I sometimes swap between if/if-not so that the one liner is the first form and the longer form the second. (not saying that's a good thing to do, but this readability aspect does affect my decision)
Just push through the expectation of ALGOL-esque syntax and it'll click eventually. Also what the comment above mine says, i.e. the "then" path should usually be the short one where less things happen. E: unfortunate resemblance between ALGOL and something else.
also, consider if what you're trying to do would fit more appropriately in a when form! especially if your operation looks like
if <true>
do the main thing
else
do some token-step like returning nil or another noop-ish thingoh yeah! https://clojurians.slack.com/archives/C053AK3F9/p1758133682367149?thread_ts=1758123584.980169&cid=C053AK3F9 works great because you can drop the “ok, when does the else branch start…” question from your mental load rather than trying to hold it while reading the then branch
yeah it's much like guard clauses, get the simple case out of the way so you can fully focus on the other one