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 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
    realJSOP
    wrote on last edited by
    #57

    I'm old and still writing C++ - I'd return integer that would indicate this way 0=success, 1=failed/invalid index, 2=failed/item doesn't exist, and I'd set up some nifty constants to match the possible return values so the code was more maintainable/readable.

    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

    1 Reply Last reply
    0
    • C Christian Graus

      OK, under what circumstances is that possible ? I have a linked list of any type, under what circumstances would you expect I'd be unable to remove an item ?

      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 )

      J Offline
      J Offline
      jlwarlow
      wrote on last edited by
      #58

      Christian Graus wrote:

      OK, under what circumstances is that possible ? I have a linked list of any type, under what circumstances would you expect I'd be unable to remove an item ?

      It would depend on the items in the list, if these items referenced a resource or database entry for example, and in this case the resource was unreachable or database was locked etc. Then you could fail to delete. Another example would be if the entry had a reference count that was higher than two meaning it was still in use. On reflection, throwing an exception would be a preferable to give a reason why the item could not be deleted. I think this has been mentioned in other replies in the thread.

      Never argue with an imbecile; they bring you down to their level, and beat you with experience.

      C 1 Reply Last reply
      0
      • J jlwarlow

        Christian Graus wrote:

        OK, under what circumstances is that possible ? I have a linked list of any type, under what circumstances would you expect I'd be unable to remove an item ?

        It would depend on the items in the list, if these items referenced a resource or database entry for example, and in this case the resource was unreachable or database was locked etc. Then you could fail to delete. Another example would be if the entry had a reference count that was higher than two meaning it was still in use. On reflection, throwing an exception would be a preferable to give a reason why the item could not be deleted. I think this has been mentioned in other replies in the thread.

        Never argue with an imbecile; they bring you down to their level, and beat you with experience.

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

        An exception can be expensive, it all depends on how often it may occur. But yes, I said from the start that it depends on the complexity of hte method. Interesting discussion that Chris started.

        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 )

        J 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
          Aamir Butt
          wrote on last edited by
          #60

          I will return S_FALSE :)

          "Some people believe football is a matter of life and death. I'm very disappointed with that attitude. I can assure you it is much, much more important than that. -- Bill Shankly"

          1 Reply Last reply
          0
          • C Christian Graus

            An exception can be expensive, it all depends on how often it may occur. But yes, I said from the start that it depends on the complexity of hte method. Interesting discussion that Chris started.

            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 )

            J Offline
            J Offline
            jlwarlow
            wrote on last edited by
            #61

            Christian Graus wrote:

            An exception can be expensive, it all depends on how often it may occur. But yes, I said from the start that it depends on the complexity of hte method.

            From my COM/DCOM days, you could return a HRESULT, S_OK for found and deleted, S_FALSE for not found assumed deleted, and failed codes for each of the reasons for failure ;)

            Christian Graus wrote:

            Interesting discussion that Chris started.

            Indeed! :-D

            Never argue with an imbecile; they bring you down to their level, and beat you with experience.

            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)

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #62

              Depends on what do you mean by return value. Does it indicate if everything was ok (true) or something went wrong (false)? In that case, return TRUE if item to delete wasn't found. Or, does return value indicate if it deleted item or not? In that case, return FALSE if item to delete wasn't found.


              "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

              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
                Bogdan Iosif
                wrote on last edited by
                #63

                The function should return FALSE. Choosing to return a boolean value from such a function implies the fact that I use exceptions to handle errors. Otherwise, it would be a bad design decision to return a simple boolean value. The function outcome is expected to be this: The value is certainly removed after a call has been made. The return value tells me a nuance of how the operation has been executed - TRUE if the value was actually removed, FALSE if there wasn't anything to remove - and I may or may not use this extra information in my code. However, the return value is not there for error handling. If anything doesn't go according to the expected operating mode - For example the value to be removed has a state that doesn't allow removal - I will throw an exception. If I can't use exceptions then the return value should be an integer with return types documented. For example, I can use 0 for success and anything else for various error codes. This is the model widely used in Win32 APIs. HTH, Bogdan P.S: I'm glad to see this kind of questions are being asked.

                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)

                  W Offline
                  W Offline
                  Woz_a
                  wrote on last edited by
                  #64

                  I wish I had a sense of Humor, I could join in the fun then. But I would tend to agree that Logically:- the function returns a value True or False therefore if there is no data to be deleted, you must return False, the data does not exist therefore cannot be deleted so the operation has not been completed. Clearly you NEED to know the operation has been completed, otherwise you would have written the function with a null return value, therefore if there is no data to delete there is a logical error which needs to be flagged and acted on as appropriate. as mentioned by another responder it may be a case of a missing database or some one not doing thier job properly and in need of being fired. Philosophically:- there is inssufficient information to answer the question, IE does it matter what the return value is? The question can of course be compared with that of "what is the sound of one hand clapping" Clearly there is insufficient information to correctly answer the question, no indication is given as to what the one hand is clapping against and the form (shape) of the hand at impact. for example if the hand was clapping against a smooth concrete surface and the hand was flat the result would be a slapping sound with an instant decay. If the hand was cupped then the sound would be more of a muffled thud. if however the hand was clapping against a large Bell, ( as the bell clapper would do) the resultant sound would be accompanied by a ringing note with a more prolonged decay. bet you are glad you asked now! Woz_a

                  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
                    gchopras
                    wrote on last edited by
                    #65

                    I believe it should return TRUE as item wouldn't exist(whether it existed before or not) after the call.

                    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)

                      M Offline
                      M Offline
                      Marcus J Smith
                      wrote on last edited by
                      #66

                      I would vote the little known third option, TRALSE, since this was neither TRUE nor FALSE and it might be nice to know the true outcome here...:-D


                      CleaKO

                      "I think you'll be okay here, they have a thin candy shell. 'Surprised you didn't know that.'" - Tommy (Tommy Boy)
                      "Fill it up again! Fill it up again! Once it hits your lips, it's so good!" - Frank the Tank (Old School)

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

                        G Offline
                        G Offline
                        Grimolfr
                        wrote on last edited by
                        #67

                        Chris Maunder wrote:

                        That's a bit of a large hammer to weild.

                        Not really. It encourages people to (properly) use the .Contains() method before calling the .Delete() method. Or in the case of file I/O, you would use the File.Exists() static method before firing off a .Delete().


                        Grim

                        (aka Toby)

                        MCDBA, MCSD, MCP+SB

                        Need a Second Life?[^]

                        SELECT * FROM users WHERE clue IS NOT NULL GO

                        (0 row(s) affected)

                        C 1 Reply Last reply
                        0
                        • G Grimolfr

                          Chris Maunder wrote:

                          That's a bit of a large hammer to weild.

                          Not really. It encourages people to (properly) use the .Contains() method before calling the .Delete() method. Or in the case of file I/O, you would use the File.Exists() static method before firing off a .Delete().


                          Grim

                          (aka Toby)

                          MCDBA, MCSD, MCP+SB

                          Need a Second Life?[^]

                          SELECT * FROM users WHERE clue IS NOT NULL GO

                          (0 row(s) affected)

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

                          But what if your collection is actually a database? (I wasn't referring, literally, to a .NET collection)

                          cheers, Chris Maunder

                          CodeProject.com : C++ MVP

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

                          G 1 Reply Last reply
                          0
                          • P PIEBALDconsult

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

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

                            PIEBALDconsult wrote:

                            If it failed it should throw an exception.

                            Agreed 100%. See my reply to Chris' reply to my reply. /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

                              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)

                              J Offline
                              J Offline
                              Jon Raynor
                              wrote on last edited by
                              #70

                              Test for existance first, then attempt delete.

                              P 1 Reply Last reply
                              0
                              • C Christian Graus

                                That's right, the question didn't indicate that the method did anything more complex. And, if it did, it's possible an enum would be required if a return value was needed to indicate what occured.

                                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 )

                                S Offline
                                S Offline
                                shinji1
                                wrote on last edited by
                                #71

                                :-D If I have to choose from them,I would choose FALSE. But I would like to design that function to return how many it would have deleted items. And if exception error would happen,that function would return negative number. Because to have more infomation makes that function more useful. But to make that function more useful makes programer more distressful.

                                shinji1.hp.infoseek.co.jp

                                1 Reply Last reply
                                0
                                • J Jon Raynor

                                  Test for existance first, then attempt delete.

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

                                  That's inefficient, the delete will do the check for existence (a second time).

                                  J 1 Reply Last reply
                                  0
                                  • M Marcus J Smith

                                    I would vote the little known third option, TRALSE, since this was neither TRUE nor FALSE and it might be nice to know the true outcome here...:-D


                                    CleaKO

                                    "I think you'll be okay here, they have a thin candy shell. 'Surprised you didn't know that.'" - Tommy (Tommy Boy)
                                    "Fill it up again! Fill it up again! Once it hits your lips, it's so good!" - Frank the Tank (Old School)

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

                                    At times I use an enum; { Good , Bad , Ugly }

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      But what if your collection is actually a database? (I wasn't referring, literally, to a .NET collection)

                                      cheers, Chris Maunder

                                      CodeProject.com : C++ MVP

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

                                      G Offline
                                      G Offline
                                      Grimolfr
                                      wrote on last edited by
                                      #74

                                      Chris Maunder wrote:

                                      But what if your collection is actually a database?

                                      Follow the already well-established database model and return an integer representing the number of rows affected. Because with a database, it's just as important to know that you deleted 150 rows as it would be to know that you didn't delete any. (More important, IMHO.)


                                      Grim

                                      (aka Toby)

                                      MCDBA, MCSD, MCP+SB

                                      Need a Second Life?[^]

                                      SELECT * FROM users WHERE clue IS NOT NULL GO

                                      (0 row(s) affected)

                                      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)

                                        T Offline
                                        T Offline
                                        Tim Deveaux
                                        wrote on last edited by
                                        #75

                                        I'm thinking enums: eLookHereMateYourAskingMeToDeleteSomethingThatJustDoNotExistYouTwit eYouCallThatAnIndexIveSeenBetterIndicesOnAFisherPriceCalculatorYouTwit eItsLockedAskMeLaterYouTwit eRaviAteItYouTwit eDeleteThisYouTwit eOKYouTwit Pretty much covers the most likely scenarios, and makes the error log a little more interetsing. A more difficult case can occur if the ham sandwich is deleted after Ravi has eaten half of it, especially if garbage collection is running in promiscuous mode. Then you should use a real programming laguage.

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          This is a trick questions. None of the answers are fully acceptable. The question doesn't leave room for a third option of returning a status code which can take at least 3 values. Then, the answer would be to just return a separate, third value if the element to be deleted wasn't found! In my philosophy classes I was thought there are actually three states of truth: true, false, and undecided.;) The reason we're stuck with just true and false in programming seems to have something to do with the 0 and 1 values of independent bits.

                                          1 Offline
                                          1 Offline
                                          123 0
                                          wrote on last edited by
                                          #76

                                          [Message Deleted]

                                          C L 2 Replies 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