Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Where do you put your code? (VB6)

    Would you say its better to put the bulk of your code:

    1. in the event that triggered it (i.e. cmd1_click)?
    2. a private function in the code against a form (so cmd1_click would call it)?
    or
    3. a public function in a seperate module (also so cmd1_click would call it)?

    Just a quick opinion poll sort of thing as I'm sure someone once told me to pass everything out to modules, but that means theres gonna be a whole load of public funcs! No Idea who told me and I dont remember the reasoning they gave - damn my poor memory to hades.

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

    Re: Where do you put your code? (VB6)

    It's a question of reusability and scope. If the code is useful from a variety of places, put it in a public routine in a class or standard module. If its functionality is limited to the present location, put it in a private routine in that location. If you need to call the code assigned to cmd_1 from somewhere else in the form, create a private routine in the form and call it from both locations. If you need to call the code assigned to cmd_1 from somewhere outside the form, put it in a public routine outside the form. The idea is to avoid having one object directly manipulate another, but that sometimes means you have to refactor some code later on when you realize you need it in several places.
    Charlotte

  3. #3
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where do you put your code? (VB6)

    Woohoo, I am doing it right after all!

    At the moment, all of my code that, for example, opens a recordset, runs some SQL, populates a combo box etc is sitting in modules and the bulk of the code specific to that form is sitting in private functions on that form (rather than in the event itself).

    I try and make the actual events as small as possible, other than ease of reading I don't really have a reason why.

    Thanks for your comments Charlotte. As ever, very helpful <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  4. #4
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where do you put your code? (VB6)

    The only "exceptions" I've run across are some of the "auto" macros (open, new etc.) in Office apps. I've noticed that filling these event procedures with code can sometimes be unreliable (reason unknown). I tend to use just a single call to a separate routine in these event procedures.

    I tend to try to use minimal scope and avoid public/ global declarations where possible. I gained a lot from articles like VB.NET: Passing variables with forms, in writing tighter, more "correct" code.

    Alan

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

    Re: Where do you put your code? (VB6)

    I go one step further, I minimize te code in templates and use classes, some in DLLs, some not.

    Here's my entire Normal.dot code

    <code>
    Option Explicit
    Public clsWordVBNormal As WordVBNormal

    Public Sub AutoClose()
    SetupClass
    clsWordVBNormal.clsAutoClose
    End Sub

    Public Sub AutoExec()
    SetupClass
    End Sub

    Public Sub AutoExit()
    SetupClass
    clsWordVBNormal.clsAutoExit
    End Sub

    Public Sub AutoNew()
    SetupClass
    End Sub

    Public Sub AutoOpen()
    SetupClass
    End Sub

    Public Sub ResetToolsOptionsView()
    SetupClass
    clsWordVBNormal.clsResetToolsOptionsView ActiveDocument
    End Sub

    Private Sub SetupClass()
    If TypeName(clsWordVBNormal) <> "WordVBNormal" Then
    Set clsWordVBNormal = New WordVBNormal
    clsWordVBNormal.SetClass Application
    End If
    End Sub
    </code>

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where do you put your code? (VB6)

    Looks very neat & tight. I must try to get my own shop floor into that sort of order one day! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Alan

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

    Re: Where do you put your code? (VB6)

    Ease of reading makes a big difference when you're tracking down a bug! I know people who disagree, but I'd rather deal with one atomic operation at a time, rather than wade through line after line of different operations that handle a particular situation. <img src=/S/nuts.gif border=0 alt=nuts width=15 height=15>
    Charlotte

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Where do you put your code? (VB6)

    I try to keep event code pretty short, and use private functions in the same form. I also have a few Global functions to avoid duplication.

    StuartR

  9. #9
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where do you put your code? (VB6)

    Most of my program is based on databases and alot of the errors come from incorrect/missing data which I try and deal with the best I can in the error handling, but I don't always find it too easy - for that reason, I tend not to use DLLs too much. I've tried before and have never managed to deal with errors that occur in the code in the DLL well enough to pass back to the main app. Sooner or later I end up having to step through the code in the DLL and that is a pain to do!

    I'm all for making code clear. I work with 2 programmers who produce "working" code as quick as they can. Unfortunately neither comment their code. Their error handling is minimal and their indenting varies. One has a widescreen laptop and uses about 2 tabs per indent, so after a couple of Loops, a With and an If - I have to scroll the window to actually see any code! I get slated for programming too slow, but at least its quite easy to debug.

    A new guy starts monday who will be bringing Standards to the company, so hopefully that'll all be a thing of the past soon!

  10. #10
    4 Star Lounger SteveH's Avatar
    Join Date
    Jan 2001
    Location
    Chelsea, Gtr London, United Kingdom
    Posts
    587
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where do you put your code? (VB6)

    <hr>Their error handling is minimal and their indenting varies<hr>

    Sounds like you need to get hold of MZ-Tools and Smart Indenter.
    Steve H
    IT Lecturer/Access Developer
    O2K SR3/O2010; Win7Pro

  11. #11
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where do you put your code? (VB6)

    I've been using MZTools for a few years (I'm the only one though), but that Smart Indenter looks like it could come in really handy, thanks Steve!

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

    Re: Where do you put your code? (VB6)

    > Would you say its better to put the bulk of your code:

    I put in event code ONLY that code that HAS to be in there. If an event handler needs code that could be run elsewhere (e.g. utility code), that code goers elsewhere.
    Example: On click this button, load a listbox from a file of data. Code to read a file of data into a string array, and code to sort/move a string array to a listbox, is utility code and sits in a utility module. The click event code calls the FileToArray procedure and the ArrayToListBox procedure. hence the click event is a two-liner.

    I put in application code ONLY that code that is seen to be unique to that application. If an application needs code that could be run elsewhere (e.g. utility code), that code goers elsewhere.
    Example: In an application to build excel charts and paste them into a PPT presentation, I will have code that arranges charts six to a slide on a presentation, Code to build a chart is utility code and sits in a utility module. The code calls the ArrayToChart procedure and the hence the application is a one-liner.

    I put in utility code any procedure (and now by definition this ought to be a function, not a sub) that might be reusable in some other world. String and number handling, file and document/workbook handling, INI file management, you name it, a humungous library.

    Most of my applications in Word rely on a library of utility code - UW.DOT - which dramatically reduces my development time. Excel application uses a library UX.XLA.
    The library code is well-used and hence well-tested (but sadly, NOT error-free!).
    Application and userform code is usually pretty small and tight, and as we know, the fewer moving parts something has, the less there is to go wrong.

    Bonus: When I enhance a utility library procedure, all applications benefit.

    Hope this helps.

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Where do you put your code? (VB6)

    > Bonus: When I enhance a utility library procedure, all applications benefit.

    Drawback: When you enhance a utility library procedure, all applications need re-testing.

    StuartR

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

    Re: Where do you put your code? (VB6)

    > all applications need re-testing.
    I know what you mean, and it is a failing of mine.
    Theory is that if the utility (procedure)'s interface is well-defined, and an enhancement adheres to that interface, it should have no impact on existing code.
    I'm human and fallible, and my interfaces are not always the best.
    FWIW My Word utility library is tested throughout the day, since I have 10 applications in constant use, each dependent on UW.dot, and another 30 or so that get fired up on demand.

  15. #15
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Where do you put your code? (VB6)

    And speaking of error handling, the VB implementation of error handling might be a consideration on how you break up your code too. An error in a procedure can only be handled within that same procedure, so tracing back what might have called the procedure can add to debugging complexity.

    Alan

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
  •