Author Topic: any advantage to place a subroutine inside a main defun function ?  (Read 1795 times)

0 Members and 1 Guest are viewing this topic.

CatDance

  • Newt
  • Posts: 57
Normally the subr are placed outside of the main defun c:xxx

But I read some codes, the subrs are placed inside the c:xxx.
Is there any reason or advantage to place it inside a c:xxx ?
A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

Brief history of Microsoft
https://www.youtube.com/watch?v=BLaMbaVT22E

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #1 on: October 28, 2021, 05:38:45 AM »
If you don't want to occupy (atoms-family) base memory, then place sub functions inside main one... Otherwise, if you have subs that act like general sub functions and can be connected with many main (c:xxx) functions and you have no problems with building huge (atoms-family), then code it for acaddoc.lsp for auto load and then they'll be available all the time you need them... But IMHO, such functions would be then general and if you want to have independently working (c:xxx) function it's good to place them inside main - you can see and search for calls between them is single *.lsp file - no need for checking acaddoc.lsp or open subs as *.lsp files that are auto loaded with acaddoc.lsp through (load "*.lsp") function...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

mhupp

  • Bull Frog
  • Posts: 250
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #2 on: October 28, 2021, 10:12:56 AM »
Other then what ribarm said. If its a lisp you don't use all the time and not loaded with acaddoc.lsp. Having the subr inside the lisp that calls it insures the subr is loaded when the main lisp is loaded.
There is nothing wrong with having multiple lisp routines in one file. I have 20-30 lisps in one file that i use in a daily basis.
The only thing that could be a problem is if editing one of the lisp I miss a parentheses then they all wont load. Because of malformed list lisp error.
Then it could be a little hard to track down where the error is. Other then that I don't see what the problem would be having multiple lisp in one file.
I have even seen some subr in the middle of a lisp. it really doesn't matter as long as all the parentheses match up.

Maybe someone can correct me on this?

danAllen

  • Newt
  • Posts: 133
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #3 on: October 28, 2021, 12:12:38 PM »
I concur with both mhupp & ribarm. I have a utility file that has several hundred helper functions that I've collected or wrote over the decades. For catching errors with parenthesis I tend to write in a separate test file to quickly load & run until the code is right. And when I edit the main files I use my editors parenthesis matching function, in textpad it is ctrl+M and moves the cursor back & forth between closing brackets.

The downside is that when I want to share a solution to someone's question, I have to track down the multiple helper subroutines and include with the code. And this is usually too much of a pain so I don't help too often. So having all the subroutines in the main code avoids that problem.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #4 on: October 28, 2021, 01:06:11 PM »
For me, it comes down to a few things:
  • There could be other subroutines with the same names out there, this ensures the correct ones are called. This is especially an issue if you didn't write all of the LISP routines you use yourself.
  • Only loads the subroutines when you use the LISP routine, this helps reduce memory overhead. This isn't as much of an issue as it used to be back in the day, so this is really more of a carry over for those of us that have been doing this a long time.

mhupp

  • Bull Frog
  • Posts: 250
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #5 on: October 28, 2021, 02:56:29 PM »
There could be other subroutines with the same names out there, this ensures the correct ones are called. This is especially an issue if you didn't write all of the LISP routines you use yourself.[/li][/list]

All subroutines should have unique names even if they are doing basically the same thing. because the last subroutine loaded with that name will run when called in any lisp.

Example
load lisp1 has a subr named "test"
load lisp2
load lisp3 has a subr also named "test"

If you run lisp 1 its not going to run test subr from lisp1 its going to run subr test from lisp 3 because it was loaded last.
the sec you change something in that subroutine but keep the same name their is potential to screw up any other lisp that uses that named subroutine in.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #6 on: October 28, 2021, 04:29:44 PM »
There could be other subroutines with the same names out there, this ensures the correct ones are called. This is especially an issue if you didn't write all of the LISP routines you use yourself.

All subroutines should have unique names even if they are doing basically the same thing. because the last subroutine loaded with that name will run when called in any lisp.

Example
load lisp1 has a subr named "test"
load lisp2
load lisp3 has a subr also named "test"

If you run lisp 1 its not going to run test subr from lisp1 its going to run subr test from lisp 3 because it was loaded last.
the sec you change something in that subroutine but keep the same name their is potential to screw up any other lisp that uses that named subroutine in.

You could also use more powerful tools to help build your code files. You could replicate the same process that the C languages use, for example. In programming, organization is crucial!
https://www.theswamp.org/index.php?topic=37700.msg427343#msg427343
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CatDance

  • Newt
  • Posts: 57
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #7 on: November 02, 2021, 06:23:35 AM »
ok thanks to all the replies.

