beginners

decentstates 2025-09-17T15:39:44.980169Z

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?

phronmophobic 2025-09-17T15:42:04.111819Z

I will often add an ;; else comment before the else statement 😅

➕ 1
🙌 2
p-himik 2025-09-17T15:42:45.238089Z

How do you format your if forms?

2025-09-17T15:42:50.971029Z

I am not aware of anything widespread. You can use , to mark things since it is treated as whitespace

Evan Bernard 2025-09-17T15:42:56.786729Z

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 book

☝️ 2
⭐ 1
Samuel Ludwig 2025-09-17T15:45:10.208669Z

very much agree with the above, the most idiomatic if-else is going to be a small if-else :^)

Evan Bernard 2025-09-17T15:46:14.298239Z

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:

decentstates 2025-09-17T15:48:35.905549Z

I format them like @ebernard mentions. The ;; else seems to work for me

Bob B 2025-09-17T16:50:39.850559Z

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).

hrtmt brng 2025-09-17T18:14:00.169469Z

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.

hrtmt brng 2025-09-17T18:22:07.591629Z

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.

Samuel Ludwig 2025-09-17T18:24:57.253249Z

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"

💡 1
Gent Krasniqi 2025-09-17T18:25:11.005169Z

Some other lisps by convention/tooling have different indentation for the if and else part. (4 vs 2 spaces)

Gent Krasniqi 2025-09-17T18:28:02.367149Z

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)

☝️ 2
exitsandman 2025-09-18T05:34:24.088159Z

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.

Samuel Ludwig 2025-09-18T13:42:51.955939Z

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 thing

Evan Bernard 2025-09-18T14:43:47.648009Z

oh yeah! https://clojurians.slack.com/archives/C053AK3F9/p1758133682367149?thread_ts=1758123584.980169&amp;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

exitsandman 2025-09-18T16:55:32.796629Z

yeah it's much like guard clauses, get the simple case out of the way so you can fully focus on the other one