tags:

views:

250

answers:

8

Which is the right approach and why?

string initializeme = string.Empty;
StringBuilder AppendToMe = new StringBuilder();

for(int i=0; i < 10; i++)
{
    initializeme = string.Empty; //Is this the right place to initialize?

    if(expressionThatEvalsTrue)
      initializeme = SomeMethodReturningString();

    if(!string.IsNullOrEmpty(initializeme)
      AppendToMe.Append(initializeme);
}

or

string initializeme = string.Empty;
StringBuilder AppendToMe = new StringBuilder();

for(int i=0; i < 10; i++)
{
    if(expressionThatEvalsTrue)
      initializeme = SomeMethodReturningString();

    if(!string.IsNullOrEmpty(initializeme)
       AppendToMe.Append(initializeme);

    initializeme = string.Empty; //Is this the right place to initialize?

}
A: 

Will you be using this variable after the loop completes? The only difference I can see is that if you need to retain the last value of that variable, the first does while the second does not.

BFree
No I will not be using the variable after the loop is completed
Nick
+5  A: 

There are three factors here:

  • Are you going to capture the variable in the loop using a lambda expression or anonymous method? If so, do you want to capture a single variable, or a separate one per iteration?
  • Do you need the value after the loop?
  • Do you need the current value in the next iteration of the loop?

I generally declare variables in the smallest possible scope, and try to initialize them immediately:

for(int i=0; i < 10; i++)
{
    string initializeme = expression ? SomeMethodReturningString() : "";    
    Console.WriteLine(initializeme);    
}
Jon Skeet
In which case, why not just inline the whole thing:Console.WriteLine(expression ? SomeMethodReturningString() : "");
Carl Manaster
Interesting to see that you have chosen to represent an empty string with "" instead of string.Empty. Is there a reason you chose to do this?
Nick
Hmm... *possibly* - but I think the extra variable here does make it slightly more readable in this case.
Jon Skeet
@Nick: See http://stackoverflow.com/questions/263191 and http://stackoverflow.com/questions/1028170
Jon Skeet
@Jon - Holy cow!! I just emailed my dev team to not use "" and use string.Empty instead. Hope they don't "google" it!!! Thx for the links
Nick
I will concatenate the variable to a stringbuilder declared outside the loop.
Nick
In which case you still don't need the variable itself declared outside the loop.
Jon Skeet
I have vague recollections of string.empty and "" having a meaningful differences in some special case where "" was better because it "" was directly known at compile time rather than being only sort of known at compile time. I don't recall the source, unfortunately. I have a feeling it related to anonymous types or templates.
Brian
+1  A: 

The best way to do it is like this:

for (int i = 0; i < 10; i++)
{
 string initializeme = string.Empty;
 if (expressionThatEvalsFalse)
  initializeme = SomeMethodReturningString();

 Console.WriteLine(initializeme);
}

If you use initializeme outside the loop, you'll need to declare it outside the loop.

SLaks
+3  A: 

I prefer this:

for(int i=0; i<10; i++)
{
    var initializeme = expression
                         ? SomeMethodReturningString()
                         : string.Empty; 
    Console.WriteLine(initializeme);
}

The reason I prefer this style is that it is self-contained: the variable is only set in one place. Doing it inside the loop reduces the scope of the variable as well, which is also to be preferred. I like the use of the extra variable in this case because using the ternary operator as a parameter, IMO, makes the function call less readable.

tvanfosson
+1  A: 

I would declare the variable inside the loop and assume that the compiler is smart enough not to create slow code from this:

for(int i=0; i<10; i++)
{
    string initializeme = string.Empty;
    if(expressionThatEvalsFalse)
      initializeme = SomeMethodReturningString();

    Console.WriteLine(initializeme);
}
sth
+1; exactly what I was going to say (but 49 seconds faster!)
Carl Manaster
+2  A: 

Modern compilers convert code to static single assignment form, in which every variable is assigned exactly once, so there's no excuse not to declare new variables whenever convenient, as in each iteration of a loop.

Bruno Martinez
A: 

in the code snippets i can see no reason to initialize in the first place. The two snipperts will differ after the loop where the initializeme is either potentially initialized to a value (snippet one) or empty string (snippet two. So the premis for the below code is that you only need the initializeme in the loop

StringBuilder AppendToMe = new StringBuilder();
for(int i=0; i < 10; i++)
{    
    if(expressionThatEvalsTrue)
      AppendToMe.Append(SomeMethodReturningString());          
}
Rune FS
A: 

Neither in my opinion. In both cases I would do the following.

Replace

initializeme = string.Empty; //Is this the right place to initialize?

if(expressionThatEvalsTrue)
  initializeme = SomeMethodReturningString();

With

initializeme = expressionThatEvalsTrue ? 
  SomeMethodReturningString() : string.Empty

This way if your expressionThatEvalsTrue is true you get the string from the method, othewise it will be set to blank.

smehaffie