Jump to content

Recommended Posts

Posted

This is my first post here, although I have been reading the forum threads for quite some time. Many thanks to all of you that have helped me solve a number of LISP programming problems! The level of expertise (and patience) shown by the members of this forum is something I truly appreciate.

 

My question is regarding the proper formatting of nested functions. We have an Enterprise.LSP file that gets loaded with our Enterprise.CUI. It contains a lot of stuff, and is was 'cobbled' together over years, with lots of odd snippets of code. I'm cleaning it up, and updating the code to be more compact and efficient. Many of the bigger routines call other routines to supply parameters needed for operation. Nearly all of them fail to declare local variables. I'd like to switch to local variables, but I'd like some guidance on the best way to do that.

 

Is it possible (or smart) to declare the sub-routine functions inside of the larger functions? If so, should the variables that are passed between the various functions be declared as local at the top level of larger function?

 

I guess what I'm asking is how to avoid all the global variable use without immediately having to re-work all the code to put the content of the various sub-routines inside the function that needs them. For instance, I have a routine that inserts a drawing border, and fills out some of the drawing information. There is a subroutine that looks up their initials from a user list. The variables it uses are all global, since the functions are currently not nested. Since the border insertion function is the only one that uses that subroutine, I could safely integrate that code with the larger function, but that is somewhat slow and laborious. Would it be proper to move that subroutine into the larger one, and then declare the variables (including those in the subroutine) as local at the top level function declarations, thus eliminating them as global variables?. I hope this question makes sense....:wink:

 

Thank you!

-Owen

Posted

It is always difficult and tedious to go through code and try to figure out what they where thinking. In most cases it is ok to nest a defun and kill declare its variable in the main defun. But you really don’t want to have several nested defuns because it makes it difficult to trouble shoot. I like to keep my sub routines separate and I don’t share variables.

Make a copy of the program before you start to change things.

First I would open my lisp console and create a project so it will be easer to work on all the files for that program. Then load all the relevant files into the console. Then one by one click on a variable and use the find function and in the find dialog box select the find in project option and select the project name. This will display every occurrence of that variable in however many file it occurs. Then you can tell how scattered the variable is and make a decision on how to deal with it.

You could declare all variable including the sub functions variables in the main functions declarations area and see if the program works, as it should. The reason you want to kill variables as soon as possible is a memory thing. A variable uses memory until it is released. If it’s a large program with hundreds of variables its better to kill a.s.a.p. but a small program with 20 variable is not that big of deal to kill all in the end.

If a sub function contains variables used in the main function or another function you should consider changing it. When a sub function is called it should return your answer then you have the opportunity to check the answer before the program continues to insure it is correct.

Simple example of one way:

 

 
(defun c:test1 ( / a b c  )
(setq a (getint "enter a number"));_user number
  (setq b (getint "enter another number"));_user number
       (setq c (addnum a b));_call to sub passing 2 vars
  (if c (alert (strcat "Your Total Is "(itoa c))));_check it and display result
  (princ)
);_defun
(defun addnum (aug1 aug2 / x ret)
    (setq x (+ aug1 aug2));_add 2 numbers
   (setq ret (* x 3));_times number by 3
   ret     ; i like to do it this way to make sure my return is the last thing processed by the sub
);_defun

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...