Straw Poll: Return True or False?
-
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
I would return true. The outcome you were expecting is 'true', so return it! Does that make me a bad programmer? :-D
---Guy H (;-)---
-
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)
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[^]
-
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)
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
-
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.
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 )
-
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
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 )
-
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.
OK, so it's possible to think of a situation that's way more complex than the original question, and in this instance, it would make more sense for the return to be an enum, to return found it, didn't find it, or various possible other error conditions.
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 )
-
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)
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 -
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 )
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.
-
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.
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 )
-
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)
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"
-
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 )
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.
-
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)
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
-
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)
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.
-
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)
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
-
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)
-
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)
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) -
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)
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
SELECT * FROM users WHERE clue IS NOT NULL GO
(0 row(s) affected)
-
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
SELECT * FROM users WHERE clue IS NOT NULL GO
(0 row(s) affected)
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)
-
If it failed it should throw an exception. Maybe you want a
TryDelete
? -
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)
Test for existance first, then attempt delete.