Author Topic: Lisp Code Formatting  (Read 7569 times)

0 Members and 1 Guest are viewing this topic.

Ben Clark

  • Newt
  • Posts: 94
Lisp Code Formatting
« on: December 28, 2017, 02:50:26 PM »
Do most of you elite lispers use the "Format code in Editor" format provided by VLIDE? Or have you developed your own style and rules over time?

Somewhere along the way I decided to always tab instead of double space indent, now I'm realizing my code looks less readable and more goofy than code that is double space indented.

I've noticed a few others:


I always write setq like this:
Code: [Select]
(setq
a 1
b 2
c 3
)
but some people do this:
Code: [Select]
(setq    a 1
b 2
c 3
)

Some people close their functions at the same indention as the opening as above, but some do this (I'm not a fan):
Code: [Select]
(setq
           b 2
           c 3)

Then there are the dreaded "non-indenters".... my oh my....




I'm thinking about just going with the VLIDE formatting provided because it will give consistency with others' code, but it will be hard to change my ways... If using double space indent, is there a good shortcut way to do it when you're really deep in nested functions?

Thoughts?
« Last Edit: December 28, 2017, 02:58:12 PM by Ben Clark »

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Lisp Code Formatting
« Reply #1 on: December 28, 2017, 03:03:11 PM »
In programming, you often start defining your own style. I did for the languages I program(ed) in. If you get involved in a large colaborative project, you will often have to follow a certain standard and third party tools can help re-format your code to follow those standards (however, those are usually for the C based lanugages and usually frowned upon).

Lisp is OLD and the one you are not a fan of is the way it has been done for a very long time and many, many lispers (not so many autolispers) follow that style. So, if you get into other lisp dialects, you had better get used to that style. 
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Lisp Code Formatting
« Reply #2 on: December 28, 2017, 03:10:54 PM »
Just sharing my preference:

Although I know about that functionality to assign multiple variables to symbols in one go,
I always prefer'd to use multiple times the setq function so then I could easily trace whats being/or not/ localised.
By just double-clicking in the notepad++ it highlights multiple instances of that word.  :-)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Lisp Code Formatting
« Reply #3 on: December 28, 2017, 03:15:34 PM »
I'm thinking about just going with the VLIDE formatting provided because it will give consistency with others' code
i believe one should stick to "VLIDE formatting" not because of "consistency" but because it saves huge lots of time

Ben Clark

  • Newt
  • Posts: 94
Re: Lisp Code Formatting
« Reply #4 on: December 28, 2017, 03:22:33 PM »
Lisp is OLD and the one you are not a fan of is the way it has been done for a very long time and many, many lispers (not so many autolispers) follow that style. So, if you get into other lisp dialects, you had better get used to that style.

I suppose I will then. It makes it look somewhat more like Python, which is good in my opinion. It's also interesting that this is not how the VLIDE formats it.



Just sharing my preference:

Although I know about that functionality to assign multiple variables to symbols in one go,
I always prefer'd to use multiple times the setq function so then I could easily trace whats being/or not/ localised.
By just double-clicking in the notepad++ it highlights multiple instances of that word.  :-)


Interesting. I've somehow become obsessed with using as little code as possible to perform a task. So something inside of me rejects this. I do see the utility of doing it that way. I don't use the Notepad++, though.



I'm thinking about just going with the VLIDE formatting provided because it will give consistency with others' code
i believe one should stick to "VLIDE formatting" not because of "consistency" but because it saves huge lots of time

I do enjoy saving huge lots of time...

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Lisp Code Formatting
« Reply #5 on: December 28, 2017, 03:47:41 PM »

Interesting. I've somehow become obsessed with using as little code as possible to perform a task. So something inside of me rejects this.


'as little code as possible' depents on the coding style... and on list manipulation skills, say somewhere you might use:
Code - Auto/Visual Lisp: [Select]
  1.   depth 5
  2.   width 60
  3.   len 140
  4.   thickness 1
  5.   material "Concrete"
  6.   elevation 300
  7. )

And then you'll need to localise 6 symbols, while one with a style of this:
Code - Auto/Visual Lisp: [Select]
  1. (setq L
  2.   '(
  3.     (depth 5)
  4.     (width 60)
  5.     (len 140)
  6.     (thickness 1)
  7.     (material "Concrete")
  8.     (elevation 300)
  9.   )
  10. )

Would need to localise only the 'L' symbol.

Lee Mac is a good example for how many different techniques one could apply to shorten his code (aswell the acronyms for the symbols).
But the cost is: are you good enough to understand what happened in his code.  :laugh:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Lisp Code Formatting
« Reply #6 on: December 28, 2017, 05:55:50 PM »
That's basic programming; and, if you did the first in autolisp programming you'd risk being smacked with a trout. Association lists are a staple.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Ben Clark

  • Newt
  • Posts: 94
Re: Lisp Code Formatting
« Reply #7 on: December 28, 2017, 07:23:24 PM »
Code - Auto/Visual Lisp: [Select]
  1.   depth 5
  2.   width 60
  3.   len 140
  4.   thickness 1
  5.   material "Concrete"
  6.   elevation 300
  7. )

Code - Auto/Visual Lisp: [Select]
  1. (setq L
  2.   '(
  3.     (depth 5)
  4.     (width 60)
  5.     (len 140)
  6.     (thickness 1)
  7.     (material "Concrete")
  8.     (elevation 300)
  9.   )
  10. )


That's basic programming; and, if you did the first in autolisp programming you'd risk being smacked with a trout. Association lists are a staple.


Neither of those are actually association lists... I think you have to have dotted pairs for that.

