Results 1 to 17 of 17

Thread: good coding practice question

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    34

    good coding practice question

    Hello,

    Is it good practice to dim statements at the top of a sub/function even if they are only used under certain circumstances?

    Example 1

    Private sub abc_click(index as integer)
    Dim intAbc as integer
    select case index
    case 1
    for intAbc = 1 to lstcount
    debug.print "hello"
    next intAbc
    Case 2
    debug.print "hello"
    End select



    or this:

    Private sub abc_click(index as integer)

    select case index
    case 1
    Dim intAbc as integer
    for intAbc = 1 to lstcount
    debug.print "hello"
    next intAbc
    Case 2
    debug.print "hello"
    End select

    The second example would be the most memory efficient way, but is that the way to do it?
    Thanks
    Rick

  2. #2
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    AS VB 6 doesn't support block declarations like C/C++ do...therefore it's a good practice to declare variables.. right on the top of the function..

  3. #3

  4. #4
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    I would not say that. I cannot imagine why would you do that.
    You should be declaring variables as and when required.

    Good programmers do not have to go to the top to look for the variables. They can always press Shift + F2 on the variables to go to its declaration and Ctrl + Shift + F2 to come back to the last locations they were.

    But make sure you name your variable properly.
    It should at least tell whether this is a local to a function variable or a module level variable or a global variable. What is the data type of the variable and some sensible name to the variable just to increase the readability.

    I think that should be enough to most of the good programmers.

  5. #5
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    While there are ways to find a variable declared in the middle of code, I would call it "good coding practice" to declare your variables at the top. That's what I've always been taught and it's what I'm most comfortable looking at.

  6. #6
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    I thought i was always taught to declare variables "As and when Required". This is the practice, i think, most programmers use in legendary languages like C/C++.

    I do not know which university did you GO !!!! Just kidding.
    I think its a matter of personal choice(Sometimes Team Choice).

  7. #7

  8. #8
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    If you believe everything what Microsoft Says, Go Ahead. Sure.
    Lest you forget, there is "Generally" word in the statement.

    But good coding practice, as much as i know, says, declare variables as and when required.

  9. #9
    Lively Member
    Join Date
    Jul 2001
    Posts
    81
    Originally posted by techyspecy
    But good coding practice, as much as i know, says, declare variables as and when required.
    I tend to disagree. To me, declaring variables at the top of a procedure provides a great deal of organization within the procedure. Also, I seem to remember someone saying that VB allocates space for all variables used in a procedure when it is first called, so "as and when required" really doesn't come into play as far as memory usage is concerned.


  10. #10
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152

    ?

    i believe this issue has been discussed before! lol

    But to throw in my 2 cents anywayz....
    i Always declare my variables at the top of the procedures.... makes for easy access... dosent get in between the other code.... basically it just helps to organize my code
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

  11. #11
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Guys you can use anything you want.
    I've had enough.

  12. #12
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: ?

    Originally posted by ice_531
    i believe this issue has been discussed before! lol
    Doesn't matter where you declare in sub/function once compiled. Put them at the top for easy reference (unless you want to be un-general, then put your dim statements wherever you dim well please. ).

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    34
    Thanks to everyone for all of the replies. I'll probably stick with them on top because I thinks it's easier to follow and
    Also, I seem to remember someone saying that VB allocates space for all variables used in a procedure when it is first called, so "as and when required" really doesn't come into play as far as memory usage is concerned.
    .
    Thanks again

  14. #14
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    If you want to convince yourself, try running this:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Exit Sub
    3.     If 1 = 0 Then
    4.         Dim Fat As aPIG
    5.     End If
    6. End Sub

  15. #15
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    Declaring variables only when they are required is a better option. In fact this approach ensures tight modular programming. Though VB6 doesn't support variable block scope, VB.NET does. So, you'll feel more comfortable at .NET while you declare variable when they required.

    Cheers!
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  16. #16
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    i always declare my variables at the top of a procedure, its just personal taste, its just makes it easier to read imo.
    Like i never use the comment line at the end of a line either, i always put my comments before a code line, strange i know but just shows that no two peeps actually code the same way

  17. #17
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    hehe.. this thread should be a poll
    -= a peet post =-

Posting Permissions

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



Click Here to Expand Forum to Full Width