Author Topic: Importance of localizing your vars  (Read 3677 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Importance of localizing your vars
« on: October 19, 2004, 05:10:49 PM »
This is cool and worth mentioning. My article has been re-surged and i am gonna add whole another section to it. hopefully i will bring the reader to a state of confusion that would compair to those seeking to understand Dent.

Here is my procedure.
Code: [Select]

(defun test (n / a)
 ;; Create a helper function just to make this more readable.
  (defun helper (n) (1+ n))
 ;; set a variable in our procedure and fill it with the value of 1+ a number.
  (setq a (helper n))
 )


Command: !a
nil

Command: (setq a 7)
7

Command: !a
7

Command: (defun test (n / a) (defun helper (n) (1+ n)) (setq a (helper n)))
TEST

Command: (test 5)
6

Command: !a
7
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

whdjr

  • Guest
Importance of localizing your vars
« Reply #1 on: October 19, 2004, 05:32:01 PM »
Not sure what your gettin at, but anytine you localize a variable it becomes nil until you assign it a value within your program or your program ends and the global value is reinstated.

Your example is a good diagram to illustrate that with. :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Importance of localizing your vars
« Reply #2 on: October 19, 2004, 07:01:48 PM »
Basicly what i was trying to covey is that if a variable exists, using a local variable in a procedure does not effect that initial variable unless you do not localize it.  I know it sounds rudementry, but i assure you, I will attempt to explain a basic (well, kinda advanced) programing method that you can use to make some really kick butt programs. (Or at least make your programs better and  more usable with.)  ...My article will explain it all. (w00t!)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

whdjr

  • Guest
Importance of localizing your vars
« Reply #3 on: October 20, 2004, 08:37:04 AM »
Where is the article?

This should be a good read. :)

daron

  • Guest
Importance of localizing your vars
« Reply #4 on: October 20, 2004, 08:45:01 AM »
patience red belt.

whdjr

  • Guest
Importance of localizing your vars
« Reply #5 on: October 20, 2004, 08:53:28 AM »
Quote from: Daron
patience red belt.

Darn, I forgot to change that.  I've moved up to pink now.

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Importance of localizing your vars
« Reply #6 on: October 20, 2004, 10:41:16 AM »
Quote from: whdjr
Where is the article?

This should be a good read. :)


Its still being worked on; been doing so, for months. (It's provent to be a a real pain!!!)

I hope so.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
Importance of localizing your vars
« Reply #7 on: October 20, 2004, 10:50:09 AM »
Se7en,

I don't remember when ADESK fixed it, but it used to be that if a program had been canceled during execution, the variables went global no matter how they were declared.  Possibly A2K fixed it but it went on for a long time.  -David
R12 Dos - A2K

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Importance of localizing your vars
« Reply #8 on: October 20, 2004, 11:14:27 AM »
True story. We had a postal worker here go global; it's not pretty.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Importance of localizing your vars
« Reply #9 on: October 20, 2004, 11:19:35 AM »
Really?! ...hummm

...A testing we will go!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Importance of localizing your vars
« Reply #10 on: October 20, 2004, 11:30:00 AM »
huh? So this was in 14 (and lower of course)

Did the error trap help? *thinking* Well i guess you would have to check all those vars and then store those values ...phew! man that must have been some work!

Just for kicks:

Acad 2000:
Command: (setq a 3)
3

Command: (defun test (n / a)
(_>   (setq a n)
(_>   (defun helper () (1+ a))
(_>   (setq a (helper)))
TEST

Command: (test (getint "\n# "))

# k

Requires an integer value.

# *Cancel*
; error: Function cancelled

Command: !a
3

Acad 2004:
Same results
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org