TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: kdub_nz on February 05, 2023, 06:28:37 PM

Title: A different Challenge.
Post by: kdub_nz on February 05, 2023, 06:28:37 PM
I have a challenge with a difference.
It applies to all programming languages.

Try to make your Functions/Methods no longer than 10 or 15 lines of code.
This won't always be practical , but we may find that :

The code can have functionality ideas separated.
The code will be easier to test and debug ( when needed )
The code will be easier to read.
The code will be easier to reason about.
The code may not need comments or documentaion because each functionality idea will be named.

The functions can be either included inside the scope of the 'parent'
or named with a prefix indicating the main function group it belongs to ( to help management )

We'll find the main function will read more like a code summary ( with breathing space between ideas ) rather than a mass of code.
We'll find the code will be easier to write because we only need to concentrate on one idea at a time.

I believe this will benefit both seasoned and more novice developers.

Regards,
Title: Re: A different Challenge.
Post by: BIGAL on February 05, 2023, 11:09:18 PM
Try to make your Functions/Methods no longer than 10 or 15 lines of code.

1 line

(command "rectang" (setq p1 (getpoint "\nPick 1st corner "))(setq p2 (getpoint "\n pick 2nd corner ")) "pedit" (entlast) "w" 5 "" "chprop" (entlast) "" "C" 1 "" "line" p1 p2 "")

Could be longer ha ha.

Yes using lambda with lisp can save lots of code lines. Often people like Lee-mac provide code a fraction of original code.

Obvious is draw a rectang with .NET compare to what I posted. Realistically the code above is 4 lines.

Look at the challenge section lots of lambda that are super short.

