Author Topic: A different Challenge.  (Read 2539 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
A different Challenge.
« 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,
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: A different Challenge.
« Reply #1 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
A man who never made a mistake never made anything

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: A different Challenge.
« Reply #2 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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: A different Challenge.
« Reply #3 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.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: A different Challenge.
« Reply #4 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()

danAllen

  • Newt
  • Posts: 132
Re: A different Challenge.
« Reply #5 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)
   )
)

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: A different Challenge.
« Reply #6 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)))



A man who never made a mistake never made anything

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: A different Challenge.
« Reply #7 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:



VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: A different Challenge.
« Reply #8 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

danAllen

  • Newt
  • Posts: 132
Re: A different Challenge.
« Reply #9 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...

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: A different Challenge.
« Reply #10 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 :)

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: A different Challenge.
« Reply #11 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 )
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: A different Challenge.
« Reply #12 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". ...Well, on to my next rabbit-hole topic. :)

Thanks again for the topic, kdub.


edit:kdub [link repaired]
« Last Edit: February 28, 2023, 02:26:45 PM by kdub »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: A different Challenge.
« Reply #13 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"
A man who never made a mistake never made anything

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: A different Challenge.
« Reply #14 on: March 14, 2023, 09:59:30 AM »
Here is another fun little rabbit-hole topic: Self-commenting code (probably, more of a debate than rabbit-hole but fun nonetheless).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org