Author Topic: Null vs empty?  (Read 11580 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10640
Re: Null vs empty?
« Reply #15 on: August 07, 2018, 03:03:38 PM »
I totally agree a non-string would signify something wrong but a programmer should check the returned value(s) and take appropriate action(s) depending on the return value(s).

Code - Auto/Visual Lisp: [Select]
  1. (setq x '("" "" "" "" "" "" 1))
  2.  
  3. (defun test-str (x)
  4.   (= "" (apply 'strcat x))
  5. )
  6.  
  7. (defun test-str-2 (x)
  8.   (if (not (apply '= (cons "" x)))
  9.     (princ "exit")
  10.     (princ "pass")
  11.     )
  12.   )
  13.  
  14. (test-str x)
  15. (test-str-2 x)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Null vs empty?
« Reply #16 on: August 07, 2018, 04:03:33 PM »
For my suggestion theres one thing to be aware of - require checking if 'x' is not null:

Code - Auto/Visual Lisp: [Select]
  1. _$ (apply '= (cons "" nil))
  2. T
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Null vs empty?
« Reply #17 on: August 07, 2018, 04:22:00 PM »


Code: [Select]
(setq l '("" "" "" "" "" """" "" "" "" "" """" "" "" "" "" """" "" "" "" "" """" "" "" "" "" """" "" "" "" "" ""))
(defun test-str (l) (= "" (apply 'strcat l)))
(defun test-str-2 (l) (apply '= (cons "" l)))
(defun _allemptystrings (l) (defun _chk (x) (and (= 'str (type x)) (= x ""))) (vl-every '_chk l))
(benchmark '((test-str l) (test-str-2 l) (_allemptystrings l)))
;;;    (TEST-STR-2 L)...........1922 / 2.25 <fastest>
;;;    (TEST-STR L).............2062 / 2.10
;;;    (_ALLEMPTYSTRINGS L).....4328 / 1.00 <slowest>

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Null vs empty?
« Reply #18 on: August 07, 2018, 04:40:17 PM »
Code: [Select]
(defun test-str-3 (l) (not (vl-remove "" l)))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Null vs empty?
« Reply #19 on: August 07, 2018, 04:47:15 PM »
Whilst I agree that type checking is necessary when the function is capable of being supplied with arbitrary data (which in itself is a rare occurrence since even AutoLISP data acquired from a user is typically either of a predetermined data type or null), if a function is being evaluated from within a program, the data type of the arguments supplied to the function is known in advance by the programmer and so additional type checking is redundant and superfluous.

Aside, note that the standard AutoLISP functions operate in much the same way -
Code - Auto/Visual Lisp: [Select]
  1. _$ (rtos "1")
  2. ; error: bad argument type: numberp: "1"

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Null vs empty?
« Reply #20 on: August 07, 2018, 05:50:45 PM »
Agree with Lee, Owen etc. Link.
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: 10640
Re: Null vs empty?
« Reply #21 on: August 07, 2018, 08:06:45 PM »
Tired of painting the bikeshed; why does the scope keep changing to suite diffferent points?

This is not overhead, complicated, or difficult. You do it a thousand times when you write code. You are not calling or creating an API, you are not creating a library, or reinventing the steam locomotive, you are mearly accepting data (from a source you may not have control of--user or text file for example) and you should check to make sure it is of the type/scope/range you expect before you go off "dividing by zero" or some other weird thing.

It is the same as:
Code - C++: [Select]
  1. if (n <= 0) {
  2.         printf("The number should be positive.\n");
  3. } else {
  4.         for (int i = 1; i <= n; ++i) {
Are [you] honestly telling me this is not "correct". -i.e. in this code the program exits (falls out of the IF statement) if the number isnt postive; are [you] telling me the programmer should have just "preformed math" or "entered a loop" or "launched N rockets" on/with a negative number?

In the two examples we have been using I mearly made the point that STRCAT will fail (not "exit" or "fall out". "fail") in the instance that the argument was not a string (it is like saying the "<=", above, is not able to check numbers and something else should be used).

Of course native functions (and library functions) will fail if they receive a value not in their wheel house. You cannot expect RTOS to accept strings nor should you expect STRCAT to accept numbers (absolutely nothing shocking here).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Null vs empty?
« Reply #22 on: August 07, 2018, 10:03:13 PM »
Agree with Lee, Owen etc. Link.
Same mindset here.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Null vs empty?
« Reply #23 on: August 08, 2018, 08:48:11 AM »
Code: [Select]
(defun test-str-3 (l) (not (vl-remove "" l)))
You win! :)
Quote
    (TEST-STR-3 L)...........1375 / 2.77 <fastest>
    (TEST-STR-2 L)...........1688 / 2.26
    (TEST-STR L).............1781 / 2.14
    (_ALLEMPTYSTRINGS L).....3813 / 1.00 <slowest>

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10640
Re: Null vs empty?
« Reply #24 on: August 08, 2018, 09:52:23 AM »
...
You win! :)
...

Nope. Doesn't meet the criteria to fail.

Code: [Select]
Command: (setq x '("" "" "" "" "" "" 1))
("" "" "" "" "" "" 1)

Command: (defun test-str-3 (l) (not (vl-remove "" l)))
TEST-STR-3

Command: (test-str-3 x)
nil
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Null vs empty?
« Reply #25 on: August 08, 2018, 09:54:09 AM »
Recursive one -

Code - Auto/Visual Lisp: [Select]
  1. (defun test-str-4 ( L )
  2.   (or (null L)
  3.     (and (= (car L) "") (test-str-4 (cdr L)))
  4.   )
  5. )

:)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Null vs empty?
« Reply #26 on: August 08, 2018, 10:01:56 AM »
...
You win! :)
...

Nope. Doesn't meet the criteria to fail.

Code: [Select]
Command: (setq x '("" "" "" "" "" "" 1))
("" "" "" "" "" "" 1)

Command: (defun test-str-3 (l) (not (vl-remove "" l)))
TEST-STR-3

Command: (test-str-3 x)
nil
Looks good to me? You gave it a 1 and it said NO not all empty strings?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10640
Re: Null vs empty?
« Reply #27 on: August 08, 2018, 10:03:26 AM »
Looks good to me? You gave it a 1 and it said NO not all empty strings?

According to you guys, it is supposed to fail.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Null vs empty?
« Reply #28 on: August 08, 2018, 10:05:01 AM »
Recursive one -

Code - Auto/Visual Lisp: [Select]
  1. (defun test-str-4 ( L )
  2.   (or (null L)
  3.     (and (= (car L) "") (test-str-4 (cdr L)))
  4.   )
  5. )

 :)
And the numbers are in :)
Quote
    (TEST-STR-3 L)...........1344 / 2.86 <fastest>
    (TEST-STR-2 L)...........1703 / 2.26
    (TEST-STR L).............1735 / 2.22
    (TEST-STR-4 L)...........3172 / 1.21
    (_ALLEMPTYSTRINGS L).....3844 / 1.00 <slowest>

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Null vs empty?
« Reply #29 on: August 08, 2018, 10:09:09 AM »

And the numbers are in :)
Quote
    (TEST-STR-3 L)...........1344 / 2.86 <fastest>
    (TEST-STR-2 L)...........1703 / 2.26
    (TEST-STR L).............1735 / 2.22
    (TEST-STR-4 L)...........3172 / 1.21
    (_ALLEMPTYSTRINGS L).....3844 / 1.00 <slowest>

Doh, recursion is slow no matter what :geek:

BTW I think if you externally define '_chk' (outside of _allemptystrings) it should perform drastically faster, since it wont redefine the subfoo each time.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg