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)

    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
              • 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
                peterchen
                wrote on last edited by
                #40

                Hole in the specification. (false, usually)


                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                My first real C# project | Linkify!|FoldWithUs! | sighist

                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
                  Rajesh R Subramanian
                  wrote on last edited by
                  #41

                  Return false, because the function did not do what it was asked to do.

                  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
                    Barbeque Source
                    wrote on last edited by
                    #42

                    throw new KeyNotFoundException();

                    Limit yourself to a maximum of 200 characters.

                    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 )

                      L Offline
                      L Offline
                      Lars Lundstedt
                      wrote on last edited by
                      #43

                      Christian Graus wrote:

                      How could removing an item from a list fail, so that the item is still there ?

                      The item could be locked in some way if your app is multi-threaded, that's one way. There could also be logical reasons for your failure, you could for example have a situation where the item you want to delete has a dependency to other items in other collections (or the same one, for that matter), and the current state would not allow for deletion of your element before the related elements have reached this or that state.

                      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)

                        J Offline
                        J Offline
                        Joan M
                        wrote on last edited by
                        #44

                        return HRESULT? ;P

                        M 1 Reply Last reply
                        0
                        • C Christian Graus

                          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 Offline
                          M Offline
                          Maarten Verdouw
                          wrote on last edited by
                          #45

                          Uhm, you could return false if something went wrong (No DB connection, index out of range e.g.). In that case a returncode or Exception would be more convenient. But that wasn't the question.

                          --------------- don't P A N I C

                          C N 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)

                            M Offline
                            M Offline
                            Muammar
                            wrote on last edited by
                            #46

                            Interesting question but I would always test the overall objective "that is the existence of the item in this one".


                            Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                            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
                              Lost User
                              wrote on last edited by
                              #47

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

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

                                Christian Graus wrote:

                                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 ?

                                I found it but couldn't delete it. :confused:

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

                                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
                                  Pete OHanlon
                                  wrote on last edited by
                                  #49
                                  public enum CollectionItemOperation
                                  {
                                    ItemDeleted,
                                    ItemNotDeleted,
                                    ItemLocked,
                                    ItemNotFound
                                  }
                                  ...
                                  public CollectionItemOperation DeleteItemFromCollection(Hashtable ht, object item)
                                  {
                                    CollectionItemOperation operationResult = CollectionItemOperation.ItemNotFound;
                                    if (ht.Contains(item))
                                    {
                                      ...
                                    }
                                    return operationResult;
                                  }
                                  

                                  There you go - no ambiguity.

                                  Deja View - the feeling that you've seen this post before.

                                  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
                                    Andrew Leeder
                                    wrote on last edited by
                                    #50

                                    In true K&R 'C' style you would return the number of items deleted, which would of course be zero. So in .NET terms that would be FALSE. ~A

                                    1 Reply Last reply
                                    0
                                    • P Patrick Etc

                                      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 Offline
                                      G Offline
                                      Guy Harwood
                                      wrote on last edited by
                                      #51

                                      I would return true. The outcome you were expecting is 'true', so return it! Does that make me a bad programmer? :-D

                                      ---Guy H (;-)---

                                      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
                                        Mark II
                                        wrote on last edited by
                                        #52

                                        I know! I know! The *right* answer is... True! No, no, I mean false! Er... no. It depends. Um. Maybe neither? Or both? Or something. Yes, yes. Something. Or null? Right. Let's start with a bit of analysis. Requirements The question explicitly states that, the "function that is *meant to* delete an item". It does *not* say "is meant to delete an item *if* it exists.". The question does *not* say what the purpose of the deletion is. This could be: 1. To ensure that the item is not in the list. 2. To release the memory / disk space that holds the item. So, the non-deletion of an item must be assumed to be a failure of the function. Parameters Persumably there are the following cases: 1. The item exists and is deleted. 2. The item exists and cannot deleted. 3. The item does not exist. 4. The user has passed invalid parameters into the function. Case (1) is a success. Case (2) and (3) are a failure of the function. Case (4) is an error. This leaves 2 reasonable behaviours: A. Return nothing for (1) or throw an exception for (2),(3) and (4). B. Return TRUE for (1), FALSE for (2) and (3), and an exception for (4). Fowever, the question asks if we should throw TRUE or FALSE for (3), so (A) is not valid. Conclusion Answer: Return FALSE. And *DOCUMENT* the meaning of this result, ideally in XML coments so that they show up in intellisense. So, what do I win? My Blog: http://allwrong.wordpress.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)

                                          T Offline
                                          T Offline
                                          The Nightcoder
                                          wrote on last edited by
                                          #53

                                          Three methods: - DeleteItem: return void, throw one exception if item doesn't exist and another if the delete failed for some other reason. - ItemExists: return true/false. - TryDeleteItem: return true if the item exists and was deleted, false if it didn't. Throw an exception if the delete failed. This is consistent with framework behaviour, and gives the class consumer the option of picking whatever best suites the need. Note that although TryDeleteItem seems like a shorthand for an if, it might potentially be a performance optimization (if the collection is a database table, for instance). Then, DeleteItem would probably be a two-line wrapper for TryDeleteItem. Later, Peter

                                          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