tags:

views:

508

answers:

4

Should we declare the variable and initialize it in the same line..

Dim x as string = "dummy"

OR

Dim x as string
x = "dummy"

I have seen some people are fan of declaring everything at the start of the routine and then using them later in the code (for more readability) and others who will initialize variables at the beginning wherever they can.

A: 

Do whichever suits you best.

Mark Probst
+6  A: 

Edit: I've voted to reopen the question; it's perfectly valid, there are actual important difference between the two approaches, and if two different programs that had the same behaviour at runtime are considered equivalent, then there's no need to learn about methods/functions, generics/templates, classes, or anything else that exists largely to help you structure a program to make it more maintainable.

To advise the original poster to stop thinking carefully about this, and just get on with churning out code, is very bad advice.

Declaring all variables at the start of a function is a habit left over from C, where it was required by the compiler.

It's far better to declare and initialize at the same time; the namespace of variables shouldn't be poluted with things that aren't needed yet, and which haven't been initialized yet.

The key point is degrees of freedom, which in this context is the number of ways in which something can go wrong. The gap between declaration:

MyClass myRef;

and initialization:

myRef = new MyClass();

is a region in which myRef is presumably not being used in the current version of the software, but where it has the value null. It's like a loose end that has been left dangling on the floor to trip people up.

There's a connection between those two lines of code; they make no sense unless they happen in that order, one after the other - imagine them connected by a piece of cable. So why stretch them apart and put them on opposite sides of the room?

Eventually your code will end up like this.

Update: It's been suggested this isn't significant because it has no effect on the functionality of the software - it's purely a matter of style, like where you put the curly-braces.

It's seriously nothing like that. Here's a fictional suggestion that would have no effect on the functionality of many programs:

TIP: put all the code for your whole program into a single giant class, with hundreds or even thousands of fields and methods. This way, you save a lot of pointless effort declaring many separate classes - not to mention the effort of thinking of names for the classes. Make sure the fields are all private, because that's important for encapsulation!

Does anyone really think we shouldn't critique a suggestion like that, just because it doesn't affect the functionality of a program? This question is about a small-scale, but no less important, version of the same problem.

Daniel Earwicker
+2  A: 

How declaring everything in the beginning makes the code more readable?

In my opinion, declaring each variable only when you need it is better.

Dikla
A: 

Pick a style that works with your language of choice and base standard of choice. Compilers and run time parsers are vastly different .. what language are you using? Your title is generic, yet your example is VB .. and you did not tag this with any specific language. It boils down to what the parser understands (and agrees with).

I'm actually voting to close this, my reply is only documentary so others can learn how to write well formed questions.

Tim Post