This is a function I use often.
(setq p2 (mapcar '+ p1 (list X Y 0.0)))
(setq pt3  (mapcar '+ pt1 pt2) )    adds 2 pts
(setq pt3 (mapcar '(lambda (x) (/ x 2.0)) pt3)   ;   divide by 2
(setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5 0.5))) ; mid point
Title: Re: A different Challenge.
Post by: JohnK on February 07, 2023, 03:01:38 PM
I always try to keep my functions small and specialized; smaller functions are defiantly the way to go but you bring up an interesting point about function names I typically do not do.

As an example, would that be: "FancyApp:Getpoint"?

On the note of using a modular programming style you often have to try and keep the code-bloat potential down. I just had an instance where I had pretty bad code-bloat.

The use of lambdas is great but if I find myself needing to use it more than once, I construct it as a separate function. The rule-of-thumb is to construct your functions to do only one thing. MP once said that if you ever used the word "and" in the function description that function should be rethought to be two functions instead of one.
Title: Re: A different Challenge.
Post by: kdub_nz on February 07, 2023, 05:13:08 PM
< . . . >

As an example, would that be: "FancyApp:Getpoint"?

< . . . >

I found that was a good way to identify what belongs with what parent.
With larger projects I'd keep all the support functions in one file which makes compiling a little cleaner.
Keeping variables in scope is pretty easy, they're either local to the child or local to the parent ( or globals can be cleaned up at termination , so there's no pollution)

Usually, if the child function was included local to the parent, I'd just use _GetPoint and include the symbol in the locals list.


// -----
I'm not suggesting being dogmatic about the proposal.
I know from hard earned experience the advantages for reading and debugging.
Title: Re: A different Challenge.
Post by: It's Alive! on February 07, 2023, 06:15:35 PM
In C++, we have core guidelines. Wondering if there’s something like that for lisp.

FancyApp:point3d-get()   :laugh:
or
FancyApp:Editor:Point3d_get()
Title: Re: A different Challenge.
Post by: danAllen on February 07, 2023, 08:23:33 PM
I'm not sure I'm following along :rolleyes2:, but here is my get point subroutine with default. Not fancyapp specific, but defun prefixed with my firm initials to avoid conflict. If I have a subroutine only used in one primary function, I just define it locally in the main, otherwise it goes in my overall utility file. Which makes it hard to share code because then I have to hunt down all the helper routines.

Code: [Select]
;;;==========================================================
;;; Get Point with Default
;;; derived from Looking Glass Microproducts / CAD cookbook utilities
;;;==========================================================
(defun SAA_getpoint (base prmpt default)
   (Setq
      prmpt (Strcat
               prmpt
               (If default
                  (Strcat " <" (vl-princ-to-string default) ">")
                  ""
               )
               ": "
            )
   )
   (Cond
      ((If base
          (GetPoint base prmpt)
          (GetPoint prmpt)
       )
      )
      (default)
   )
)
Title: Re: A different Challenge.
Post by: BIGAL on February 07, 2023, 10:18:09 PM
I have started using external code rather than internal defun for common get variables or select from a list. Its a simple 1 line of code  to load the lisp.

eg
Code: [Select]
(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter values " "Length " 5 4 "6" "width" 5 4 "1")))
(setq l1 (atof (car ans)) l2 (atof (cadr ans)))
(http://)


Title: Re: A different Challenge.
Post by: It's Alive! on February 07, 2023, 10:18:29 PM
The readability of code, I.e. functions, arguments and variables, with names that are meaningful.
(setq timeInSeconds …) vs (setq t …)

I went back and looked over code I wrote in 2004, generally not bad, not too many single letter variables, except x, y, z, w, h d
Surprisingly, I actually documented most every function, I give it a c  :crazy2:

My C++ is like C--  :whistling:


Title: Re: A different Challenge.
Post by: VovKa on February 08, 2023, 03:19:06 PM
Which makes it hard to share code because then I have to hunt down all the helper routines.
here's the special 'hunting' function https://www.theswamp.org/index.php?topic=56466.msg602906#msg602906
Title: Re: A different Challenge.
Post by: danAllen on February 08, 2023, 04:09:06 PM
here's the special 'hunting' function https://www.theswamp.org/index.php?topic=56466.msg602906#msg602906

Thanks, I had seen that but not studied it. Assumed my inconsistent subroutine header commenting would be problem.

Also barely have the energy/time to devote to programming, much less cleanup old routines. I also need to move to BIM either archicad or revit as drawing our projects both in 2d CAD & 3d sketchup is getting to be too much...
Title: Re: A different Challenge.
Post by: kdub_nz on February 08, 2023, 07:18:37 PM
Which makes it hard to share code because then I have to hunt down all the helper routines.
here's the special 'hunting' function https://www.theswamp.org/index.php?topic=56466.msg602906#msg602906

Thanks Vovka , very nice :)

Title: Re: A different Challenge.
Post by: kdub_nz on February 08, 2023, 07:28:49 PM
The readability of code, I.e. functions, arguments and variables, with names that are meaningful.
(setq timeInSeconds …) vs (setq t …)

I went back and looked over code I wrote in 2004, generally not bad, not too many single letter variables, except x, y, z, w, h d
Surprisingly, I actually documented most every function, I give it a c  :crazy2:

My C++ is like C--  :whistling:

Good IDE's and auto-generated doc-comments have made the process a lot easier.
Unfortunately that doesn't apply to AutoLisp . . .  a pity, because a good IDE would have made development simpler.

// . . . I've recently been through some of my 25+ year old lisp code. Some of it is pretty good , some not so much  :) ( but still works )
Title: Re: A different Challenge.
Post by: JohnK on February 28, 2023, 10:01:19 AM
Back to the original request. I have gone down a rabbit-hole because of this topic so I tried to write up my thoughts into a small writeup/tutorial called "bottom-up (https://www.theswamp.org/index.php?topic=58123.0)". ...Well, on to my next rabbit-hole topic. :)

Thanks again for the topic, kdub.


edit:kdub [link repaired]
Title: Re: A different Challenge.
Post by: BIGAL on February 28, 2023, 06:23:09 PM
Seems the right place Chatgp is creeping in writing lisp code only problem is that its not very good at this stage, needing some one who knows what there doing to edit code produced, starting to appear as a request in forums "please fix code not working"
Title: Re: A different Challenge.
Post by: JohnK on March 14, 2023, 09:59:30 AM
Here is another fun little rabbit-hole topic: Self-commenting code (https://www.theswamp.org/index.php?topic=58149.msg613580#msg613580) (probably, more of a debate than rabbit-hole but fun nonetheless).
Title: Re: A different Challenge.
Post by: JohnK on April 11, 2023, 01:49:31 PM
Another addition to this topic: using locally bound variables to be more descriptive. https://www.theswamp.org/index.php?topic=58202.0