Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I was just wondering if anything could be considered wrong with using the following...

while(MyFunction()) {}

Basically the functionality of the while is moved to a seperate function, am I likely to encounter any problem using this syntax, or would it be better todo...

bool isSuccess = true;
while(isSuccess)
{
      isSuccess = MyFunction();
}

As far as I can tell both these sections of code will perform the same, I just thought I'd put the question out incase theres something I'm missing. The last thing I wan't is for my application to start throwing random errors somewhere down the line.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

My opinions (because it's really just going to come down to style preference):

 

The first chunk of code is very C, very macho coder. I like it. My only caveats are that MyFunction is named something useful in real life, and that there aren't any major side effects by calling the MyFunction (in either case, really).

 

I personally think the first code chunk is more readable because everything is contained in the loop invariant. You know right at the top how the loop is affected rather than having to search through the loop to find what modifies isSuccess. I like that you don't have to declare a variable that you're just going to throw away. There isn't a speed concern here because you'll be running the method each time through the loop in either case.

 

The only minor thing is that the two chunks as you wrote them aren't quite equivalent (this is just me being a little nit picky) :)

bool isSuccess = MyFunction();  //the first chunk did the check before going into the loop so this one should too.
while(isSuccess)
{
      isSuccess = MyFunction();
}

Others may make the argument for chunk two, though again, I think it comes down to style -- I don't really see a technical/performance concern.

Posted
I see your point they weren't "Technically" the same :p. It just looks really strange because no extra code is inside the while loop, hence the entire code is proccessed as part of the loop invariant, leading to a single line while statement.
Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

There is absolutely nothing with that. It can be very useful sometimes. For example, just say that instead of using a foreach loop, you decided to be crazy and use the IEnumerator yourself. Instead of writing code like this:

string[] stuff = new string[] {"A", "B", "C"};

foreach(string s in stuff) {
MessageBox.Show(s);
}

You could write code like this:

string[] stuff = new string[] {"A", "B", "C"};

IEnumerator stuffEnumerator= ((IEnumerable)stuff).GetEnumerator();

while(stuffEnumerator.MoveNext()) {
MessageBox.Show((string)(stuffEnumerator.Current));
}

Granted the code is not pretty and not worth the extra effort, but it demonstrates exactly what the While(Function()) type of loop is good for: when an object has a function that performs an action and returns a value that indicates that it should continue to be looped on/enumerated. In fact, this is exactly the pattern that compilers use for enumerable objects other than arrays. This is decompiled code from a foreach on a List<> object.

List<int> list1 = new List<int>();
List<int>.Enumerator enumerator2 = list1.GetEnumerator();
try{
   while (enumerator2.MoveNext())
   {
       int num1 = enumerator2.get_Current();
   }
}

 

[Edit]I guess mskeel writes replies alot faster than I do. And you too, cags.[/edit]

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...