Author Topic: Where to save variable  (Read 2190 times)

0 Members and 1 Guest are viewing this topic.

kruuger

  • Swamp Rat
  • Posts: 633
Where to save variable
« on: March 03, 2014, 05:38:18 PM »
hello,
please see two codes below. which code is better/more clear 1 or 2 ?
any pros/cons.
Code: [Select]

(defun c:tx1 (/ _Sub a)
  (defun _Sub (/ res)
    ...code...
    res
  )
  (princ (setq a (_Sub))) ; save variable return by subroutine
)
(defun c:tx2 (/ _Sub a)
  (defun _Sub (/ res)
    ...code...
    (setq a res) ; save variable under subroutine
  )
  (princ a)
)


thanks

kruuger

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Where to save variable
« Reply #1 on: March 03, 2014, 05:51:20 PM »
Hi,

IMO the first one is better, but there's no need to define the 'a' variable if you don't use it.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tx1 (/ _Sub)
  2.   (defun _Sub (/ res)
  3.     ...code...
  4.     res
  5.   )
  6.   (princ (_Sub))
  7. )
Speaking English as a French Frog

kruuger

  • Swamp Rat
  • Posts: 633
Re: Where to save variable
« Reply #2 on: March 03, 2014, 05:58:10 PM »
Hi,

IMO the first one is better, but there's no need to define the 'a' variable if you don't use it.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tx1 (/ _Sub)
  2.   (defun _Sub (/ res)
  3.     ...code...
  4.     res
  5.   )
  6.   (princ (_Sub))
  7. )
yes, "a" is not necessary in first code but it was just a sample. in my program i'm using "this" variable few times.
looks like it is much easier to track variable if it is "outside" the subroutine.


thanks gile

kpblc

  • Bull Frog
  • Posts: 396
Re: Where to save variable
« Reply #3 on: March 03, 2014, 10:32:38 PM »
I think use global variables is not good practice. So first code is more readable and more profeccional.
Sorry for my English.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Where to save variable
« Reply #4 on: March 04, 2014, 05:19:02 PM »
IMO the first one is better

Agreed - I try to use lexical scoping over dynamic scoping where possible, otherwise functions may reference symbols which may not necessarily be defined.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Where to save variable
« Reply #5 on: March 05, 2014, 01:26:55 AM »
I'd agree with the lexical idea, even though AutoLisp doesn't use lexical. Though both the OP's methods are still strictly lexical too. That's due to nesting. Even in lexical scope the 2nd sample is possible. a "true" dynamic scope would mean you can write something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun _Sub (/ res)
  2.   ;... some code ...
  3.   (setq a res)
  4.   ;... some other code ...
  5. )
  6.  
  7. (defun c:tx1 (/ a)
  8.   ;... some code ...
  9.   (_Sub)
  10.   ;... some more code ...
  11.   (princ)
  12. )
  13.  
  14. (defun _intermediate (/ c)
  15.   ;... some code ...
  16.   (_Sub)
  17.   ;... some more code ...
  18.   (setq b c)
  19.   ;... some other code ...
  20. )
  21.  
  22. (defun c:tx2 (/ a b)
  23.   ;... some code ...
  24.   (_intermediate)
  25.   ;... some more code ...
  26.   (princ)
  27. )
So you see with dynamic scope it's even possible to affect a defun's local variable without nesting being necessary. All that's needed is that the local variable forms part of one of the defuns which called the _Sub defun (even indirectly) - i.e. that variable had to be defined somewhere. Else it would assign the value to a global a variable - if it doesn't exist, it would create one.

So you "can", but "should" you? Is actually the question. And that in most cases is a definite NO! Try to avoid it at all costs, since using this method might break something if you're not totally sure that the variables are the exact same names in any scenario. I'd say use the dynamic scope only if you're absolutely sure it can't be done the lexical way, and then make sure you document it extensively so someone else (or even yourself a year later) using your code isn't surprised when their variables get new values they didn't expect.
« Last Edit: March 05, 2014, 01:31:00 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

kruuger

  • Swamp Rat
  • Posts: 633
Re: Where to save variable
« Reply #6 on: March 06, 2014, 06:05:40 PM »
thanks guys for all your inputs
kruuger