Results 1 to 8 of 8
  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

    Re: Code: Function strAlphaOnly(strIn As String) As St

    Chris,

    I'd really have to ask if it's worth while to to have a function to wrap another function?

    If strOnly is properly documented and tested, might it confuse a coder to have to drill down one more level to work out what's happening- especially when there's just a single line of code?


    I've just had to pick up an application this last week in which everything was extremely "well-structured". It's a real B** to work out what's happening. There's 2 lines of code- "perform initialise" and "perform main". "Main" has 4 lines of code. I've had to drill sown 5 levels to find out wht the routine is doing- where, when I've restructured (for major mods), it's taken 30 lines of code, and no drill-down whatsoever.

    Sorry Chris, it's touched a nerve.

    I guess there's a fine line between having code which you know well and can use generically- and codse which has to be maintained by other people, and which
    must be very straight-forward.

    My [img]/w3timages/icons/2cents.gif[/img][img]/w3timages/icons/2cents.gif[/img] worth.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

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

    Code: Function strAlphaOnly(strIn As String) As St

    This should go into a module STRNGS or similar; it will be a large module when we are finished.

    Please see "Code: getting started" for various tips on VBA.

    Public Function strAlphaOnly(strIn As String) As String
    ' Procedure : strAlphaOnly
    ' Description: This code returns only alphabetic characters of strIn.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: A data string.
    ' Returns: STRING.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    strAlphaOnly = strOnly(strIn, strcAlpha)
    'Sub TESTstrAlphaOnly()
    'MsgBox strAlphaOnly("alphabet") ' regular use
    'MsgBox strAlphaOnly("") ' empty source string
    'MsgBox strAlphaOnly("alphabetand1digit") ' strip digits
    'End Sub
    End Function

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

    Re: Code: Function strAlphaOnly(strIn As String) As St

    Geoff,

    I can't tell which description you would apply to what you described! [img]/w3timages/icons/crazy.gif[/img]

    I tend to make my code very modular because it's hard to wade through long procedures and see at a glance what they're doing, and readability and maintainability are big issues for me. I've inherited too many projects containing long, criminally convoluted procedures, to commit them myself.[img]/w3timages/icons/grin.gif[/img] It's extemely rare for my standard procedures in Access to contain more than 200 lines, and half of those are comments. The usual size is half that or less.

    If called procedures are given self-explanatory names, the calling procedure should pretty much document itself as it goes along. If you see a call to ReturnFolderPath(), it's a lot easier to understand what's happening than if you have to wade through whatever technique was used to accomplish it. Designing code that way also means that I have the flexibility of changing the method ReturnFolderPath() uses without having to change my calling routine at all. In my book, that's worth a little drill down!
    Charlotte

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

    Re: Code: Function strAlphaOnly(strIn As String) As St

    Charlotte,

    I guess I'm having a reaction to work I've inherited.

    The programmer believes that 10 lines is a maximum for a routine- even when there's no loops of other complex logic.

    So routines which are taking me a little while to decipher are merely one line of code, 20-40 lines long when deconstructed, broken into 5 levels. And they include "goSub" within Subs.

    So I guess I'm just having a bad reaction to that.

    What's an optimum number of codes for a routine?

    That probably depends on complexity- and the person reading it.

    If it was just sequential- with maybe a couple of one or two level "if" statements, 100 lines is easy. If it gets more complex, perhaps 20 lines.

    And I guess it's fair enough that a well-named routine (such as Chris's) can do a lot for readability.

    I'm just reacting against code I'm having toruble with now.

    Thanks for your thoughts
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

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

    Re: Code: Function strAlphaOnly(strIn As String) As St

    >I'd really have to ask if it's worth while to to have a function to wrap another function?

    You're right; probably not.

    Such development probably reflects several traits

    1) My continuing quest to strive for functions which collapse two arguments to one result

    2) My (lazy) desire to use something I can remember (strAlphaOnly) rather than having to type two things that I have to remember (strOnly and strcAlpha)

    3) There's most likely remnants of my original approach to these problems, many of the functions are collections that result from dis-assembling an unieldly function. The effect of this is sometimes to create weird little things that are runt-offspring.


    An example of this would have been seen if you'd been watching me yesterday. I was cleaning up strGP and strPP prior to posting and noticed about six lines of common code. I extracted them and formed a new function strPPS.

    I think the fine line you mentioned (I agree) is between creation and maintenance. I don't manage it vey well, and probably never will. Looking back over the years, I've probably spent a great deal more time maintaining existing code that wasn't written well in the first place, than I have building applications out of existing code, and that's not good.

    The best code I seem to write is code that's so simple it *can't* fail - and that leads me back to writing very small functions that are so small there's nothing to go wrong.

    End result of that is I never *have* to go back and maintain.

    I suspect that once we have this basic set of functions out of the way, you'll (all) see what I see - that I regularly just call strGP and never have to worry about what's under the hood. The frantic revision - especially in documentation and classification - that's going on back here is a direct result of having my VBA code exposed to the public for the first time, sort of.

    Sorry to ramble. I agree with your thoughts; I suspect that no two people would ever agree on the position of the fine line. It's good to agree that we are sometimes unbalanced!!!


    (later)

    You may remember I was posting a boolMaskCompare or similar function a few months back. A lounger pointed out that the LIKE operator did what i wanted (I'd never thought VBA could be so clever and had just coded my own). Discoveries like that are easy to implement if the code consists of small watertight compartments; more difficult to implement if the code is complex.

    Another example would be the "install templates" thread. There's a procedure posted there which could have some internal code partitioned into smaller procedures with arguments. In that form it would be much easier to sugegst improvements to the small chunk of code without unbalancing the larger function, because the small chunk of code has a well-defined interface, namely the arguments and the result returned.

    Hope this makes sense

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

    Re: Code: Function strAlphaOnly(strIn As String) As St

    >changing the method ReturnFolderPath() uses without having to change my calling routine
    at all. In my book, that's worth a little drill down!

    That's what I was trying to say when I said "well-defined interface". You speak ENGLISH!

  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: Code: Function strAlphaOnly(strIn As String) As St

    >The programmer believes that 10 lines is a maximum for a routine

    But see strBreakFileString and you'll feel a lot better (grin!)


    >What's an optimum number of codes for a routine?

    In my youth it was held to be the number of lines (25) on a coding pad, whether that was an assembly-language, FORTRAN or COBOL coding pad. If you had to turn the page, you couldn't comprehend the entire module. I didn't actually WRITE like that then, but that was how I was SUPPOSED to write.

    Then VDUs came along, and the standard changed to be the number of lines that could be displayed on a VDU (25, plus ca change!). I still could understand the standard, but didn't follow it.

    Then one day I had to go back and modify my own code.



    >If it was just sequential- with maybe a couple of one or two level "if" statements, 100 lines is easy.

    Let me know what you think of strBreakFileString. That's HORRIBLE to read if you don't have the state transition table. The code works (I think!) because it is a literal translation of a table that DOES fit onto one page. That one-page table is in effect my procedure. Literally transcaribing it to 100 lines of code maintains the logic and structure, even though you'll have worn out the scroll bars on the RHS of your monitor by now .....



    >I'm just reacting against code I'm having toruble with now.

    There are standard techniques for decomposing ugly program code - any language. One of my greatest achievements was ripping 1,400 lines of code out of a 4,200 line COBOL program and demonsrating that the program still worked.

    Do you want to post/email a chunk of code? I'd be happy to disect it. And I *love* criticising other people's work (grin!)

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

    Re: Code: Function strAlphaOnly(strIn As String) As St

    <hr>And I *love* criticising other people's work (grin!)
    <hr>

    Yeah, it's much more fun than having them criticizing yours![img]/w3timages/icons/grin.gif[/img]
    Charlotte

Posting Permissions

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