Skipping an iteration, not the loop
-
I had some code that would enumerate through sharepoint lists in a subsite, and i had my code skip out if the list was not the one i was looking for. Code looked something like this:
if (node.Attributes["Title"] != "Announcements") break;
thinking dumbly that with the 'break' clause it would cut out of the loop and go to the next item. But then I finally realized that it was only iterating once before breaking out of the loop entirely, but only after having gone through every single line of code presceding that little if statement, attempting to find the logic errors in the view identification and credentials and other queries on the desired list. After all that work, I realized all I needed to do was change 'break' to 'continue'... -
I had some code that would enumerate through sharepoint lists in a subsite, and i had my code skip out if the list was not the one i was looking for. Code looked something like this:
if (node.Attributes["Title"] != "Announcements") break;
thinking dumbly that with the 'break' clause it would cut out of the loop and go to the next item. But then I finally realized that it was only iterating once before breaking out of the loop entirely, but only after having gone through every single line of code presceding that little if statement, attempting to find the logic errors in the view identification and credentials and other queries on the desired list. After all that work, I realized all I needed to do was change 'break' to 'continue'...Sometimes its the simplist things that mess us up. I've spent hours chasing a bug just to find it was something very simple and was right in front of me. "Theres no place like home....." Mike
Life is not measured by the number of breaths we take, but by the moments that take our breath away. "George Carlin"
-
I had some code that would enumerate through sharepoint lists in a subsite, and i had my code skip out if the list was not the one i was looking for. Code looked something like this:
if (node.Attributes["Title"] != "Announcements") break;
thinking dumbly that with the 'break' clause it would cut out of the loop and go to the next item. But then I finally realized that it was only iterating once before breaking out of the loop entirely, but only after having gone through every single line of code presceding that little if statement, attempting to find the logic errors in the view identification and credentials and other queries on the desired list. After all that work, I realized all I needed to do was change 'break' to 'continue'...There's a simple, guaranteed, way to find bugs like this, just go to your boss or even respected co-worker, tell them you've spent two days wracking your brain over a problem but just can't figure it out. Get them to come to your station and look over your shoulder as you demonstrate the bug. One of three things will then happen: 1) You will immediately see your stupid error and have to apologise for wasting their time. 2) They will immediately see your stupid error and you'll have to apologise for wasting their time. 3) The bug will spontaneously disappear and you'll have to apologise for wasting their time.
-
There's a simple, guaranteed, way to find bugs like this, just go to your boss or even respected co-worker, tell them you've spent two days wracking your brain over a problem but just can't figure it out. Get them to come to your station and look over your shoulder as you demonstrate the bug. One of three things will then happen: 1) You will immediately see your stupid error and have to apologise for wasting their time. 2) They will immediately see your stupid error and you'll have to apologise for wasting their time. 3) The bug will spontaneously disappear and you'll have to apologise for wasting their time.
More often than not, I get #1. As they arrive, I see my stupid error, but I don't apologise, I just thank them. I wasn't wasting their time, because if I didn't call them, I wouldn't have seen the bug.
-
I had some code that would enumerate through sharepoint lists in a subsite, and i had my code skip out if the list was not the one i was looking for. Code looked something like this:
if (node.Attributes["Title"] != "Announcements") break;
thinking dumbly that with the 'break' clause it would cut out of the loop and go to the next item. But then I finally realized that it was only iterating once before breaking out of the loop entirely, but only after having gone through every single line of code presceding that little if statement, attempting to find the logic errors in the view identification and credentials and other queries on the desired list. After all that work, I realized all I needed to do was change 'break' to 'continue'...Be glad goto's are strongly frowned on. I do not miss my VB6 days.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
More often than not, I get #1. As they arrive, I see my stupid error, but I don't apologise, I just thank them. I wasn't wasting their time, because if I didn't call them, I wouldn't have seen the bug.
-
Only problem being, that I don't work with anyone else that knows how to code :(. Everyone else is just IT, install and repair.. I do all the code.
-
Be glad goto's are strongly frowned on. I do not miss my VB6 days.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest HemingwayA well placed label and a goto are no different to a 'continue'. Even 'continue' can be frowned upon in some places. Your conditional should take care of all loop exits blah blah blah.
-
Sometimes its the simplist things that mess us up. I've spent hours chasing a bug just to find it was something very simple and was right in front of me. "Theres no place like home....." Mike
Life is not measured by the number of breaths we take, but by the moments that take our breath away. "George Carlin"
Mike Hankey wrote:
something very simple and was right in front of me
True. Sometimes the innocuous looking little semicolon right under the nose would play the game of a rat and the cat. :-D
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
Be glad goto's are strongly frowned on. I do not miss my VB6 days.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest HemingwayEnnis Ray Lynch, Jr. wrote:
I do not miss my VB6 days
:confused:
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
Ennis Ray Lynch, Jr. wrote:
I do not miss my VB6 days
:confused:
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
Do you?
-
A well placed label and a goto are no different to a 'continue'. Even 'continue' can be frowned upon in some places. Your conditional should take care of all loop exits blah blah blah.
That is a debate I suppose. However, I think break and continue are more self-evident than the corresponding code to terminate a loop early. Of course you qualified your label and goto statement with well placed. The problem was never with well placed labels and goto's as assembly language requires it the problem is non-well placed resulting in spaghetti.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
I had some code that would enumerate through sharepoint lists in a subsite, and i had my code skip out if the list was not the one i was looking for. Code looked something like this:
if (node.Attributes["Title"] != "Announcements") break;
thinking dumbly that with the 'break' clause it would cut out of the loop and go to the next item. But then I finally realized that it was only iterating once before breaking out of the loop entirely, but only after having gone through every single line of code presceding that little if statement, attempting to find the logic errors in the view identification and credentials and other queries on the desired list. After all that work, I realized all I needed to do was change 'break' to 'continue'...Another simple solution, if continue isn't available, is to put everything inside the loop into a method that gets passed any objects (like records or arrays) needed from the loop for processing. Then you can issue a return when you are finished processing that record and the loop will process the next record, skipping over the rest of the method. It also has the advantage of being readily understood by the newbie programmers who might have to maintain your code later.
-
Be glad goto's are strongly frowned on. I do not miss my VB6 days.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest HemingwayYou're alone on that one.
-
Sometimes its the simplist things that mess us up. I've spent hours chasing a bug just to find it was something very simple and was right in front of me. "Theres no place like home....." Mike
Life is not measured by the number of breaths we take, but by the moments that take our breath away. "George Carlin"
Mike Hankey wrote:
"Theres no place like home....."
No, there is home itself, at least. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.