Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp Code Formatting

One of the people who took the time to comment on my other question about Clojure/LISP syntax pointed out that I had not written my sample code in the standard LISP way. So he was kind enough to rewrite the code snippet and that's a big help. But it raised another question in my mind. Why would this:

(if (= a something)   (if (= b otherthing)     (foo))) 

which is standard LISP formatting be preferrable to this form:

(if (= a something)   (if (= b otherthing)     (foo)   ) ) 

which is the way I would have naively formatted this code because of my C++ development background. I'm wondering if there's any benefit to the latter formatting or it's just an entrenched standard (like a QWERTY keyboard). I'm not trying to be argumentative--it's just difficult for me to understand why the first form would be preferable. The second form helps me to see the code structure more easily.

like image 944
Onorio Catenacci Avatar asked Feb 12 '09 14:02

Onorio Catenacci


1 Answers

The closing parentheses on extra lines don't really help with seeing the structure of the code, because you can get the same information from the level of indentation. However, the second form takes up almost twice as many lines, forcing you to scroll more often when reading code.

And if you need to inspect the nested parentheses more closely, an editor that highlights the matching parenthesis will help you. This will also be easier when the matching parenthesis is not too far away.

If expressions get too long and complicated to be easily read, it may also be a sign that you should extract part of the functionality into a separate function.

like image 193
Christian Berg Avatar answered Sep 19 '22 11:09

Christian Berg