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 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
    • R Ravi Bhavnani

      Chris Maunder wrote:

      meant to delete an item

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

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

      What if it can't find the item but instead returns a ham sandwich? throw new BribeRaviWithGoodiesException

      cheers, Chris Maunder

      CodeProject.com : C++ MVP

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

      R 1 Reply Last reply
      0
      • C code frog 0

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

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

        Yeah, you said 'if you want to do additional processing'. Assuming you do not, assuming the method just does the remove and returns true if the item is removed, or did not exist, then when will it return false, in that instance ?

        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 Christian Graus

          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 Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #23

          you don't return false. actually, you don't return anything. but that wasn't an option. if you can't delete it (because you're about to blow the stack or something), throw an exception. i'm sure you can write an app that needs Delete to tell you if the object was deleted or not (and i probably have), but if we're talking about the Platonic Ideal Delete, i don't think it should tell you anything besides "don't know why, but total failure is imminent".

          image processing toolkits | batch image processing | blogging

          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)

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

            Then an exception would be thrown (or passed along).

            1 Reply Last reply
            0
            • C Chris Maunder

              What if it can't find the item but instead returns a ham sandwich? throw new BribeRaviWithGoodiesException

              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
              #25

              Actually, returning a status is very 60s. Throwing (a la ItemNotFound) would be the way to go. And please, no jokes about ham sandwiches. Mmm... :cool: /ravi

              This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

              T 1 Reply Last reply
              0
              • C Christian Graus

                Yeah, you said 'if you want to do additional processing'. Assuming you do not, assuming the method just does the remove and returns true if the item is removed, or did not exist, then when will it return false, in that instance ?

                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
                #26

                Christian Graus wrote:

                Assuming you do not, assuming the method just does the remove and returns true if the item is removed, or did not exist, then when will it return false, in that instance ?

                Assuming you don't want to do additional processing then my vote for 1.0 applies and that's that. But really... if your not going to do anything else you don't need a result do you? Make it void unless you intend to throw an error in the case it finds nothing. If you don't wish to do additional processing you don't need any return just void it.

                C 1 Reply Last reply
                0
                • C Chris Maunder

                  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 Offline
                  C Offline
                  Clickok
                  wrote on last edited by
                  #27

                  Chris Maunder wrote:

                  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.

                  It depends of the level of control that you desires. Look: TRUE: The item was in the list, and the method deleted it successfully. FALSE: The item was in the list, and the method cannot delete it by some dummy reason. EXCEPTION: The item wasn't in the list, the method cannot start the process of deletion because the argument was invalid. If the method started and the item exists, then you lock the collection and try delete it. Just my two cents. :)


                  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:

                  1 Reply Last reply
                  0
                  • R Ravi Bhavnani

                    Actually, returning a status is very 60s. Throwing (a la ItemNotFound) would be the way to go. And please, no jokes about ham sandwiches. Mmm... :cool: /ravi

                    This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #28

                    ok, how about a hamburger, instead?

                    Silence is the voice of complicity. Strange women lying in ponds distributing swords is no basis for a system of government. -- monty python Might I suggest that the universe was always the size of the cosmos. It is just that at one point the cosmos was the size of a marble. -- Colin Angus Mackay

                    R 1 Reply Last reply
                    0
                    • C code frog 0

                      Christian Graus wrote:

                      Assuming you do not, assuming the method just does the remove and returns true if the item is removed, or did not exist, then when will it return false, in that instance ?

                      Assuming you don't want to do additional processing then my vote for 1.0 applies and that's that. But really... if your not going to do anything else you don't need a result do you? Make it void unless you intend to throw an error in the case it finds nothing. If you don't wish to do additional processing you don't need any return just void it.

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

                      Yeah, ultimately, I don't think a return value is needed or justified, if all you're doing is removing an item. But, the question didn't ask that :-)

                      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 )

                      M 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
                        #30

                        Add a boolean field and property so the user of the collection can specify what he wants.

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          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)

                          S Offline
                          S Offline
                          S Douglas
                          wrote on last edited by
                          #31

                          Chris Maunder wrote:

                          considering that in the end it's not so much an error, but more of a "oh well" thing.

                          So why even return anything at all?


                          1 Reply Last reply
                          0
                          • T TheGreatAndPowerfulOz

                            ok, how about a hamburger, instead?

                            Silence is the voice of complicity. Strange women lying in ponds distributing swords is no basis for a system of government. -- monty python Might I suggest that the universe was always the size of the cosmos. It is just that at one point the cosmos was the size of a marble. -- Colin Angus Mackay

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

                            ahz wrote:

                            ok, how about a hamburger, instead?

                            I got close (too damn close) to a Lick's[^] burger the other day. The bun was eh, the trimmings eh, but the patty... mmm mmm delicious! Highly recommended if you live near one. /ravi

                            This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

                            1 Reply Last reply
                            0
                            • C Chris Maunder

                              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 Offline
                              C Offline
                              Clickok
                              wrote on last edited by
                              #33

                              Another approach to solve your dilemma is ask what "delete" does: (1) Exclude an specified item in a list; (2) Ensure that the specified item will not exists in the list; The sample for item (1) is System.IO.File.Delete()[^] method: Deletes the specified file. An exception is not thrown if the specified file does not exist. The sample for item (2) is SQL DELETE[^]. If does not exists rows that match the where clause, nothing will be deleted, and all ends well. But the definitive reference to you is ICollection.Remove[^]: Return Value true if item was successfully removed from the ICollection; otherwise, false. This method also returns false if item is not found in the original ICollection.


                              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:

                              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)

                                A Offline
                                A Offline
                                Andy Brummer
                                wrote on last edited by
                                #34

                                Actually unless there was some obscene performance reason I'd have no return value and throw an exception for errors. I'd wouldn't care about not finding an object to delete unless there was a good reason to care about it.


                                Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

                                P 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)

                                  J Offline
                                  J Offline
                                  Jerry Hammond
                                  wrote on last edited by
                                  #35

                                  Chris Maunder wrote:

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

                                  Correct me if I'm wrong, but isn't your exception handling supposed to deal with the "Something bad happened"?

                                  "We are all repositories for genetically-encoded information that we're all spreading back and forth amongst each other, all the time. We're just lousy with information." - Neal Stephenson

                                  C 1 Reply Last reply
                                  0
                                  • R Ravi Bhavnani

                                    Chris Maunder wrote:

                                    meant to delete an item

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

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

                                    If it failed it should throw an exception. Maybe you want a TryDelete?

                                    R 1 Reply Last reply
                                    0
                                    • A Andy Brummer

                                      Actually unless there was some obscene performance reason I'd have no return value and throw an exception for errors. I'd wouldn't care about not finding an object to delete unless there was a good reason to care about it.


                                      Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

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

                                      Hear hear

                                      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)

                                        S Offline
                                        S Offline
                                        Shog9 0
                                        wrote on last edited by
                                        #38

                                        Depends entirely on the situation. If i want it gone because i want a collection without it, then TRUE. If i want it gone as part of some user request for it to be gone, then FALSE, because the user provided bad input (either directly specifying a non-existent item, or as some sort of bizarre context thing). If i'm doing just a generic collection of some sort, then nothing - the routine should either always succeed, or throw an exception. The caller should be responsible for keeping things sane. now, on to read the other responses and find out why i'm full of it... ;)

                                        ----

                                        It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.

                                        --Raymond Chen on MSDN

                                        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)

                                          B Offline
                                          B Offline
                                          bryce
                                          wrote on last edited by
                                          #39

                                          true :) because then you know its not there :) like that beer you keep promising - "vapour beer" Bryce

                                          --- To paraphrase Fred Dagg - the views expressed in this post are bloody good ones. --
                                          Publitor, making Pubmed easy. http://www.sohocode.com/publitor

                                          Our kids books :The Snot Goblin, and Book 2 - the Snotgoblin and Fluff

                                          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