|
-
Oct 14th, 2000, 02:05 AM
#1
Thread Starter
New Member
Should all variables be declared at the top of a sub?
Or is is better to declare them wherever they are
needed? I was doing the latter but I want to clean up my
code, problem is I don't know what "clean" is.
-
Oct 14th, 2000, 02:39 AM
#2
Guru
It generally looks better if all the variables are declared at the beginning.
But, it's mainly your choice.
-
Oct 14th, 2000, 05:42 AM
#3
transcendental analytic
IT depends on what scope you need, and sometimes you don't even need them!
All modulescope and public variables should be declared at top of each file. That doesn't mean you put types, constants enumerations and declarations in second, just that they are in the declarations sections means that they are in the top of the file, which ends where the first method starts.
Procedure scope variables should be declared at top ofin their procedures. parameters passed to them are automatically on top but next to that line you put the rest.
But some of these don't need to be used, depending on a selectional execution. IF's, select cases and exit subs can cause that. Then you declare the variable inside that if or behind that exit sub, but remember not to use it after the if statement.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 14th, 2000, 06:21 AM
#4
Sometimes you have to declare variables within a subroutine or a function. A regular example of this is Recursion.
-
Oct 14th, 2000, 06:50 AM
#5
transcendental analytic
Actually that means you launch the a subroutine or function from within itself, causing procedure variables to be in several instances. Now if you use a Static variable, you can keep it the same even if you do recursive methods.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 14th, 2000, 08:39 AM
#6
At Module and Global level, declare them at the top, and at local level, declare them in the procedure.
-
Oct 14th, 2000, 08:17 PM
#7
Thread Starter
New Member
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
|