12

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.

4
  • I you want to have " string TheString = ""; " consider using string.Empty Commented Apr 7, 2012 at 17:37
  • 1
    How is the first form possibly more readable? It takes longer to read and there is more to process. Commented Apr 7, 2012 at 19:16
  • @alpha123: I have all my variables declared at the top of the method Commented Apr 7, 2012 at 20:05
  • 1
    IMO, it's not a good practice to declare variables at the top of the method. You should declare them exactly before you use them. Commented Apr 9, 2012 at 7:38

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

7 Comments

The code I provided is really a simplification: the returnValue variable goes through several if statements and sometimes other function calls. I'll use string TheString; instead of string TheString = ""; to initialize. I got into the habit of using The in front of my variable names so that I can find them easily with intellisense and none are actually named TheString but rather TheCounter or ThePermission.... Thanks for the answer.
Totally agree, I wish to add. I require that, at least every public method, property or class, should be described in its functionality, parameters and return value expected using the XML comment.
@frenchie: It's hard to give advice on code which is completely different to the example you've given. I'd also suggest that if you're finding your local variables hard to keep track of, your methods are probably longer than they should be. You shouldn't need to resort to prefixes to understand your code.
@Steve: While I generally agree, it's pretty rare to bother including such comments on Stack Overflow, at least when the topic isn't directly related to XML documentation.
@frenchie: Are you looking for variable = condition ? SomeMethod() : String.Empty?
|
13

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.

3 Comments

ok, thanks for the initialization tip: I'll remove the = ""; from my code.
In some cases, I have an initialization with = ""; and then I have the assignment in an if statement. If the if statement never gets executed, then the return variable is just equal to "". So in some cases, I really need to have string TheString = ""; Is there another way of doing it?
@frenchie: You could use theString = null in the else block. In most cases you can refactor the code to avoid having to temporarily initialize variables to null before setting the real value, but you'd have to post your specific code before I can comment on it.
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.