coding style -- parentheses placement

For the Compleat Fan
Locked
dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

coding style -- parentheses placement

Post by dukester »

Hi all....

Is it "good" style to write:

(set 'counter 1)
(map (fn (i)
............(println "Element " counter ": " i)
............(inc 'counter)
..........)
..........(sequence -5 5)
)

instead of:

(set 'counter 1)
(map (fn (i)
...........(println "Element " counter ": " i)
...........(inc 'counter))
...........(sequence -5 5))

NB. I'm using ........ above to preserve spacing

TIA....

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

There is no law of style.
And there are different tastes and opinions:

http://www.alh.net/newlisp/phpbb/viewto ... =formating

http://www.alh.net/newlisp/phpbb/viewto ... ght=indent
Hans-Peter

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Post by dukester »

HPW wrote:There is no law of style.
And there are different tastes and opinions:

http://www.alh.net/newlisp/phpbb/viewto ... =formating

http://www.alh.net/newlisp/phpbb/viewto ... ght=indent
Thanks for the references! This is good news indeed! I'm loving newLISP more and more as I get more acquainted with it. L8r....

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

I personally feel that it's more intuitive and easier to read to indent to the last open paren. Just like curly-brace-loving languages are easier to visually parse when the scope is indented, I indent once (two spaces, soft tabs) for scope and then to the first atom in the last unclosed list.

Code: Select all

(define (foo x y)
  (+ x y))

(map println '(foo bar baz
               bat)) ; bat should be lined up with the first char of foo
                     ; although you can't tell with variable width fonts :(
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I'm no coder - but I use both these ways of formatting (and some more, besides :-). While I'm starting work on something i'll use your first style more. For one thing, it can help getting the syntax right. Also, it's slightly easier to add or remove (or comment out) lines if they're each on their own line. After that I probably tend towards your second style: a more compact presentation (sometimes it matters if you're writing about code rather than just writing it). And a very Lisp-y look too, with all those parentheses holding on for dear life at the end of a function definition...

If you want to format your code automatically, have a look at Fanda's code analysis tools (http://intricatevisions.com/index.cgi?page=nlcode) for some ideas!

Locked