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. The Lounge
  3. Straw Poll: Return True or False?

Straw Poll: Return True or False?

Scheduled Pinned Locked Moved The Lounge
questioncsharpc++asp-netcom
101 Posts 58 Posters 0 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.
  • C Offline
    C Offline
    Chris Maunder
    wrote on last edited by
    #1

    Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

    cheers, Chris Maunder

    CodeProject.com : C++ MVP

    The 9 things Microsoft should be announcing at MIX07 (but won't)

    P C P C N 45 Replies Last reply
    0
    • C Chris Maunder

      Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

      cheers, Chris Maunder

      CodeProject.com : C++ MVP

      The 9 things Microsoft should be announcing at MIX07 (but won't)

      P Offline
      P Offline
      Patrick Etc
      wrote on last edited by
      #2

      The typical approach in the .NET Framework seems to be returning false because the item wasn't in the collection and wasn't actually deleted. It's pretty intuitive too, I suppose. If you make the call expecting the item to be deleted, and the return value says it wasn't, you might at least want to log it.


      Cheers, Patrick

      G 1 Reply Last reply
      0
      • C Chris Maunder

        Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

        cheers, Chris Maunder

        CodeProject.com : C++ MVP

        The 9 things Microsoft should be announcing at MIX07 (but won't)

        C Offline
        C Offline
        code frog 0
        wrote on last edited by
        #3

        I voted a 1 but I would have to say that it depends on if you need to do additional processing after the delete. I mean if it doesn't find anything perhaps that means you want to insert a value somewhere else then you'd want to have a false result.


        My name is Maximus Decimus Meridius, Commander of the Armies of the North, General of the Felix Legions, loyal servant to the true emperor, Marcus Aurelius. Father to a murdered process, husband to a murdered thread. And I will have my affinity, in this life or the next. - Gladiator. (Okay, not quite Gladiator but close.) I work to live. I do not live to work. My clients do not seem capable of grasping this fact. Ancient of days! august Athena! where, Where are thy men of might? - Lord Byron

        C 1 Reply Last reply
        0
        • C Chris Maunder

          Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

          cheers, Chris Maunder

          CodeProject.com : C++ MVP

          The 9 things Microsoft should be announcing at MIX07 (but won't)

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Neither? Return a reference to the removed item or null. Throw an exception if necessary.

          1 Reply Last reply
          0
          • C Chris Maunder

            Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            The 9 things Microsoft should be announcing at MIX07 (but won't)

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            If there's any other reason for delete to fail, you need an enum. Otherwise, the only possible answer is false. Otherwise, when would you return false ? You'd return true for found it and deleted it, or true for didn't find it, the return value would be useless.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            C 1 Reply Last reply
            0
            • C code frog 0

              I voted a 1 but I would have to say that it depends on if you need to do additional processing after the delete. I mean if it doesn't find anything perhaps that means you want to insert a value somewhere else then you'd want to have a false result.


              My name is Maximus Decimus Meridius, Commander of the Armies of the North, General of the Felix Legions, loyal servant to the true emperor, Marcus Aurelius. Father to a murdered process, husband to a murdered thread. And I will have my affinity, in this life or the next. - Gladiator. (Okay, not quite Gladiator but close.) I work to live. I do not live to work. My clients do not seem capable of grasping this fact. Ancient of days! august Athena! where, Where are thy men of might? - Lord Byron

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              If you return true for 'I found it and deleted it', and true for 'I couldn't find it, so I didn't have to delete it', what would return false ?

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              C J 2 Replies Last reply
              0
              • C Chris Maunder

                Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

                cheers, Chris Maunder

                CodeProject.com : C++ MVP

                The 9 things Microsoft should be announcing at MIX07 (but won't)

                N Offline
                N Offline
                Nirosh
                wrote on last edited by
                #7

                What other options, you have hidden in-between option number 1 and 5??

                L.W.C. Nirosh. Colombo, Sri Lanka.

                1 Reply Last reply
                0
                • C Christian Graus

                  If you return true for 'I found it and deleted it', and true for 'I couldn't find it, so I didn't have to delete it', what would return false ?

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  C Offline
                  C Offline
                  code frog 0
                  wrote on last edited by
                  #8

                  I don't see where I said return true in both cases. That would be stupid. If you are going to return the same result in either case make it void and don't return anything.


                  My name is Maximus Decimus Meridius, Commander of the Armies of the North, General of the Felix Legions, loyal servant to the true emperor, Marcus Aurelius. Father to a murdered process, husband to a murdered thread. And I will have my affinity, in this life or the next. - Gladiator. (Okay, not quite Gladiator but close.) I work to live. I do not live to work. My clients do not seem capable of grasping this fact. Ancient of days! august Athena! where, Where are thy men of might? - Lord Byron

                  C 1 Reply Last reply
                  0
                  • C Christian Graus

                    If there's any other reason for delete to fail, you need an enum. Otherwise, the only possible answer is false. Otherwise, when would you return false ? You'd return true for found it and deleted it, or true for didn't find it, the return value would be useless.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    C Offline
                    C Offline
                    Chris Maunder
                    wrote on last edited by
                    #9

                    Christian Graus wrote:

                    Otherwise, when would you return false ?

                    What if an incorrect parameter was passed (eg item # -1), or the collection was actually a database table and you coldn't open the table? False could mean "Something bad happened and there's no way the item could be removed", and true "The item is no longer there".

                    cheers, Chris Maunder

                    CodeProject.com : C++ MVP

                    The 9 things Microsoft should be announcing at MIX07 (but won't)

                    C P J 3 Replies Last reply
                    0
                    • C Chris Maunder

                      Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

                      cheers, Chris Maunder

                      CodeProject.com : C++ MVP

                      The 9 things Microsoft should be announcing at MIX07 (but won't)

                      C Offline
                      C Offline
                      Chris Losinger
                      wrote on last edited by
                      #10

                      TRUE, for exactly the reason you gave. if the caller needed to know that the item wasn't in the list to begin with, and needs to act on that info, he should've called collection.Find(item) beforehand.

                      image processing toolkits | batch image processing | blogging

                      C 1 Reply Last reply
                      0
                      • C Chris Maunder

                        Christian Graus wrote:

                        Otherwise, when would you return false ?

                        What if an incorrect parameter was passed (eg item # -1), or the collection was actually a database table and you coldn't open the table? False could mean "Something bad happened and there's no way the item could be removed", and true "The item is no longer there".

                        cheers, Chris Maunder

                        CodeProject.com : C++ MVP

                        The 9 things Microsoft should be announcing at MIX07 (but won't)

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        If you're passing in an item index, then false is going to denote that item index did not exist. If you're passing in a key to remove, then false will indicate the key did not exist. If there's any other reason for failure, then it depends on how specific you want to be. Do you need an enum, or is false OK for all failures ? Either way, true seems wrong to me.

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

                          cheers, Chris Maunder

                          CodeProject.com : C++ MVP

                          The 9 things Microsoft should be announcing at MIX07 (but won't)

                          G Offline
                          G Offline
                          Germyan
                          wrote on last edited by
                          #12

                          what is your vote?? :-> G.

                          1 Reply Last reply
                          0
                          • C Chris Losinger

                            TRUE, for exactly the reason you gave. if the caller needed to know that the item wasn't in the list to begin with, and needs to act on that info, he should've called collection.Find(item) beforehand.

                            image processing toolkits | batch image processing | blogging

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #13

                            When would you return false, in a method that does nothing more than remove an item from a list ? How could removing an item from a list fail, so that the item is still there ?

                            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                            C L 2 Replies Last reply
                            0
                            • C Chris Maunder

                              Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

                              cheers, Chris Maunder

                              CodeProject.com : C++ MVP

                              The 9 things Microsoft should be announcing at MIX07 (but won't)

                              C Offline
                              C Offline
                              Clickok
                              wrote on last edited by
                              #14

                              Chris Maunder wrote:

                              If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exis

                              ArgumentOutOfRangeException[^] The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.


                              For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:

                              C 1 Reply Last reply
                              0
                              • C Chris Maunder

                                Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

                                cheers, Chris Maunder

                                CodeProject.com : C++ MVP

                                The 9 things Microsoft should be announcing at MIX07 (but won't)

                                L Offline
                                L Offline
                                Leslie Sanford
                                wrote on last edited by
                                #15

                                Neither. As a general rule, I don't like methods with side effects to return values. That's not to say that I don't break this rule from time to time, but I try to avoid it if possible.

                                1 Reply Last reply
                                0
                                • C code frog 0

                                  I don't see where I said return true in both cases. That would be stupid. If you are going to return the same result in either case make it void and don't return anything.


                                  My name is Maximus Decimus Meridius, Commander of the Armies of the North, General of the Felix Legions, loyal servant to the true emperor, Marcus Aurelius. Father to a murdered process, husband to a murdered thread. And I will have my affinity, in this life or the next. - Gladiator. (Okay, not quite Gladiator but close.) I work to live. I do not live to work. My clients do not seem capable of grasping this fact. Ancient of days! august Athena! where, Where are thy men of might? - Lord Byron

                                  C Offline
                                  C Offline
                                  Christian Graus
                                  wrote on last edited by
                                  #16

                                  The question was: "If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or" You said you voted '1'. Surely if I call delete, and the item DOES exist, it's going to return true ? And so, if it doesn't exist, it's also going to return true. When will it return false ? I'm not sure I see how removing an item from a list is going to fail, when the item is in there. Or am I missing something ?

                                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                                  C P 2 Replies Last reply
                                  0
                                  • C Chris Maunder

                                    Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

                                    cheers, Chris Maunder

                                    CodeProject.com : C++ MVP

                                    The 9 things Microsoft should be announcing at MIX07 (but won't)

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

                                    Generally I wouldnt return anything. The contract for a delete method is generally that the item identified by the params to the call will not exist in the collection after the call. If it didnt exist it the first place then them method has still done its job. Any other error I'd report via an exception. If the client code needs to know if the object actually existed in the collection prior to the delete method it should use a find method. An exception to this would be where performace is critical and calling a find before a delete would result in two identical costly lookups in the collection in which case I'd return true if it was found and deleted

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      Here's a philosophical question: If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or 5. Return FALSE because since the function couldn't find the item, it couldn't actually delete it. Vote now.

                                      cheers, Chris Maunder

                                      CodeProject.com : C++ MVP

                                      The 9 things Microsoft should be announcing at MIX07 (but won't)

                                      R Offline
                                      R Offline
                                      Ravi Bhavnani
                                      wrote on last edited by
                                      #18

                                      Chris Maunder wrote:

                                      meant to delete an item

                                      The function failed. It must therefore return false. I have spoken. /ravi

                                      C P 2 Replies Last reply
                                      0
                                      • C Clickok

                                        Chris Maunder wrote:

                                        If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exis

                                        ArgumentOutOfRangeException[^] The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.


                                        For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:

                                        C Offline
                                        C Offline
                                        Chris Maunder
                                        wrote on last edited by
                                        #19

                                        That's a bit of a large hammer to weild. What if you vaguely expect an item to be there but another process has removed it just before you do. Is an exception a good idea, considering that in the end it's not so much an error, but more of a "oh well" thing. throw new NotThereShrugException();

                                        cheers, Chris Maunder

                                        CodeProject.com : C++ MVP

                                        The 9 things Microsoft should be announcing at MIX07 (but won't)

                                        C S G 4 Replies Last reply
                                        0
                                        • C Christian Graus

                                          The question was: "If you have a function that is meant to delete an item from a collection and the item you wish to delete doesn't exist, do you: 1. Return TRUE since the final outcome (not having that item) has been fulfilled, or" You said you voted '1'. Surely if I call delete, and the item DOES exist, it's going to return true ? And so, if it doesn't exist, it's also going to return true. When will it return false ? I'm not sure I see how removing an item from a list is going to fail, when the item is in there. Or am I missing something ?

                                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                                          C Offline
                                          C Offline
                                          code frog 0
                                          wrote on last edited by
                                          #20

                                          I think the last 10 words of my last sentence clear that up. "...then you'd want to have a false result..." :-D

                                          C 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