9

I've been using Emacs for quite a number of years now, but I only recently stumbled over the coding standards. There it is stated:

Don't make a habit of putting close-parentheses on lines by themselves; Lisp programmers find this disconcerting.

This is exactly the habit I've built up, since I find the code to be easier to read. I see mixed styles in the packages delivered with Emacs. My question is if this disconcert is common to most elisp programmers.

Edit: Looking at my code, I'm not strictly following this. Then the code would be quite horrible. It's mostly defun, let, if and so forth. But, it seems to be against the grain, so I should probably stop doing this. Cheers for the input.

(Borderline opinion based, but it seems related to closing braces in C++, where it shouldn't be controversial to state that they generally should be on their own lines).

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 4
    Not unacceptable, just really annoying:) – abo-abo Dec 02 '14 at 11:18
  • 4
    Think of it like a whitespace language, except brackets remove possible ambiguity - in well formatted lisp you should be able to read it without looking at the brackets – Squidly Dec 02 '14 at 11:26
  • 1
    I'm curious as to which packages delivered with emacs don't adhere to this. – Malabarba Dec 02 '14 at 12:49
  • 3
    @Malabarba A search with `ag` on the lisp sources gives me 3414 candidates for the `^\s*?\)$` regex which make up 468 files which belong to about 200 packages? – wasamasa Dec 02 '14 at 13:03
  • 1
    @wasamasa Good idea. I put that through a couple more pipes, and got that 90% of the `.el` files have less than 10 instances of this. Meaning these files don't adopt it as a style, they just use it at some convenient locations (like to finish off *really* long functions). – Malabarba Dec 02 '14 at 14:53
  • 1
    FWIW - I think nearly every new Lisp user who is used to a language where the style you mention is common starts out writing Lisp code that way. I've never encountered any that continue doing so, however, after they get used to using/reading Lisp code with Emacs (`show-paren-mode` etc.). – Drew Dec 02 '14 at 15:00
  • @Drew: I've been coding in elisp for well over 15 years... – Meaningful Username Dec 02 '14 at 15:34
  • So I guess now I've encountered one. ;-) That's one in 30 years, FWIW. Nothing wrong with that. You asked about "*most Elisp programmers*". I cannot say whether most are bothered by standalone right parens, but based on my experience, most do not use them. – Drew Dec 02 '14 at 18:15
  • @Drew: I shall henceforth withdraw my usage of this much maligned style :). – Meaningful Username Dec 02 '14 at 19:14
  • I would say use whatever you and your readers are most comfortable with. Do not fear the *Parenthesis Police*. ;-) – Drew Dec 02 '14 at 20:57
  • @Drew: I'm quite picky when it comes to e.g. C++ style. Doing what most people do, if it's not against everything one believes in, is a good thing when it comes to style. – Meaningful Username Dec 03 '14 at 08:48

3 Answers3

7

Well a short answer is "there is no need". As Emacs can parse the s-expressions of LISP it knows exactly where the forms balance out and can re-indent the code correctly. In this case putting moving a bracket down to it's own line simply wastes an extra line of space and at the same time is non-idomatic and jarring to other LISP programmers who are used to the canonical style. As has been mentioned in the comments this doesn't preclude you from using vertical white-space to visually separate chunks of code.

In practice a lot of Emacs using LISP programmers use a variety of additional visual cues like show-paren-mode that help navigating through a dense nest of closing parenthesis. Engineers that have subsumed modules like paredit and smart-parens into their editing cycle have become at one with the AST and are simply manipulating its structure directly in a way that expressions are always balanced and complete.

stsquad
  • 4,626
  • 28
  • 45
  • Of course I have parenthesis matching and so forth, but I think vertical space has a place in all code, and that is what this non-recommended habit brings. But a common style is always good, for anything public I would use the recommended way. But to turn it around, what value does the mass of parentheses at the end bring :). – Meaningful Username Dec 02 '14 at 14:21
  • 4
    You can always add vertical space; nothing says you have to smoosh each line up to the next. Sometimes vertical space is useful; sometimes it's not. Putting the parentheses at the end of the line lets you choose when to add vertical space and when not to. Parens on a new line forces vertical space, and a lot of it. – zck Dec 02 '14 at 14:33
  • @zck: I've updated the answer to mention your point. Thanks. – stsquad Dec 02 '14 at 16:30
  • Good point. I've started to change my code to conform to the standard. Not much difference, so should be quick to get used to. Guess I just felt the parentheses should be used for something, whereas most others seem to pretty much ignore 'em. – Meaningful Username Dec 02 '14 at 17:52
7

Riastradh's Lisp Style Guide elaborates a bit more on this specific topic. While he generally discourages from placing closing parentheses on their own line, he recognizes several exceptions, such as a previous line with a closing paren being commented out and really long lists that would otherwise constantly mess up diffs when adding new elements at their beginning or end.

wasamasa
  • 21,803
  • 1
  • 65
  • 97
3

I've been writing Elisp for about 2-3 years now. At first, I was doing the hanging parens thing, but since I've discovered the rule that you mentioned, I've started doing it myself. And I can say now that it's much easier to read the code when it's properly styled.

I even wrote some LISP prettifying code here: lispy-tab.

And, of course, I have show-paren-mode on all the time.

abo-abo
  • 13,943
  • 1
  • 29
  • 43