Declaring Variables

Discussion in 'Scripting & Programming' started by Fergal1982, Jul 30, 2007.

  1. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    Question for you developer types out there. How do you declare your variables within a Function/Sub/etc? i.e Do you declare them just before using them, or do you have all your declarations at the start of the function?

    Personally I go with the Latter. I just like to have all my declarations in one place. It means that I know where to look for a particular declaration, rather than having to scan through the code.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  2. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Well best practice tends to vary depending on the language.

    For example the original C specification only allowed variables at the beginning, this was loosened in the C++ spec.
    It was then seen as better to define variables close to the point of use.

    I think you need to use whatever scoping tools at your disposal based on the language. I also favour functional decomposition and OO so generally end up with fairly small methods so it does not really matter which approach I use.

    I would however still advise declaring variables at point of first use, this is current accepted best practice for most languages. In fact many variables are implicit these days with people chaining method calls etc. Otherwise you have just artificially expanded the scope for no good reason. Theres really no need to collect your variables in one place in a method, what does this really buy you ? The semantics of the method is what is important the variables on their own are fairly meaningless.
    Its worth having a coding standard and collecting class members in the same place at the top of the class sorted by visibility/scope.
     
  3. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    I agree with dmarsh. It depends on the language. But I'm also old-fashioned, and, as he says, C originaly needed them declared at the start of a function.

    I've got so used to seeing them there that that is where I tend to declare them these days!

    Even with Perl, where you can declare almost anywhere, I still gather them together at the top!

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  4. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    all my programming i done i have always declared them before.. makes your code a lot more readable IMO
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.