Author Topic: (and (or *acad*  (Read 19140 times)

0 Members and 1 Guest are viewing this topic.

cadman6735

  • Guest
(and (or *acad*
« on: August 30, 2011, 12:35:35 PM »
I have been noticing code like this:

Code: [Select]
(and
  (or *acad* (setq *acad* (vlax-get-acad-object)))
  (or *doc* (setq *doc* (vla-get-activedocument *acad*)))

How does the (and (or statement benefit?

Thanks

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (and (or *acad*
« Reply #1 on: August 30, 2011, 12:41:57 PM »
No benefit, just a creative use of the language.
IMO   8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: (and (or *acad*
« Reply #2 on: August 30, 2011, 12:53:19 PM »
`creative use' is a stretch at best IMO. If that code were to be converted to plain English it would be "something" resembling utter gibberish.

Use COND in lieu of (and (or

I use:
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object))))

Far more clean and concise code wise.

The extension of that concept would be:
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object)))
      *activedocument*
         (cond
           (*activedocument*)
           ((vla-get-activedocument *acadobject*)))
      *documents*
         (cond
           (*documents*)
           ((vla-get-documents *acadobject*)))
...
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (and (or *acad*
« Reply #3 on: August 30, 2011, 01:02:28 PM »
Beauty is in the eye of the beholder.

Sorry you only see ugliness.  :-(
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: (and (or *acad*
« Reply #4 on: August 30, 2011, 01:05:28 PM »
I like the or method, but I also use a slight mod of Se7en's:

Code: [Select]
(cond (*acadobject*)
      ((setq *acadobject* (vlax-get-acad-object)))
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: (and (or *acad*
« Reply #5 on: August 30, 2011, 01:15:34 PM »
CAB, I don't see it as ugly. I see it as obfuscation for unknown reasons and code obfuscation is not something that should be in anyone's eye (beholders or users) IMO.

I'm sure you have heard the saying that programming languages are based upon the English language and to program you just assemble words into sentences and sentences into paragraphs; try and convert the above into a sentence then convert mine.

I posted a write up at one time on this subject but I'm not sure what it was called. I will/can/would be happy to look for it if you want to discuss (I really enjoy discussions like this quite a bit! I think they are a lot of fun). Let me know.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: (and (or *acad*
« Reply #6 on: August 30, 2011, 01:15:59 PM »
Another might be:

Code: [Select]
(if (null *acad*)
    (setq *acad* (vlax-get-acad-object))
)

I can see Se7en's viewpoint with regard to how the code 'reads' since the logical functions are more suited to cases where a boolean return is specifically required, and I would also avoid using the 'and' as in the first post, but I do agree that the use of 'or' for this purpose is quite clean and concise.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: (and (or *acad*
« Reply #7 on: August 30, 2011, 01:18:52 PM »
English language my butt! Someone needs to put that in a sentence for me 'cause I don't understand.

I see no English in this. :)
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object))))
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: (and (or *acad*
« Reply #8 on: August 30, 2011, 01:20:46 PM »
Another might be:

Code: [Select]
(if (null *acad*)
    (setq *acad* (vlax-get-acad-object))
)
Now see, that's what I'm talkin 'bout. A plain ol' IF statement. That I understand!
LOL
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: (and (or *acad*
« Reply #9 on: August 30, 2011, 01:22:45 PM »
English language my butt! Someone needs to put that in a sentence for me 'cause I don't understand.

I see no English in this. :)
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object))))

"Set the variable based upon two conditions: the previous definition or the result to a call of `vlax-get-acad-object'"

*smile*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

cadman6735

  • Guest
Re: (and (or *acad*
« Reply #10 on: August 30, 2011, 01:25:00 PM »
Why do you need a condition?

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: (and (or *acad*
« Reply #11 on: August 30, 2011, 01:26:24 PM »
@Lee, Mark:
I agree `that' statement reads and converts nicely but I also think that it hides the fact that a variable is being set/created (and if that variable is GLOBAL I think that should be "out front" and "highlighted" to any reader). That is why I adopted the (setq var (cond syntax a long time ago.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: (and (or *acad*
« Reply #12 on: August 30, 2011, 01:26:59 PM »
Why do you need a condition?

Because you want to avoid having "extra" calls if necessary.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BlackBox

  • King Gator
  • Posts: 3770
Re: (and (or *acad*
« Reply #13 on: August 30, 2011, 01:28:27 PM »
If defining the variable in an AND statement at the beginning of a function (i.e., IF+AND), I like to use the OR method, whereas when nested in an 'action' statement (i.e., during an vlax-for iteration, etc.) I use a COND method as Alan has shown.

Yes, using the COND method would work in both situations, but I choose to use similar/separate syntax for my own edification when reading the code later. Personal choice on my part.

Separately, I also take the time to write a SETQ for each variable, instead of including them all in a single call - I find it easier to make quick changes, or substitution/exclusion(s) on the fly by simply commenting out a single SETQ as needed. Again, my preference only FWIW.
"How we think determines what we do, and what we do determines what we get."

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (and (or *acad*
« Reply #14 on: August 30, 2011, 01:33:19 PM »
All the COND structure is doing is (represented by equiv IF code) is:

Code: [Select]
(setq *acad*
    (if *acad*
        *acad*
        (vlax-get-acad-object)
    )
)

Versus:

Code: [Select]
(setq *acad*
    (cond
        (*acad*)
        ((vlax-get-acad-object))
    )
)

and since the latter really affords no increased value (execution, "calls" or typing) I prefer the former (even tho many times I've used the latter in the past) simply because I don't have to explain it to others reading my code. Saves them and me time. /2¢

PS: If you really want to split hairs the IF code is about 10% faster than the equivalent COND code. :P
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst