declaring variables - best practice

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
I'm now using C#. Seeing as though you can declare & initialize or pass a
value to a variable on the same line as the declaration, is it still best
practice to group all the variables together at the top of the method, or is
it now acceptable for them to be declared at the point where they are
initially needed, seeing as though you can 'go to the definition', with the
right click of a mouse?
Pedantic I know, but I'm curious.

thanks for your opinions

Ant
 
Ant said:
hi,
I'm now using C#. Seeing as though you can declare & initialize or pass a
value to a variable on the same line as the declaration, is it still best
practice to group all the variables together at the top of the method, or is
it now acceptable for them to be declared at the point where they are
initially needed, seeing as though you can 'go to the definition', with the
right click of a mouse?
Pedantic I know, but I'm curious.

thanks for your opinions

Ant
Coming from vb6 which did not have "proper" scope for variables I really
like the fact that if you declare a variable within an if block then
that variable is limited to that if block etc..

Personally I always declare when needed, not at the top.

JB
 
Ant, there's no reason not to be pedantic; these things matter.

Many people believe that variables should be declared at the top of the
scope they need to be visible in.

The compiler doesn't care where they lie, but for visual scans of the code
it seems to help if they're all grouped together at some predictable place
in the code. Encourages you to use some sort of structured commentary
describing them, lets you see them all in a glance, etc. The more visual
structure you apply to your coding style, in fact, the easier it is for a
reader to use our human pattern-matching abilities to extract information
from the source code. And as we all know, it's much harder to read code than
it is to write it.

Many people also believe that you should declare a variable within the
narrowest scope that it's needed in. So variables declared at the very top
of a method are those that are used throughout the method, those that are
used in inner blocks should be declared at the top of their blocks, and so
forth. One good reason for keeping tight scope on variables is that if an
inner block never gets executed on account of a branch condition, the
runtime never needs to exert itself to instantiate it.

C# is highly block-scoped; VB didn't used to be, but now is.

Iteration variables are best declared, according to Microsoft, in the top of
the iteration:
foreach (string s in substrings) ;
for (int i = 0; i <= something; i++) ;
Or if you like VB.Net:
For Each s As String In substrings
For i As Integer = 0 To something

Naturally, I concur with these opinions, after about a million years in this
biz, or I wouldn't be spreading them around.

In practice, of course, I sometimes bend these rules and plunk down a
declaration in medias res, but I always feel a teensy bit uncomfortable when
I do it. And sometimes I go back and move them. But not always.

HTH,
Tom Dacon
Dacon Software Consulting
 
"" Seeing as though you can declare & initialize or pass a
value to a variable on the same line as the declaration, is it still best
practice to group all the variables together at the top of the method ""

I like to declare & initialize/pass a value to a variable on the same line
as the declaration where i can, and normally at the top of the specific scope
where it is used. I find this helps readability because its fewer lines of
code.
 
Ant said:
I'm now using C#. Seeing as though you can declare & initialize or pass a
value to a variable on the same line as the declaration, is it still best
practice to group all the variables together at the top of the method, or is
it now acceptable for them to be declared at the point where they are
initially needed, seeing as though you can 'go to the definition', with the
right click of a mouse?
Pedantic I know, but I'm curious.

If you can, acquire a copy of Code Complete 2 by Steve McConnell. It's a
great book (I'd call it a 'must-have') which gives advice on things like
this.

The author's idea on this particular subject is that a variable's
declaration should appear as close as possible to its definition, in order
to aid readability. This is then discussed in detail.
 
Tom Dacon said:
Ant, there's no reason not to be pedantic; these things matter.

Many people believe that variables should be declared at the top of the
scope they need to be visible in.

The compiler doesn't care where they lie, but for visual scans of the code
it seems to help if they're all grouped together at some predictable place
in the code.

I agree with that for member variables, but for local variables I far
prefer to see a variable at the point of first use. That way they are
often self-commenting. For instance:

// The name of the recipient for the email address we're about to
// process
string recipientName;

// ...Lots more stuff in the same scope...

recipientName = email.Recipient;
// etc


isn't as readable to me as:

// Lots of stuff in the original scope...

string recipientName = email.Recipient;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top