One last question, which is a more better practice ?

(1) Subr "XYZ" that has 1/2 of the variables that is supplied by argument and remaining half is the vars. declared by the subrs that called this subr "XYZ".  Example ...
Code - Auto/Visual Lisp: [Select]
  1. (defun ABC ()
  2.  
  3. ;do stuff here
  4. (XYZ a1 b1 c1 d1)
  5. );defun ABC
  6.  
  7. (Defun XYZ (a b c d)
  8. ;do stuff here
  9. )
  10.  

(2)  Place all argument into subr XYZ
Example ..
Code - Auto/Visual Lisp: [Select]
  1. (defun ABC ()
  2.  
  3. ;do stuff here
  4.  
  5. (XYZ a1 b1 c1 d1 e1 f1 g1 h1 i1 j1 k1 l1 m1 n1 o1 p1)
  6. );defun ABC
  7.  
  8.  


A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

Brief history of Microsoft
https://www.youtube.com/watch?v=BLaMbaVT22E

CatDance

  • Newt
  • Posts: 57
Re: any advantage to place a subroutine inside a main defun function ?
« Reply #8 on: November 02, 2021, 06:25:36 AM »
    There could be other subroutines with the same names out there, this ensures the correct ones are called. This is especially an issue if you didn't write all of the LISP routines you use yourself.[/li][/list]

    All subroutines should have unique names even if they are doing basically the same thing. because the last subroutine loaded with that name will run when called in any lisp.

    Example
    load lisp1 has a subr named "test"
    load lisp2
    load lisp3 has a subr also named "test"

    If you run lisp 1 its not going to run test subr from lisp1 its going to run subr test from lisp 3 because it was loaded last.
    the sec you change something in that subroutine but keep the same name their is potential to screw up any other lisp that uses that named subroutine in.

    ya, I understand that.
    A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
    https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

    Brief history of Microsoft
    https://www.youtube.com/watch?v=BLaMbaVT22E

    CatDance

    • Newt
    • Posts: 57
    Re: any advantage to place a subroutine inside a main defun function ?
    « Reply #9 on: November 02, 2021, 06:27:25 AM »

    The downside is that when I want to share a solution to someone's question, I have to track down the multiple helper subroutines and include with the code. And this is usually too much of a pain so I don't help too often. So having all the subroutines in the main code avoids that problem.

    What is a multiple "helper subroutines" ?
    A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
    https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

    Brief history of Microsoft
    https://www.youtube.com/watch?v=BLaMbaVT22E

    danAllen

    • Newt
    • Posts: 133
    Re: any advantage to place a subroutine inside a main defun function ?
    « Reply #10 on: November 02, 2021, 10:20:06 AM »
    What is a multiple "helper subroutines" ?

    I have subroutines such as error initiate & end, convert degrees to radians, ask for input with default, etc.
    Lee Mac has some posted: http://www.lee-mac.com/programs.html#subfunctions

    cmwade77

    • Swamp Rat
    • Posts: 1443
    Re: any advantage to place a subroutine inside a main defun function ?
    « Reply #11 on: November 02, 2021, 02:08:58 PM »
      There could be other subroutines with the same names out there, this ensures the correct ones are called. This is especially an issue if you didn't write all of the LISP routines you use yourself.[/li][/list]

      All subroutines should have unique names even if they are doing basically the same thing. because the last subroutine loaded with that name will run when called in any lisp.

      Example
      load lisp1 has a subr named "test"
      load lisp2
      load lisp3 has a subr also named "test"

      If you run lisp 1 its not going to run test subr from lisp1 its going to run subr test from lisp 3 because it was loaded last.
      the sec you change something in that subroutine but keep the same name their is potential to screw up any other lisp that uses that named subroutine in.
      In theory that sounds great, but there is always the potential of duplicating subroutine names written by others. Given that users could potentially use additional LISP routines, you don't always know the name of every single subroutine used.

      If the routine is within your defined routine, then it will ensure that your version is loaded just for that routine alone and then unloaded after your routine finishes. As I said, this originally was mostly done for memory management, but this was a secondary benefit.

      CatDance

      • Newt
      • Posts: 57
      Re: any advantage to place a subroutine inside a main defun function ?
      « Reply #12 on: November 03, 2021, 09:35:18 AM »
      What is a multiple "helper subroutines" ?

      I have subroutines such as error initiate & end, convert degrees to radians, ask for input with default, etc.
      Lee Mac has some posted: http://www.lee-mac.com/programs.html#subfunctions

      ok thanks. I take a look at LM subr later.
      A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
      https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

      Brief history of Microsoft
      https://www.youtube.com/watch?v=BLaMbaVT22E