Results 1 to 28 of 28

Thread: Declairing Vars - What's better?

  1. #1
    Stiletto
    Guest

    Declairing Vars - What's better?

    Sups,
    This question is buggin me and Id really like to the answer.
    Lets say I have many functions and subs in my project, and i'm using For-Next loops in those functions/subs.
    My question is, what is the best way to declaire the i var:
    1. Decliare it once in General Declairations as public
    2. Decliare it one by one as private in each function/sub
    ?
    Tnx.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Dim i As Integer inside each and every sub/function... never public counters ! U will mess up sooner or later

    well thats my opinion anyways
    -= a peet post =-

  3. #3
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I declare it in each sub or function. The way my code is written, it wouldn't matter, but that's just the way I like to do it.

    Some people will say that it's possible for it to retain a value and screw things up, but in a For... Next loop, the start and end values are explicitly declared, so that really can't happen.

  4. #4
    Stiletto
    Guest
    1 thing that is good is that if for example i write a function, and then i need to use it in a new project, i can just copy and paste it, without adding and changin the code.
    But I mean as for the memory thing, what is better and what is takin less resources?

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    u will go flat on your nose sooner or later .... just take a look at this EXCELENT sample

    in form
    VB Code:
    1. Private Sub Command1_Click()
    2.     test
    3. End Sub

    in a module

    VB Code:
    1. Option Explicit
    2.  
    3. Private i As Integer
    4.  
    5. Public Sub test()
    6.     For i = 0 To 32
    7.         MsgBox GetSomeData
    8.     Next i
    9. End Sub
    10.  
    11. Private Function GetSomeData() As String
    12.     Dim s As String
    13.     For i = 0 To 3
    14.         s = s & i
    15.     Next i
    16.     GetSomeData = s
    17. End Function

    damn I'm creative

    must be the worst sample ever, but I think(uhh hoping ) my point gets throuhg
    -= a peet post =-

  6. #6
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    the statement "in a For... Next loop, the start and end values are explicitly declared, so that really can't happen" is patently untrue.

    If the for ... next section contains a function call, which it certainly could, and that function uses the same public loop index then you absolutely WILL screw yourself up.

    Thus, NEVER use public loop counter. Peet has it right. If you write enough code over a long period of time and you use public counters, you WILL screw up eventually.

  7. #7
    Stiletto
    Guest
    Creative like nuts! Good example man.
    So ya sayin I should declaire it in each and every function/sub.
    But what about the memory? What is better when it comes to memory if ne?

  8. #8
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by phinds
    the statement "in a For... Next loop, the start and end values are explicitly declared, so that really can't happen" is patently untrue.

    If the for ... next section contains a function call, which it certainly could, and that function uses the same public loop index then you absolutely WILL screw yourself up.

    Thus, NEVER use public loop counter. Peet has it right. If you write enough code over a long period of time and you use public counters, you WILL screw up eventually.
    Sorry for the bad info. I didn't think about it the way Peet used in his example which is why I don't declare public counters - something bad is bound to happen that you didn't think about. I think the memory savings are nil because the variable goes out of scope when you leave the sub anyway.

    Personally, I think it's a waste of time to try to squeeze every last ounce of efficiency with things like this. Most people write the same string all over their apps instead of declaring one constant. That's the sort of thing that should be looked at because strings consume a lot of memory compared to numeric data types.

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by Stiletto
    Creative like nuts! Good example man.
    So ya sayin I should declaire it in each and every function/sub.
    But what about the memory? What is better when it comes to memory if ne?
    never thought of that, and I'm never gonna

    its a no topic today... go back 15 years, and I'd concider it
    -= a peet post =-

  10. #10
    Stiletto
    Guest
    Originally posted by phinds
    the statement "in a For... Next loop, the start and end values are explicitly declared, so that really can't happen" is patently untrue.

    If the for ... next section contains a function call, which it certainly could, and that function uses the same public loop index then you absolutely WILL screw yourself up.

    Thus, NEVER use public loop counter. Peet has it right. If you write enough code over a long period of time and you use public counters, you WILL screw up eventually.
    Yea I was thinkin the way you and peet...but what is better when it comes to memory? is there a difference?

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I suggest that you never use "i" or any other one-character variable names. How much more trouble is it to use something like intIndex? A name like that is self-documenting in two ways in that it describes both its type and use. BTW, "i", "j" and "k" are left over from the old days of Fortran.

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    you won't leave this alone will u ?

    I think (well try to anyway) that declearing i's inside each and every sub is not more memory consuming than using one public i.
    the reason is that the i's inside the subs and function are only "alive" as long as u'r inside the sub / function that its decleared. once your app leaves the sub / function the var goes out of scope and dies...

    am I making any sense at all ?
    -= a peet post =-

  13. #13
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by peet
    you won't leave this alone will u ?

    I think (well try to anyway) that declearing i's inside each and every sub is not more memory consuming than using one public i.
    the reason is that the i's inside the subs and function are only "alive" as long as u'r inside the sub / function that its decleared. once your app leaves the sub / function the var goes out of scope and dies...

    am I making any sense at all ?
    Yes... that's exactly what I said. It was the only part I got right, as a matter of fact.

  14. #14
    Stiletto
    Guest
    Originally posted by MartinLiss
    I suggest that you never use "i" or any other one-character variable names. How much more trouble is it to use something like intIndex? A name like that is self-documenting in two ways in that it describes both its type and use. BTW, "i", "j" and "k" are left over from the old days of Fortran.
    Im always givin actual names to my vars. But as for I, J, K, i'm using them only in for-next loops when i need to count number of loops.

  15. #15
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Marty has a point. I have not gone that far down the road of self diciplin though

    I use i,j,k,x..... have not started using æ,ø,å yet... that is something ?

    often regret it thoug... when comming back to reread my excelent coding
    -= a peet post =-

  16. #16
    Stiletto
    Guest
    Originally posted by peet
    you won't leave this alone will u ?

    I think (well try to anyway) that declearing i's inside each and every sub is not more memory consuming than using one public i.
    the reason is that the i's inside the subs and function are only "alive" as long as u'r inside the sub / function that its decleared. once your app leaves the sub / function the var goes out of scope and dies...

    am I making any sense at all ?
    Yes, you right.

  17. #17
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by cafeenman

    Yes... that's exactly what I said. It was the only part I got right, as a matter of fact.


    oh.. well a good thing cannot be said to often....yadiyadiya...
    -= a peet post =-

  18. #18
    Stiletto
    Guest
    Sorry, some x-posting...

  19. #19
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by cafeenman

    ... I think the memory savings are nil because the variable goes out of scope when you leave the sub anyway....
    again..

    sorry man .. didn't see that at first
    -= a peet post =-

  20. #20
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    It's cool. I wasn't trying to say "I said it first." I was just saying that I wasn't totally screwed up in my response. Just mostly messed up.

  21. #21
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by cafeenman
    It's cool. I wasn't trying to say "I said it first." I was just saying that I wasn't totally screwed up in my response. Just mostly messed up.
    u explained in 2 lines what I used 9 lines to explain

    I have to take some english lessons

    -= a peet post =-

  22. #22
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by Stiletto

    Im always givin actual names to my vars. But as for I, J, K, i'm using them only in for-next loops when i need to count number of loops.
    Here are two examples of the same loop (the first one modified) from an app of mine. I think the second one is better because when I look at the code 6 months from now I don't have to remember what "i" is and I don't have to scroll up the code window to find out. It's a small difference, but I believe it's much more programmer-friendly.
    VB Code:
    1. For i = 0 To frmMain.chkOtherAttr.Count - 1
    2.         If frmMain.chkOtherAttr(i).Value = vbChecked Then
    3.             ' The TranslateXSDDataType routine converts the xml element
    4.             ' types to a string with the Case values.
    5.             Select Case TranslateXSDDataType(LCase$(gcolOtherAttr(i + 1).Name), _
    6.                         TRANS_OTHER_ATTR)
    7.                 Case "TextType"
    8.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
    9.                                                   frmMain.txtOtherAttr(i).Text)
    10.                         If LCase$(frmMain.chkOtherAttr(i).Caption) = NAME_ITEM Then
    11.                             '******* 1.00.001 #5 Start *******
    12.                             frmMain.txtOtherAttr(i).Text = _
    13.                                  CorrectCaseName("attribute", _
    14.                                                  frmMain.txtOtherAttr(i).Text)
    15.                             '******* 1.00.001 #5 End *********
    16.                             If frmMain.tvwXML.SelectedItem.Text <> _
    17.                                frmMain.txtOtherAttr(i).Text Then
    18.                                 ' The user has changed the name of the attribute-element
    19.                                 ' so we need to change the text of the tree node
    20.                                 frmMain.tvwXML.SelectedItem.Text = _
    21.                                                       frmMain.txtOtherAttr(i).Text
    22.                                 gTree.AttributeName = frmMain.txtOtherAttr(i).Text
    23.                             End If
    24.                         End If
    25.                 Case "BooleanType"
    26.                     If frmMain.optOtherYesNo(i).OptYesValue = True Then
    27.                         Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i) _
    28.                                                      .Caption, "yes")
    29.                     Else
    30.                         Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i) _
    31.                                                      .Caption, "no")
    32.                     End If
    33.                 Case "ComboBoxType"
    34.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
    35.                                  frmMain.cboOtherAttr(i) _
    36.                                         .List(frmMain.cboOtherAttr(i).ListIndex))
    37.                 Case "PosIntType"
    38.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
    39.                                                   frmMain.txtOtherAttrPosint(i).Text)
    40.                 Case "FloatType"
    41.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
    42.                                                   frmMain.txtOtherAttrFloat(i).Text)
    43.                 Case Else
    44.                     MsgBox ResolveResString(resUnexpectedData, _
    45.                           "|1", TranslateXSDDataType _
    46.                                 (LCase$(gcolOtherAttr(i + 1).Name), _
    47.                                 TRANS_OTHER_ATTR), _
    48.                           "|2", frmMain.fraMisc(fraOtherAttr).Caption, _
    49.                           "|3", frmMain.chkOtherAttr(i).Caption), _
    50.                           vbExclamation, _
    51.                           ResolveResString(resValidationErrorTitle, _
    52.                                           "|1", frmMain.fraMisc(fraOtherAttr).Caption)
    53.            End Select
    54.         Else
    55.             Call oxmlElement.removeAttribute(frmMain.chkOtherAttr(i).Caption)
    56.         End If
    57.     Next
    VB Code:
    1. For intIndex = 0 To frmMain.chkOtherAttr.Count - 1
    2.         If frmMain.chkOtherAttr(intIndex).Value = vbChecked Then
    3.             ' The TranslateXSDDataType routine converts the xml element
    4.             ' types to a string with the Case values.
    5.             Select Case TranslateXSDDataType(LCase$(gcolOtherAttr(intIndex + 1).Name), _
    6.                         TRANS_OTHER_ATTR)
    7.                 Case "TextType"
    8.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
    9.                                                   frmMain.txtOtherAttr(intIndex).Text)
    10.                         If LCase$(frmMain.chkOtherAttr(intIndex).Caption) = NAME_ITEM Then
    11.                             '******* 1.00.001 #5 Start *******
    12.                             frmMain.txtOtherAttr(intIndex).Text = _
    13.                                  CorrectCaseName("attribute", _
    14.                                                  frmMain.txtOtherAttr(intIndex).Text)
    15.                             '******* 1.00.001 #5 End *********
    16.                             If frmMain.tvwXML.SelectedItem.Text <> _
    17.                                frmMain.txtOtherAttr(intIndex).Text Then
    18.                                 ' The user has changed the name of the attribute-element
    19.                                 ' so we need to change the text of the tree node
    20.                                 frmMain.tvwXML.SelectedItem.Text = _
    21.                                                       frmMain.txtOtherAttr(intIndex).Text
    22.                                 gTree.AttributeName = frmMain.txtOtherAttr(intIndex).Text
    23.                             End If
    24.                         End If
    25.                 Case "BooleanType"
    26.                     If frmMain.optOtherYesNo(intIndex).OptYesValue = True Then
    27.                         Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex) _
    28.                                                      .Caption, "yes")
    29.                     Else
    30.                         Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex) _
    31.                                                      .Caption, "no")
    32.                     End If
    33.                 Case "ComboBoxType"
    34.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
    35.                                  frmMain.cboOtherAttr(intIndex) _
    36.                                         .List(frmMain.cboOtherAttr(intIndex).ListIndex))
    37.                 Case "PosIntType"
    38.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
    39.                                                   frmMain.txtOtherAttrPosint(intIndex).Text)
    40.                 Case "FloatType"
    41.                     Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
    42.                                                   frmMain.txtOtherAttrFloat(intIndex).Text)
    43.                 Case Else
    44.                     MsgBox ResolveResString(resUnexpectedData, _
    45.                           "|1", TranslateXSDDataType _
    46.                                 (LCase$(gcolOtherAttr(intIndex + 1).Name), _
    47.                                 TRANS_OTHER_ATTR), _
    48.                           "|2", frmMain.fraMisc(fraOtherAttr).Caption, _
    49.                           "|3", frmMain.chkOtherAttr(intIndex).Caption), _
    50.                           vbExclamation, _
    51.                           ResolveResString(resValidationErrorTitle, _
    52.                                           "|1", frmMain.fraMisc(fraOtherAttr).Caption)
    53.            End Select
    54.         Else
    55.             Call oxmlElement.removeAttribute(frmMain.chkOtherAttr(intIndex).Caption)
    56.         End If
    57.     Next

  23. #23
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I'm not even going to pretend like I understand that code.

  24. #24
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Wow. That's a lot of code for a little point.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  25. #25
    Addicted Member MegaMan's Avatar
    Join Date
    May 2002
    Posts
    128
    well i usually leave the single letter variables for integer counting. i j and k --and i use x and y for positioning.

  26. #26
    Addicted Member MegaMan's Avatar
    Join Date
    May 2002
    Posts
    128
    Originally posted by cafeenman
    I'm not even going to pretend like I understand that code.
    how can you-- the code references a lot of subs and functions.. who knows what they do

  27. #27
    Stiletto
    Guest
    Originally posted by cafeenman
    I'm not even going to pretend like I understand that code.
    Hehe

  28. #28
    Stiletto
    Guest
    What I mean is that for counting number of loop, I use one letter counters like I,J,H. But when it comes to vars or to count something that i need to use it (say counting number of users logged on), i always use actual names.

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