|
-
Sep 10th, 2002, 07:54 PM
#1
Thread Starter
Member
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
-
Sep 10th, 2002, 08:08 PM
#2
Frenzied Member
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..
-
Sep 10th, 2002, 08:09 PM
#3
Yes. It is traditional to Dim them all at the top and not only is it easier find them there, that is where most programmers will look.
-
Sep 10th, 2002, 08:25 PM
#4
Let me in ..
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.
-
Sep 10th, 2002, 08:47 PM
#5
Addicted Member
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.
-
Sep 10th, 2002, 08:51 PM
#6
Let me in ..
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).
-
Sep 10th, 2002, 09:03 PM
#7
Here is what Microsoft Help has to say
Note When you use the Dim statement in a procedure, you generally put the Dim statement at the beginning of the procedure.
-
Sep 10th, 2002, 09:07 PM
#8
Let me in ..
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.
-
Sep 10th, 2002, 09:11 PM
#9
Lively Member
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.
-
Sep 10th, 2002, 09:15 PM
#10
Frenzied Member
?
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++
-
Sep 10th, 2002, 09:17 PM
#11
Let me in ..
Guys you can use anything you want.
I've had enough.
-
Sep 10th, 2002, 10:32 PM
#12
Fanatic Member
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. ).
-
Sep 10th, 2002, 11:13 PM
#13
Thread Starter
Member
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
-
Sep 10th, 2002, 11:28 PM
#14
Fanatic Member
If you want to convince yourself, try running this:
VB Code:
Private Sub Command1_Click()
Exit Sub
If 1 = 0 Then
Dim Fat As aPIG
End If
End Sub
-
Sep 11th, 2002, 03:29 AM
#15
Fanatic Member
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.
-
Sep 11th, 2002, 06:24 AM
#16
Fanatic Member
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
-
Sep 11th, 2002, 06:40 AM
#17
-= B u g S l a y e r =-
hehe.. this thread should be a poll
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|