clojure-style-guide icon indicating copy to clipboard operation
clojure-style-guide copied to clipboard

No style given for try..catch

Open a613 opened this issue 9 years ago • 6 comments

Two styles I have seen:

(try (f)
  (catch E e
    (g))
(try
  (f)
  (catch E e
    (g))

a613 avatar Sep 09 '16 15:09 a613

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 avatar Sep 09 '16 17:09 arrdem

@arrdem is completely right. Guess we should add a rule about this.

bbatsov avatar Oct 06 '16 12:10 bbatsov

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 avatar Feb 25 '20 21:02 MicahElliott

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

bbatsov avatar Feb 26 '20 09:02 bbatsov

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.

MicahElliott avatar Feb 26 '20 14:02 MicahElliott

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.

a613 avatar Feb 26 '20 14:02 a613