Conventions Question

Results 1 to 3 of 3

Thread: Conventions Question

  1. #1
    Join Date
    Dec 1969

    Default Conventions Question

    Hi,<BR>Do you tend to declare al variables at the begining of the page, or do you declare them next to where they are being used?It seems to me that my code is more readable when I declare them next to where there used, but I remember in school they told me to declare all variables at the beginning of the page. Whats the standard way of doing this (if there is one)?

  2. #2
    Join Date
    Dec 1969

    Default I do it at the top.

    To me, it makes them easy to find.<BR><BR>And, if you use a variable in more than one spot, where do you put the variable (in your case)?

  3. #3
    Join Date
    Dec 1969

    Default Depends...

    If the variable is used *throughout* the page, I&#039;d declare it at the top of the page, including a comment about what it is used for.<BR><BR>If the variable is only used for (say) a FOR loop or one branch of an IF block, then I&#039;d declare it in place.<BR><BR>VBScript doesn&#039;t *really* care, unlike other languages. In C++, for example if you do:<BR> for ( i = 1; i &#060; 10; ++i )<BR> {<BR> int j;<BR> if ( i &#062; j ) j = i; // boy is this a stupid thing!<BR> }<BR> someFunction( j );<BR>you will get a compile error! Because "j" was declared inside the {...} block and is *NOT* accessible outside that block.<BR><BR>So with C++ it can make a huge difference where you do the declaration. And there are occasions where you certainly *WANT* the scope to be limited.<BR><BR>But with VBScript, there are only 3 possible scopes for a variable:<BR> -- Entire page<BR> -- Inside a Function or Sub<BR> -- as a Class member<BR><BR>So with VBS, if you do this:<BR> For i = 1 To 10<BR> Dim j<BR> If i &#062; j Then j = i<BR> Next<BR> Response.Write j<BR>then yes, "j" is available outside the loop. And, in fact, the effect is identical to what you&#039;d get if you DIM&#039;ed j at the very top of the page.<BR><BR>So... I&#039;m a lot less fussy about where my variables are declared in VBS than I am in other languages.<BR><BR>

Posting Permissions

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