Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why declare a variable in one line, and assign to it in the next?

I often see in C# code the following convention:

some_type val;
val = something;

like

DataTable dt;
dt = some_function_returning_datatable();

or

DataTable dt = new DataTable();
dt = some_function_returning_datatable();

instead of

some_type val = something;
DataTable dt = some_function_returning_datatable();

I initially assumed that this was a habit left over from the days when you had to declare all local variables at the top of the scope. But I've learned not to dismiss so quickly the habits of veteran developers.

(In my 3rd code section will it not be wastage of memory when we assign dt first with new and then from function)

So, is there a good reason for declaring in one line, and assigning afterwards?

like image 712
Nikhil Agrawal Avatar asked May 08 '12 10:05

Nikhil Agrawal


2 Answers

In my 3rd code section will it not be wastage of memory when we assign dt first with new and then from function

Yes, it will indeed. Only relatively minor - creating a useless DataTable object - but still wasteful and unclear.

So, is there a good reason for declaring in one line, and assigning afterwards?

Only if you don't have the value immediately. For example:

string x;
if (someCondition) {
    // Do some work
    x = someResult;
} else {
    // Do some other work
    x = someOtherResult;
}

Often this can be improved either using a conditional operator or extracting that code into a method. Sometimes it doesn't work out that way though.

For simple situations of:

Foo x = SomeInitializationWithNoUsefulSideEffects();
x = SomeUsefulValue();

Or

Foo x;
x = SomeUsefulValue();

You should absolutely refactor to

Foo x = SomeUsefulValue();

One other interesting situation where the declaration point does make a difference is in captured variables, although normally not the way it's intended:

int x;
for (int i = 0; i < 10; i++) {
    x = SomeFunction();
    actions.Add(() => Console.WriteLine(x));
}

vs

for (int i = 0; i < 10; i++) {
    int x = SomeFunction();
    actions.Add(() => Console.WriteLine(x));
}

In the first snippet, each delegate will capture the same variable, so they would all effectively see the last value returned from SomeFunction. In the second snippet, each delegate will capture a separate "instance" of x.

like image 54
Jon Skeet Avatar answered Oct 05 '22 14:10

Jon Skeet


But I've learned not to dismiss so quickly the habits of veteran developers.

Dismiss them very quickly without worrying or any hesitation. It's completely meaningless to write in C#:

some_type val;
val = something;

instead of:

some_type val = something;

or:

DataTable dt = new DataTable();
dt = some_function_returning_datatable();

instead of:

DataTable dt = some_function_returning_datatable();

Anyway if you don't dismiss them in your code, the C# compiler will dismiss them when emitting the resulting IL.

like image 35
Darin Dimitrov Avatar answered Oct 05 '22 14:10

Darin Dimitrov