Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Skipping an iteration, not the loop

Skipping an iteration, not the loop

Scheduled Pinned Locked Moved Clever Code
sharepoint
15 Posts 10 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    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'...

    Mike HankeyM Offline
    Mike HankeyM Offline
    Mike Hankey
    wrote on last edited by
    #2

    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"

    V CPalliniC 2 Replies Last reply
    0
    • L Lost User

      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'...

      K Offline
      K Offline
      Kyudos
      wrote on last edited by
      #3

      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.

      B 1 Reply Last reply
      0
      • K Kyudos

        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.

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #4

        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.

        L 1 Reply Last reply
        0
        • L Lost User

          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'...

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #5

          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

          B V C 3 Replies Last reply
          0
          • B Brady Kelly

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            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.

            C 1 Reply Last reply
            0
            • L Lost User

              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.

              C Offline
              C Offline
              codemunch
              wrote on last edited by
              #7

              That's the beauty of it - wether they can code or not is irrelevant :) The bug always manifests itself with this technique.

              1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                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

                B Offline
                B Offline
                Brady Kelly
                wrote on last edited by
                #8

                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.

                E 1 Reply Last reply
                0
                • Mike HankeyM Mike Hankey

                  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"

                  V Offline
                  V Offline
                  Vasudevan Deepak Kumar
                  wrote on last edited by
                  #9

                  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

                  1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    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

                    V Offline
                    V Offline
                    Vasudevan Deepak Kumar
                    wrote on last edited by
                    #10

                    Ennis Ray Lynch, Jr. wrote:

                    I do not miss my VB6 days

                    :confused:

                    Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                    B 1 Reply Last reply
                    0
                    • V Vasudevan Deepak Kumar

                      Ennis Ray Lynch, Jr. wrote:

                      I do not miss my VB6 days

                      :confused:

                      Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                      B Offline
                      B Offline
                      Brady Kelly
                      wrote on last edited by
                      #11

                      Do you?

                      1 Reply Last reply
                      0
                      • B Brady Kelly

                        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.

                        E Offline
                        E Offline
                        Ennis Ray Lynch Jr
                        wrote on last edited by
                        #12

                        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

                        1 Reply Last reply
                        0
                        • L Lost User

                          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'...

                          D Offline
                          D Offline
                          Draugnar
                          wrote on last edited by
                          #13

                          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.

                          1 Reply Last reply
                          0
                          • E Ennis Ray Lynch Jr

                            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

                            C Offline
                            C Offline
                            Collin Parker
                            wrote on last edited by
                            #14

                            You're alone on that one.

                            1 Reply Last reply
                            0
                            • Mike HankeyM Mike Hankey

                              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"

                              CPalliniC Online
                              CPalliniC Online
                              CPallini
                              wrote on last edited by
                              #15

                              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.

                              In testa che avete, signor di Ceprano?

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups