Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Microsoft- a pet peeve

    Hi,

    When I'm programming in VB or VBA, I have the "option explicit" set to require variable declaration, so that typos in variable names are trapped at compile time. I would have thought that most professional programmers would do the same (I may be wrong).

    However, Microsoft do not appear to like this. In all the sample code in the help file, variables are not defined. So, after pasting in the sample code, I have to try and work out what each variable type is. That's easy for items which are obviously numbers or strings- but it's not always obvious.

    It only took me about 15 seconds to find an example:

    Set doc = ActiveDocument
    Set myRange = doc.Range(Start:=doc.Words(1).Start, _
    End:=doc.Words(3).End)


    obviously, myRange should be defined as range- but what about doc?

    OK, if you've been around VB for a while, you might know that you might use a "set" statement with an "object" data type, and define "dim doc as object". But I shouldn't have to go to this extra work; and there are times when even an experienced programmer doesn't know how to declare a variable.

    Sure, I could define everything as variant- ant that would work. I've had to resort to that sometimes. But that then means that I don't have access to the properties and methods of that variable when I'm coding.

    When I mentioned professional programmers at the beginning, Microsoft were obviously not included!
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    2 Star Lounger
    Join Date
    Dec 2000
    Location
    eastern Connecticut, Connecticut, USA
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    I agree -- the examples in the Help files aren't always what one might expect from 'pros'.

  4. #3
    2 Star Lounger
    Join Date
    Dec 2000
    Posts
    188
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Lots of samples are basically code snippets, and they probably figure it's up to you to declare variables etc. They probably did it to keep the help files size down. If you want real good code samples check out "Inside Microsoft Office", "Inside Microsoft Excel", and "Inside Microsoft Visual Basic" all from element K Journals.

    Also, don't overlook the Developer's Handbooks for the various apps and VBA at Sybex books, and all the good ones at O'Reilly & Associates.

    FWIW

  5. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 28 Times in 27 Posts

    Re: Microsoft- a pet peeve

    One senses that the assignment of writing code for some of the Help files (as well as KB articles) must often be given to more junior level programmers.

    For an example of MS getting it right, check out MS Press' "MS Office 2000 Visual Basic Programmer's Guide", by David Shank, Mark Roberts and Tamra Myers. I was blown away by the quality and practicality of the samples. It includes a CD with tons of useful code.

  6. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Option Explicit does not require explicit typing.
    You can use a simple Dim without specifying the type of the variable.

    All of the examples you are mad at are using thge implicit default Variant type, so explicitly typing the variables can actually cause some code to not work. You'd have to carefully study the code to figure out which type to use.

    When running such examples, it is easiest to just remove the Option Explicit.

  7. #6
    knoway
    Guest

    Re: Microsoft- a pet peeve

    Hey guys,

    Don't you know that the "professional" programming credo is, "Strong Typing - Weak Minds".

    If byte, half-word, word and double-word were good enough for my grandpappy, they are good enough for me.[img]/w3timages/icons/crazy.gif[/img]

  8. #7
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    They were good enough for me too 30 years ago. I didn't know any better then.

    I thinkk you might be talking "real" programmers, not "Professionnal".
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  9. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Howard,

    I'd rather not have to just use the dim or a variant, or just to run the example without the option explicit. It's useful for me to know what type a variable is (granted, it might be variant- but I'd like to know that as well). If I know the type, I can explore the properties and methods of that variable much more easily. As it is, I have to spend time, when incorporating example code into real life, to work out whether something is variant, integer, object or whatever.

    I guess it would make life a lot easier if the people at Microsoft were to give as some (optional) dim statements, where the type was specified- even if it was specified as variant.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  10. #9
    knoway
    Guest

    Re: Microsoft- a pet peeve

    Yea, I guess you're right.

    "Real" programmers don't eat quiche.

  11. #10
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Microsoft- a pet peeve

    Um, aren't you a "professional" programmer if that's how you make your living, regardless of diet or quality of output?
    Charlotte

  12. #11
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Microsoft- a pet peeve

    You don't have to specify a variant, they're the default type. And the use of the variable name "doc" suggests a document object if it isn't a variant. Keep in mind these are SAMPLES, not necessarily fully working code. It may not even be very good code, but look on the brighter side: coding styles vary so much by programmer, that truncated samples at least keep you from having to recast everything in a totally different style.
    Charlotte

  13. #12
    knoway
    Guest

    Re: Microsoft- a pet peeve

    I was referring to an in-joke that did the rounds in the seventies, possibly from the Real Earth Catalog. It went something like:

    Real programmers don't eat quiche. Real programmers live on Hershey bars, potato chips, coffee and coke.
    Real programmers don't program in Fortran. Fortran is for namby-pamby mothers boys.
    Real programmers don't program in Cobol. Cobol is for people who can't hack Fortran.
    Real programmers don't program in Algol - Algol is for people who don't have to work for a living.
    Real programmers don't program in Basic. Nobody programs in Basic after puberty.
    Real programmers don't document code - if it was hard to write it should be hard to read.
    Real programmers don't waste company time in testing - real programmers test at the console on third shift (and zap changes directly into the executable code).
    Real programmers don't ... etc. etc.

    I am sure that I have totally misquoted the above because I no longer have a copy of the original, but you get the idea. Real programmers work directly in machine code, or possibly in Assembler if they feel like using a development environment.[img]/w3timages/icons/grin.gif[/img]

    I suspect that Geoff and I are from the same generation, hence his distinction between "real" programmers and "professional" programmers.

    By the way, if anybody has the full text of the joke, I would love to have a copy.

  14. #13
    knoway
    Guest

    Re: Microsoft- a pet peeve

    Hi Geoff,

    In spite of my tendency to wander off-topic on occasion, I actually agree with you one hundred percent on this. [img]/w3timages/icons/wink.gif[/img]

    I have always believed that editing was faster than authoring, and will quite shamelessly reuse code that is in the public domain if it saves me time and effort.

    <Flame on>
    Unfortunately, I have found that using code from the Help examples sometimes wastes more time than it saves. A significant number of the examples just plain won't work, and require debugging!

    Good coding requires you to use some form of consistent naming convention, at least within the same project. The Hungarian notation is a pain in the b*tt, but it is better than nothing, and is widely understandable even given all its dialects.

    I think that Microsoft could do a great deal towards the professionality of the profession by setting an example of quality and "best practice".

    I know that Microsoft are not so dumb as to use real programmers to write their help files, but when you see the state of the examples, it makes you wonder about the quality of their production code. No wonder they have so many "issues" and "execution side-effects".
    <flame off>

  15. #14
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Hi Charlotte,

    You're definitely right. You don't need to specify variant- but it's something I do out of habit, to remind myself when I come back to the code that I set that explicitly, I didn't just forget to type it in.

    Type "doc" does suggest document- but I took it as object in my original posting. Both work, but if you're exploring a new variable type, "Document" would have given you much better information on the pop-up properties.

    Samples, yes. But a tiny bit more information on some occasions would have saved me a lot of time.

    There's some sample code in Excel which goes:

    "for each cell in ..."

    When I first came across that, I couldn't find a type which worked, so I ended up coding

    dim i as integer, j as integer
    for i = 1 to range.rows.count
    for j = ...

    That was before I even realised I could define as variant, or in this case, as range.

    If I know the type of a variable, I can use VBA's really useful pop-up properties and methods. If I don't, I have to play a guessing game until I find out.

    They are samples- but fully self-contained samples. The addition of declarations (even as an optional extra- press on a button for all declarations) would make the samples much easier to use.

    My 2 cents worth anyway- California sales tax does not here.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  16. #15
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Gary,

    I certainly will reuse code if I can. My recent attempts at Javascript have only been possible because of the large amount of code posted on the net. And I will rely on help files before I go looking in my books- it's much easier to hit a PF key- then to cut and paste code if it works.

    Hungarian notation is something I tend to use myself- most of all because it's a site standard. There are some who don't agree with it- that's cool. I'll code according to the standards of whatever site I'm on.

    Microsoft, I agree, are inconsistent in advocating Hungarian notation in some places, but ignoring it in their help. But I can spend relatively little time changing variable names to suit whatever convention I am following at the time.

    Obviously they have to use technical writers to produve their help- and the help is extremely comprehensive. If not always correct, it's usually OK. I just object to having to spend, in some cases, a lot of extra time trying to find out the obeject type. It's not because the examples are complete except for the definition- they're not. The examples are (as Charlotte pointed out) only starting points.

    However, sometimes it's not intuitive as to the object type. For instance:

    For Each xlName In Excel.Names
    Debug.Print xlName.Name
    Next

    This will give me named ranges in Excel. I would guess from any standards that xlName would be defined as "Name". If I guessed correctly, I could proceed to find out more properties of the named ranges.

    However "Name" does not work, it should have been defined as "Excel.Name". And it's only as a result of finding out that, that I can find a "RefersTo" property which gives me the information I was looking for to begin with. If the statement "dim xlName as Excel.Name" had been in the sample code, it would have saved me a lot of time.

    Sorry about the hobby horse. Agian, the help files are good, they are only samples- I'm only suggesting a little bit of extra info.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Page 1 of 2 12 LastLast

Posting Permissions

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