And besides, are you saying to never store things as variables, but only stores items in lists? (or risk being smacked with a trout?)

That seems overly rigid.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Lisp Code Formatting
« Reply #8 on: December 28, 2017, 07:36:21 PM »
Neither of those are actually association lists... I think you have to have dotted pairs for that.

Second one is - but its not structured of dotted pairs like we're used in DXF manipulation.
The accessor to such list is this:
Code - Auto/Visual Lisp: [Select]
  1. (assoc 'len L)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Lisp Code Formatting
« Reply #9 on: December 29, 2017, 03:41:01 AM »
I think you have to have dotted pairs for that.

AutoLISP only knows one type of data structure: the linked list.
It's the type of elements in the list that allows it to be used as a matrix, a tree, an association list and so on.
Any list whose every items are lists can be used as an association list.
The use of dotted pairs element of the association lists makes it possible to maintain coherence for the treatment of such lists as "dictionaries": you can use cdr to get the value of the 'key' entry that either a list or an atom: (cdr (assoc key lst)).

Code - Auto/Visual Lisp: [Select]
  1. (setq alst
  2.        '(
  3.          (10 5. 3. 0.)
  4.          (1 . "foo")
  5.          (2 "bar")
  6.         )
  7. )
alst is an association list because you can get each element using (assoc ...).
(cdr (assoc 10 alst)) returns (5.0 3.0 0.0)
(cdr (assoc 1 alst)) returns "foo"
but:
(cdr (assoc 2 alst)) returns ("bar") which is a list, we have to use (cadr (assoc 2 alst)) to get the value.
Speaking English as a French Frog

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: Lisp Code Formatting
« Reply #10 on: December 29, 2017, 04:12:54 AM »
The obvious thing for me is yes I am guilty no indenting, I wrote in Notepad copy-paste to command line as I go rather than spend an hour writing code throw into vlide and find the 5 mistakes. Its just me I lock up VLIDE so many times and have to crash out, if you can tell me where the secret button is I would use it more. The break on error, reset to top level etc only works to a point. I will try to find a good example bit of code.

I need to throw into vlide and let it arrange before posting to places like here or use Lee's lisp styler.

A man who never made a mistake never made anything

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Lisp Code Formatting
« Reply #11 on: December 29, 2017, 06:45:51 AM »
The obvious thing for me is yes I am guilty no indenting...

No one is judging you, BIGAL - the codes you write are yours. You know what you're doing and how to maintain/debug them.
I was just throwing an opinion about the 'as little code as possible' issue - that there should be more important things to worry about, than how one uses setq.
One could judge a guy like Lee the same way "man, its relatively short - but you are overcomplicating the code", but again he knows what hes doing and how to maintain/debug his programs.
Well.. if you code in a team with other guys, that might be a different thing.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: Lisp Code Formatting
« Reply #12 on: December 29, 2017, 07:04:43 AM »
There is an axiom that considerably more time is spent reading and re-reading code than is spent writing it ... so code MUST be readable.

just to throw a cat amongst the pigeons :...


http://carma.astro.umd.edu/nemo/pitp/papers/style.pdf



« Last Edit: December 29, 2017, 07:10:45 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Lisp Code Formatting
« Reply #13 on: December 29, 2017, 08:49:06 AM »
I'd say Michael Puckett is a good example for clearly-written code and Lee Mac for short-written.
Just my opinion, no offense to the rest of the guys and the impressive routines they have done.

I think from that book are listed the general accepted rules when coding, but you know everyone has different perception on the things (and with time it changes).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Ben Clark

  • Newt
  • Posts: 94
Re: Lisp Code Formatting
« Reply #14 on: December 29, 2017, 11:10:05 AM »
I think you have to have dotted pairs for that.

AutoLISP only knows one type of data structure: the linked list.
It's the type of elements in the list that allows it to be used as a matrix, a tree, an association list and so on.
Any list whose every items are lists can be used as an association list.
The use of dotted pairs element of the association lists makes it possible to maintain coherence for the treatment of such lists as "dictionaries": you can use cdr to get the value of the 'key' entry that either a list or an atom: (cdr (assoc key lst)).

Code - Auto/Visual Lisp: [Select]
  1. (setq alst
  2.        '(
  3.          (10 5. 3. 0.)
  4.          (1 . "foo")
  5.          (2 "bar")
  6.         )
  7. )
alst is an association list because you can get each element using (assoc ...).
(cdr (assoc 10 alst)) returns (5.0 3.0 0.0)
(cdr (assoc 1 alst)) returns "foo"
but:
(cdr (assoc 2 alst)) returns ("bar") which is a list, we have to use (cadr (assoc 2 alst)) to get the value.


Gile, thanks for schooling me on that. Learning about the way lisp stores data definitely clears a lot of things up. I'm learning.

John Kaul, sorry for trying to correct you when it was me who was wrong.





The obvious thing for me is yes I am guilty no indenting...

No one is judging you, BIGAL - the codes you write are yours. You know what you're doing and how to maintain/debug them.
I was just throwing an opinion about the 'as little code as possible' issue - that there should be more important things to worry about, than how one uses setq.
One could judge a guy like Lee the same way "man, its relatively short - but you are overcomplicating the code", but again he knows what hes doing and how to maintain/debug his programs.
Well.. if you code in a team with other guys, that might be a different thing.


Didn't mean to ruffle any feathers. I guess I shouldn't have written the line about the "non-indenters" ... If people could see all my code I'm sure it could be scrutinized in many ways.

I do however think that code organization is important and wanted people's opinions on the matter.