Results 1 to 7 of 7

Thread: Should all variables be declared at top?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    fort worth texas
    Posts
    11

    Unhappy

    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.

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    It generally looks better if all the variables are declared at the beginning.
    But, it's mainly your choice.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  4. #4
    Guest
    Sometimes you have to declare variables within a subroutine or a function. A regular example of this is Recursion.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Guest
    At Module and Global level, declare them at the top, and at local level, declare them in the procedure.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    fort worth texas
    Posts
    11
    THANKS!

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