clojure-style-guide
clojure-style-guide copied to clipboard
No style given for try..catch
Two styles I have seen:
(try (f)
(catch E e
(g))
(try
(f)
(catch E e
(g))
2nd style should be preferred. try is like do or let in that all body forms should be broken onto the next line(s).
@arrdem is completely right. Guess we should add a rule about this.
Oh no, the style I've seen/am used to is:
(try (f)
(catch E e
(g))
That's the Emacs default!
It's also similar to the way it's done with do blocks.
It would be great to get this settled/codified.
@MicahElliott Well, both are standard Emacs indentations - the first layout is wider, the second is narrower (and I believe way more common). The second layout has the distinct advantage that it highlights better the special nature of do and try.
I guess I'd argue that a try is ideally a single expression, though I know there are cases where you do use multiple. As a single, it's a nice pattern to say (try (something) ... on a single line, but I know that's not the rule, so let's ignore my first suggestion. But it does feel wrong to prescribe that try should always be alone on its own line. We wouldn't want to say that for do I'd think (and certainly not when or let etc), since I've seen that very commonly as one-line.
My point was probably more to the notion that if you do a one-line try, you should indent the catch to match what's being tryd.
So my vote would be to not capture a prescription here for the style guide. It is an interesting case, though, where we could capture that there are a couple acceptable ways to do it, so as to avoid people changing existing code that's one or the other.
If I remember correctly, when I filed this issue I was thinking of a full-sized nested (f), not a short thing that could fit on one line. If the form being tried is short, indeed it could be written on one line, and the catch indented an extra 3 spaces to line up. If the form is longer, though, I wonder if it is ever acceptable to do the same? That's the question I believe I was aiming at, and apologies for that not being obvious due to my over-simplified example.