Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Activate/Select method considered harmful. (Word/Excel VBA)

    Edsger Dijkstra died earlier this year. Those who don't know of him may use the reference link at the foot of this message.

    About eighteen months ago I (was) switched from the Select Method and Selection Object in WordVBA to the Range Object. About two weeks ago I was steered away from the Activate method and Selection Object in ExcelVBA to the Range Object.

    I now consider the Activate and Select methods harmful, but with nowhere near the grand theoretical insight of Dijkstra.

    There are two types of computer users in the world, those who program ("us") and those who don't ("them"). We programmers actually use computers too. Everyone balances their cheque book and everyone computes gas mileage. They do it painfully slowly. Us programmers develop cute little GUI forms to assist us.

    The Theory Of Life applies to all users. "All of life consist of two things; keying in, or doing something with it".

    Users manage to key in or do something by use of the mouse and/or keyboard. The user clicks (or keyboards) on the application title bar to activate the workbook, clicks on the tab to activate the worksheet, clicks on the cell to activate the cell, and then keys in some data or prepares to Edit, Copy or Format, Font the existing contents of the cell that was selected for the operation.

    The keyboard and mouse are the only user' only communication channel with the computer, especially are they the only means of communicating with the computer to effect what is displayed on the screen (the workbook, the worksheet, the cell, the value, the formatted value). ((OK. Touch-screens allow the user to use the index finger directory to point to the workbook, worksheet etc, but the principle is sound. The user must point or touch to start any kind of process.))

    Programmers manage to key in or do something by use of instructions. We programmers write:

    <pre> Documents("TrainingFlyer.doc").Paragraphs(3).Range .InsertAfter ("Hello World!")
    </pre>


    or

    <pre> Documents("TrainingFlyer.doc").Paragraphs(2).Space Before = 288
    </pre>


    Programmers don't need to communicate by touch with the computer. Programmers can tell the computer where to go, what to do, by powerful instructions.

    Now, programmers have two strikes against them:

    (1) Programmers are users, and think like users, some of the time.

    (2) Programmers (in any language) record macros and examine the entrails to learn how to code instructions to the computer.

    The consequence in VBA is immediately obvious to anyone who has recorded a macro, modified the recorded code to perform a task, and continued to use that style of programming.

    When, as users, we record a macro, we are recording what the user does. The user Activates the workbook, the user Activates the worksheet, the cell etc. The user is forced to point things out by hand, one painful step at a time. "Bring the workbook to my attention so that I can show you which of the three tabs interests me.".

    The recorded language of the user has to include words that represent the touch of a finger.

    Users, unwittingly, Activate objects.

    Programmers who follow the recorded path of Users will be forced to use the "language" of users, hopelessly inefficient, as we know.

    Programmers don't need to Activate Objects, because Programmers don't need to touch objects. Most of the time, programmers aren't anywhere near the computer (screen) that is being used - they are thousands of miles away and therefore can't point with the use of finger-devices. Programmers must point by use of other pointing devices. Called Range Objects.

    An Range Object is a pointer to a chunk of memory. (To an Object in memory, if you prefer, but it is still a pointer).

    Programmers are given the ability to communicate with the computer directly, in a way that Users never can.

    Programmers can issue instructions (write programs) to make it appear that they are there, sitting right beside the user, but the user isn't fooled. The user knows that the programmer is lying on the beach enjoying the sun.

    Only the programmer has been fooled.

    When we program .Activate or .Select in order to key in or do something with data, we are pretending that we need to be there with the user. We don't.


    Moral: If you get a chance to program it without the Activate or Select methods, without the Selection object, do so.


    References:
    ==========


    Do a Google search for "Go To Statement Considered Harmful" - Edsger W. Dijkstra - Reprinted from Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147-148. Copyright

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Activate/Select method considered harmful. (Word/Excel VBA)

    I think to make your case more convincing you ought to give us programmers a few examples of
    Activate and Select that have been proven dangerous for computer users and other strangers.

    Not that I disagree in the least.

    Thanks for the "memorial link" re: Mr. Dijkstra.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Activate/Select method considered harmful. (Word/Excel VBA)

    >give us programmers a few examples of

    Well, Activate and Select aren't dangerous to Users, in fact, although the User is unaware of it, it's the only (read "99%") way for a User to, well, Use. (No User is going to be interested in learning that the mouse and keyboard are electrical extensions of the fingertips, right?)

    Activate and Select aren't really "dangerous" to programmers, but then Dijkstra ("harmful") didn't mean that if I used GoTo one more time I'd lose the use of my right hand, or that my hair would fall out. (Actually, now that I think of it ....).


    "Dangerous" in this sense means, broadly, costly, in costs of getting-it-to-work, debugging, enhancing. If it were a spaceship controller, life-threatening, but for the 99% of regular programming, "GoTo" is almost bound to cause problems some time, the only question being "When?".

    We have pretty well stopped using GoTo, haven't we? We are all writing nested loops, using While/Do/Until and If/Then/Else.

    One of Dijkstra's points was that GoTo is dangerous once you leave the realms of machine/assembly language programming. One could make a similar remark about Activate/Select, that they are dangerous once you leave the User Interface.

    Unless there is a compelling need to shove some data into the User's face, the flashing screens that erupt with Activate/Select should be avoided, and replaced by a quiet non-intrusive status bar or similar to report progress.


    Now I think about it, Activate/Select are to Windows (programmers) what the rows of flashing lights were to machine-language programmers (the hardware engineers) - they allow the designers to visualise the process for purposes of debugging etc.


    Notwithstanding John's point that some things can only be done with an ActiveWindow - I consider that a design flaw in the VBA language - Activate and Select generally cause programmers to write more code than we ought, and that is close to dangerous. (If you are going to Activate sheets or Select cells, then you ought first write code to preserve the initial Activation or Selection, and then write code to restore it after the job is done).


    Finding an example of "dangerous" is tough, maybe impossible, but the busines sof quantifying the (extra) cost of programming Activate/Select ought to be becoming obvious to us all.


    I know that neither you nor i are nit-picking; I hope this response is satisfactory.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Activate/Select method considered harmful. (Word/Excel VBA)

    I went back and re-read http://www.acm.org/classics/oct95/ and found another gem: "It is fairly obvious that I am not uninfluenced by ... ". He never did get around to writing a second paper on the use of double-negatives.

    I used to ask programming classes "Is it not true that I am not a woman?". I'd give them five seconds to consider the answer and then raise their hands if they thought the answer was 'True". Now raise your hand if you think "False". Laughter erupts as each 50% of the class realise that they can't both be right ....

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Activate/Select method considered harmful. (Word/Excel VBA)

    I think the choice boils down to one's preference between these two things:

    (1) Elegance of operation
    (2) Time to do anything else

    Unless, of course, you charge by the hour. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  6. #6
    Star Lounger
    Join Date
    Feb 2001
    Location
    Vancouver, BC, Br. Columbia, Canada
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Activate/Select method considered harmful. (Word/Excel VBA)

    Chris - I just want you to know that I read this post some time ago and it has totally revolutionized by code - I program faster, better, more robustly.
    Thank you!!!
    <img src=/S/love.gif border=0 alt=love width=15 height=15>

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Activate/Select method considered harmful. (Wo

    > it has totally revolutionized by code

    Gwenda, I've stopped laughing now, honest .......

    What a beauty!



    OK. Calm down.

    Whew.

    It's Dijkstra we have to thank, plus a few thousand Loungers who have, periodically, hit me over the head with a four-by-two chunk of Range Object.



    I'm glad it has made a difference for you. It sure has for me. Thanks for the follow-up. You made by day (hah hah hah!)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •