frenchie
frenchie

Reputation: 51927

c# avoiding variable declaration

Suppose I have some code like this:

public string SomeMethod(int Parameter)
{
  string TheString = "";

  TheString = SomeOtherMethod(Parameter);

  return TheString;
}

Of course, this code is equivalent to this:

public string SomeMethod(int Parameter)
{
  return SomeOtherMethod(Parameter);
}

I think the first version is more readable and that's how I'm writing my code, even thought I'm using a variable when I know I could avoid it. My question is this: does the compiler compile the code in the same way (ie same performance) or is the second option really better in terms of performance.

Thanks.

Upvotes: 12

Views: 1483

Answers (3)

Dhananjay
Dhananjay

Reputation: 3793

Answer is already posted though let me give a different try :

There are 3 things that you are looking for :

Readability, Performance, usefulness (such as debugging, logging etc..)

1.Readability is somewhat relative. What Eric Lippert / Jon Skeet finds something more readable , same thing will not be applicable to me. More and more you code, many things and your perspective will change toward looking at the code.

Both choices you gave are readable , for me second is more readable.

2.Performance : In the first choice , as you might me aware of string immutability that if you reinitialize a string it will not clear earlier name (interning) and it will create new string and the variable will point it to it.

So from performance perspective intializing a variable to new value (unnecessarily) will cause performance bottleneck. Again this is relative, and depends on size/coplexity of the application. For this you need to go with second option. Your second option and Jon's answer will result into same performance.

3.Debugging perspective : you would want to have local variable if you are looking for this stuff.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500055

I'd say the first form is less readable and it contains a redundant initializer. Why initialize the variable to "" if you're about to give it a different value? At least change it to:

public string SomeMethod(int parameter)
{
  string returnValue = SomeOtherMethod(parameter);    
  return returnValue;
}

or if you really want to separate declaration from initialization:

public string SomeMethod(int parameter)
{
  string returnValue;
  returnValue = SomeOtherMethod(parameter);    
  return returnValue;
}

(Note that I've also adjusted the named to follow .NET naming conventions and to give a more meaningful name to the local variable -"TheString" conveys no useful meaning.)

You really won't see any performance problems from using the local variable, but I'd really encourage you to think about the readability. What is the purpose of the local variable here? You'd presumably describe the method as: "Returns the result of calling SomeOtherMethod with the given parameter" - at which point, the one-line version implements exactly that description.

Upvotes: 20

Mark Byers
Mark Byers

Reputation: 838076

The compiler will produce very similar code for your two examples. One slight modification though is to avoid initializing to an empty string that you never use.

public string SomeMethod(int Parameter)
{
    string result;
    result = SomeOtherMethod(Parameter);
    return result;
}

I'm not sure rewriting the code in this way makes it more readable, but it does mean that you can add a breakpoint and see the value of result before the method returns. This can be useful when debugging.

Note you can combine the first and second line and still get this benefit:

public string SomeMethod(int Parameter)
{
    string result = SomeOtherMethod(Parameter);
    return result;
}

I think this last version is both highly readable and easy to debug.

Upvotes: 13

Related Questions