Straw Poll: Return True or False?
-
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
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
-
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)
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:
-
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)
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
-
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)
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
-
Chris Maunder wrote:
meant to delete an item
The function failed. It must therefore return false. I have spoken. /ravi
If it failed it should throw an exception. Maybe you want a
TryDelete
? -
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
Hear hear
-
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 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
-
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)
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/publitorOur kids books :The Snot Goblin, and Book 2 - the Snotgoblin and Fluff
-
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)
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 -
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)
Return false, because the function did not do what it was asked to do.
-
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)
throw new KeyNotFoundException();
Limit yourself to a maximum of 200 characters.
-
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 )
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.
-
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)
-
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 )
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
-
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)
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.
-
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 )
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.
-
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)
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.
-
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)
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
-
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 (;-)---