Comment On Unique Ways to Play Catch

Normally, we don't write up submissions that come from code websites or open source projects. This policy is partially because F/OSS projects are supposed to already have many eyes and partially because I don't want to get beat up. When the submitter is afraid of getting fired, anonymity kicks in. As for code websites that take themselves seriously, I just feel sorry for the people that write those. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Unique Ways to Play Catch

2007-03-16 09:04 • by akatherder
So now it's bad form to rely on an exception in your standard code path? Picky picky.

Re: Unique Ways to Play Catch

2007-03-16 09:05 • by Chnoodzen (unregistered)
How about just letting the values overwrite each other

foreach(string s in strObj){
UniqueHT[s] = "My CAPTCHA: craaazy";
}

Re: Unique Ways to Play Catch

2007-03-16 09:06 • by Lastchance
Ah, the old "use exception handling for non-exceptional logic" trick. That's the third time this week!

Yes, I just got my "Get Smart" DVD set.

Re: Unique Ways to Play Catch

2007-03-16 09:08 • by KattMan
This is so enterprisey I might spring this on my co-workers.
I mean it has arbitrarily adding values to an array, Exceptions used for standard processing, and recursion.

Who wouldn't like this solution?

Re: Unique Ways to Play Catch

2007-03-16 09:13 • by DOA (unregistered)
To get a bit biblical... He who has never used exceptions in this manner, cast the first stone

captcha: Noone gives a rat's ass

Re: Unique Ways to Play Catch

2007-03-16 09:19 • by guy (unregistered)
This person was probably translating VB6 code. I believe in VB6 collections did not have .contains, and using On Error Resume Next and checking the error number was the only way to check if the collection had a given key.

.Net has done a lot to prevent using exceptions for flow control. For example they add tryparse for parsing strings into various datatypes in 2.0.

Re: Unique Ways to Play Catch

2007-03-16 09:19 • by Misha (unregistered)
For those too lazy to google:
http://www.codeproject.com/useritems/tips.asp#tip1.12

I like how the snippet has "OOPS" in big letters right underneath.

Funnily enough, found it in standard google search but not code search.

Re: Unique Ways to Play Catch

2007-03-16 09:21 • by FDF (unregistered)
127068 in reply to 127065
DOA:
To get a bit biblical... He who has never used exceptions in this manner, cast the first stone

Here, catch my stone.

Re: Unique Ways to Play Catch

2007-03-16 09:27 • by Mcoder
127069 in reply to 127065
DOA:
To get a bit biblical... He who has never used exceptions in this manner, cast the first stone

captcha: Noone gives a rat's ass

I would be first, but it seems people have aready got there. The real WTF it that his hash maps can have duplicated keys. The fact that it trows an exceptions is a plus. And, no, I don't know .net enough to know if it is true, or if the program is just wrong.

The 'contains(value)' solution isn't good either, now your code uses O(n^2) operations, when the original uses just O(n).

Re: Unique Ways to Play Catch

2007-03-16 09:32 • by Jim (unregistered)
There's no Set object in .Net? In Java we just do:
Set unique = new HashSet();

unique.addAll(Arrays.asList(strObj));

Done.

Re: Unique Ways to Play Catch

2007-03-16 09:38 • by snoofle (unregistered)
127071 in reply to 127070
Jim:
There's no Set object in .Net? In Java we just do:
Set unique = new HashSet();

unique.addAll(Arrays.asList(strObj));

Done.

Hey, your LOC count is waaaay too low for this to be solid code - beef it up a little!

Re: Unique Ways to Play Catch

2007-03-16 09:38 • by zip
127072 in reply to 127069
Mcoder:
The real WTF it that his hash maps can have duplicated keys.
The 'contains(value)' solution isn't good either, now your code uses O(n^2) operations, when the original uses just O(n).


The real WTF is that you have no idea what you're talking about.

The exception he's using is caused by duplicate keys.

Hashtables have O(1) lookup.

Re: Unique Ways to Play Catch

2007-03-16 09:43 • by Twon (unregistered)
127073 in reply to 127069
I believe .Net Hashtables store keys in some sensible (read: tree) data structure rather than just a list; I don't think .ContainsKey does an O(n) pass through the whole list of keys any time. Assuming some kind of balanced binary tree, you're looking at O(n lg n) instead, which is not so bad.

Also, check your math on the original solution -- the recursion adds a heaping helping of extra, wasted computation if there are any duplicated values.

Re: Unique Ways to Play Catch

2007-03-16 09:45 • by NateP (unregistered)
127074 in reply to 127061
akatherder:
So now it's bad form to rely on an exception in your standard code path? Picky picky.


Exception handling is expensive.. unwinding the stack and all..

Re: Unique Ways to Play Catch

2007-03-16 09:46 • by Jones McFaggot (unregistered)
127075 in reply to 127073
FRIST POST!

THE REAL WTF IS

CAPTCHA: dubya - Mandate of Heaven

Re: Unique Ways to Play Catch

2007-03-16 09:46 • by guy (unregistered)
127076 in reply to 127073
Umm... no.

Re: Unique Ways to Play Catch

2007-03-16 09:49 • by Symbiatch (unregistered)
Umm, I don't know if it's some kind of a requirement in this case to use hashtables (probably not since it's an example), but I wouldn't if I just needed to find unique values. I'd just use:

List<string> Unique;
foreach(string s in strObj)
if(! Unique.ContainsKey(s))
Unique.Add(s);

But maybe I'm just stupid. (And in .NET 1.1 I'd just use ArrayList)

Re: Unique Ways to Play Catch

2007-03-16 09:49 • by guy (unregistered)
127078 in reply to 127073
Twon:
I believe .Net Hashtables store keys in some sensible (read: tree) data structure rather than just a list; I don't think .ContainsKey does an O(n) pass through the whole list of keys any time. Assuming some kind of balanced binary tree, you're looking at O(n lg n) instead, which is not so bad.

Also, check your math on the original solution -- the recursion adds a heaping helping of extra, wasted computation if there are any duplicated values.


What part of the name "Hashtable" suggests that there is a tree involved?

Re: Unique Ways to Play Catch

2007-03-16 09:49 • by Nomen Nescio (unregistered)
even taking the exception-in-ordinary-code-path thing as given, isn't there a bug in the way the exception is handled? if the last item in the array is a dupe, won't the exception "handler" risk advancing the loop counter too far?

or i could be worng, seeing as i *still* haven't managed my morning cup of coffee yet. gotta get on that problem now.

Re: Unique Ways to Play Catch

2007-03-16 09:50 • by Yartz (unregistered)
127080 in reply to 127063
Lastchance:
Ah, the old "use exception handling for non-exceptional logic" trick. That's the third time this week!

Yes, I just got my "Get Smart" DVD set.


Well, it's not such a big deal in many languages. Just because .Net exceptions are SO EXPENSIVE, does not mean catching exceptions in mainstream logic is so horrid.

In Python, for instance, the try to insert then catch if it's there is a somewhat agreed on pattern (with the motto "it's easier to be forgiven than to be granted").

Anyway, the code snippet was a little braindead anyway. And in .Net you should not merrily throw exceptions like this. No huge WTF, just poor code.

Re: Unique Ways to Play Catch

2007-03-16 09:52 • by Justin (unregistered)
I think I'll take the original programmer over some of these commenters.

Re: Unique Ways to Play Catch

2007-03-16 09:58 • by Hellfire (unregistered)
127082 in reply to 127067
The real WTF is that the Article got a rating of 4/5...

CAPTCHA: pling-pling - spaboing - pling

Re: Unique Ways to Play Catch

2007-03-16 09:59 • by Will Perdikakis (unregistered)
127084 in reply to 127072
zip:
Mcoder:
The real WTF it that his hash maps can have duplicated keys.
The 'contains(value)' solution isn't good either, now your code uses O(n^2) operations, when the original uses just O(n).

Hashtables have O(1) lookup.
Beat me to it!

Mcoder,

The point of a hashtable is to store data for quick finds later. Based on the hash value, you can jump directly to the "bucket" where the value is stored, you do NOT iterate through. However, you then have to iterate that bucket (Unless it is doubly hashed) until you find the value. This is usually not a problem since that bucket is usually significantly smaller than the total number of elements.

Re: Unique Ways to Play Catch

2007-03-16 10:00 • by dave (unregistered)
No. no no no. exceptions do not belong as part of the main flow of a program. just dont do it kids.

Re: Unique Ways to Play Catch

2007-03-16 10:02 • by Marko (unregistered)
127086 in reply to 127073
Twon:
I believe .Net Hashtables store keys in some sensible (read: tree) data structure rather than just a list; I don't think .ContainsKey does an O(n) pass through the whole list of keys any time. Assuming some kind of balanced binary tree, you're looking at O(n lg n) instead, which is not so bad.

Also, check your math on the original solution -- the recursion adds a heaping helping of extra, wasted computation if there are any duplicated values.


I believe .NET hashtable stores keys in a hashtable. I suggest you do a little research on a hash tables subject.

Re: Unique Ways to Play Catch

2007-03-16 10:05 • by newt0311 (unregistered)
127087 in reply to 127080
Yartz:
Lastchance:
Ah, the old "use exception handling for non-exceptional logic" trick. That's the third time this week!

Yes, I just got my "Get Smart" DVD set.


Well, it's not such a big deal in many languages. Just because .Net exceptions are SO EXPENSIVE, does not mean catching exceptions in mainstream logic is so horrid.

In Python, for instance, the try to insert then catch if it's there is a somewhat agreed on pattern (with the motto "it's easier to be forgiven than to be granted").

Anyway, the code snippet was a little braindead anyway. And in .Net you should not merrily throw exceptions like this. No huge WTF, just poor code.


It should also be noted in python that the get method is significantly faster especially if something does not exist because, exception handling is expensive.

Re: Unique Ways to Play Catch

2007-03-16 10:07 • by Twon
127088 in reply to 127086
Completely right. I need more coffee this morning...

Re: Unique Ways to Play Catch

2007-03-16 10:11 • by Faxmachinen (unregistered)
127089 in reply to 127079
Nomen Nescio:
even taking the exception-in-ordinary-code-path thing as given, isn't there a bug in the way the exception is handled? if the last item in the array is a dupe, won't the exception "handler" risk advancing the loop counter too far?

Would be amusing if that caused a segfault exception.
It's not a problem though, the while loop is just skipped when index >= strObj.Length. But it does give you one Console.ReadLine() too many.

Re: Unique Ways to Play Catch

2007-03-16 10:16 • by RON (unregistered)
The REAL WTF is these comments.


Seriously WTF.

Re: Unique Ways to Play Catch

2007-03-16 10:17 • by Xr (unregistered)
127092 in reply to 127078
guy:
Twon:
I believe .Net Hashtables store keys in some sensible (read: tree) data structure rather than just a list; I don't think .ContainsKey does an O(n) pass through the whole list of keys any time. Assuming some kind of balanced binary tree, you're looking at O(n lg n) instead, which is not so bad.

Also, check your math on the original solution -- the recursion adds a heaping helping of extra, wasted computation if there are any duplicated values.


What part of the name "Hashtable" suggests that there is a tree involved?

And what would be the point in a hash table without O(1) operations ?
BTW, balanced trees are O(log n), not O(n log n)

Re: Unique Ways to Play Catch

2007-03-16 10:17 • by Cloaked User
127093 in reply to 127080
Yartz:
Well, it's not such a big deal in many languages. Just because .Net exceptions are SO EXPENSIVE, does not mean catching exceptions in mainstream logic is so horrid.


Yes, that's why they're called exceptions - because they're normal occurrences, and not exceptional at all...

Anyone using exception handling as part of the normal control flow of their code on one of my projects is going to have to have an exceptionally good reason, or they're going to be rewriting it.

Re: Unique Ways to Play Catch

2007-03-16 10:19 • by RobertB
127094 in reply to 127066
guy:
This person was probably translating VB6 code. I believe in VB6 collections did not have .contains, and using On Error Resume Next and checking the error number was the only way to check if the collection had a given key.

.Net has done a lot to prevent using exceptions for flow control. For example they add tryparse for parsing strings into various datatypes in 2.0.

It also looks like this code from the VB6 project I work with.

Public SomeFunction () As String
On Error GoTo EmptyArray
j = UBound(iSomething)
On Error GoTo 0
If j >= 0 Then
...
End If
Exit Function

EmptyArray:
'An array has not been initialized.
If Err = 9 Then
j = -1
Resume Next
Else
SomeFunction = ""
Exit Function
End If

End Function


Since this is apparently the only* way to identify an empty array, I can't run the application with "break on all errors" set, at least not until I get into my own code.

* In my code, I set a second variable to indicate whether the array has been initialized. I still wonder, though, if I'm missing something more elegant. And don't bother with "switch to .NET" or "use a real language". That horse is dead, stop beating it.

Re: Unique Ways to Play Catch

2007-03-16 10:19 • by SuperousOxide
There's no Set in .Net? And adding to a HashTable with a previously used key throws an exception?

Anyway, the part I like is calling the function again every time you throw the exception, rather than just skipping that item and continuing the loop.

Re: Unique Ways to Play Catch

2007-03-16 10:34 • by igitur (unregistered)
127097 in reply to 127066
I agree. Definitely translated VB6 code.

I hate that thing. I'm having to work on a project that was "upgraded" from VB6 to VB.NET, then thrown into a VB.NET -> C# converter. Furthermore, the "developer" decided that having different Subs / Functions wasn't necessary because they were called all after each other in a predetermined fixed order.

The result... one looooong function of about 1600+ lines of C# code, vaguely resembling VB6 flow.

Re: Unique Ways to Play Catch

2007-03-16 10:37 • by slinkp (unregistered)
127098 in reply to 127095
SuperousOxide:
There's no Set in .Net?


I was wondering about that... the way to do it in python >= 2.4 is in one line:

unique = set(astring)

And adding to a HashTable with a previously used key throws an exception?


I don't know squat about .NET, but a quick look at msdn suggests that is in fact the case:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsHashtableClassAddTopic.asp

even so, surely calling .Contains() or .ContainsKey() would be more idiomatic than checking for the exception.

Re: Unique Ways to Play Catch

2007-03-16 10:41 • by Josh (unregistered)
127100 in reply to 127073
Twon:
I believe .Net Hashtables store keys in some sensible (read: tree) data structure rather than just a list; I don't think .ContainsKey does an O(n) pass through the whole list of keys any time. Assuming some kind of balanced binary tree, you're looking at O(n lg n) instead, which is not so bad.

Also, check your math on the original solution -- the recursion adds a heaping helping of extra, wasted computation if there are any duplicated values.


I love how people conjecture about what they don't know just to make a point. That should be the real WTF. Just bullshiting here but now let me have a point.

Re: Unique Ways to Play Catch

2007-03-16 10:43 • by Pesho (unregistered)
My gut reaction was pretty much the one proposed by the second smartass, but what's more important, the REAL WTF is, of course C#. Why? Because you can't call a xxx on Dictionary to do the job, like, say d.xxx( key, ... ). No, you have to do d[key] = ..., and it just plain sucks bigtime that exported functionality REQUIRES special syntax (i.e. you can't do it with a plain old function call.)

(Of course this does not wholly excuse the poor sod :) He could have discarded the exception within the iteration, as the next smartass suggested.)

Re: Unique Ways to Play Catch

2007-03-16 10:44 • by ST ST (unregistered)
uhm...

^aCollection asSet.

Smalltalk wins

he who doesn't nkow should speak

2007-03-16 10:51 • by Brian (unregistered)
127103 in reply to 127069
1. hasmaps don't allow duplicate keys for classes that have properly implemented HashCode() (in java and .Net).

2. the cleaner option is NOT O(n^2) - the very point of a cleanly implemented hash based collection strategy is to speed lookups. If you've not implemented a hashtable/map you won't know this bit of practical information.

Conclusion - your response is as WTF as the original code....

Re: Unique Ways to Play Catch

2007-03-16 10:52 • by RON (unregistered)
Here is the best way to do it in .NET 2.0

No need to mess around with the inefficient hashtable class.


static class Unique
{
static IEnumerable<T> FindDuplicates<T>( IEnumerable<T> collection )
{
Dictionary<T, object> unique = new Dictionary<T, object>();

foreach( T t in collection )
unique[t] = null;

return unique.Keys;
}
}

Re: Unique Ways to Play Catch

2007-03-16 10:52 • by -j (unregistered)
F/OSS using the patent minefield that is .NET?
Seems unduly rash!

Re: Unique Ways to Play Catch

2007-03-16 11:00 • by joe.edwards
127106 in reply to 127065
DOA:
To get a bit biblical... He who has never used exceptions in this manner, cast the first stone

captcha: Noone gives a rat's ass

/me casts the first stone.

Re: Unique Ways to Play Catch

2007-03-16 11:06 • by Eduardo Habkost (unregistered)
127109 in reply to 127065
DOA:
To get a bit biblical... He who has never used exceptions in this manner, cast the first stone


first_stone = (Stone)stones[0];

Re: Unique Ways to Play Catch

2007-03-16 11:08 • by Leadhyena (unregistered)
This is nothing. Back in the dot-com days I worked for a stock trading firm that was writing their own trading system in Java 1.2. If only I remembered any of the code nowadays, I'd send it in, because it totally was WTF quality. One of their 'hallmarks' was a signaling system that was based entirely on exception handling. They would throw exceptions as signals that would hit the parent caller that would then act as an operator to another child process. The best part of the system was that none of it had comments. Our CTO at the time would brag about the sheer complexity of the system, and how something that was so complicated couldn't possibly be so wrong.

throw new CAPTCHAException("Smile! Your code is crap.");

Re: Unique Ways to Play Catch

2007-03-16 11:17 • by J (unregistered)
127115 in reply to 127101
Pesho:
the REAL WTF is, of course C#. Why? Because you can't call a xxx on Dictionary to do the job, like, say d.xxx( key, ... ). No, you have to do d[key] = ..., and it just plain sucks bigtime that exported functionality REQUIRES special syntax (i.e. you can't do it with a plain old function call.)



Oh, what a tragedy!! Poor C# coders are forced to use the same syntax that is used in every other collection in the language on their hash table. Typing a square bracket puts a person through so much suffering. All other aspects of a language which commits such a pernicious evil should be ignored, as they'll surely be overshadowed by a syntax variation.

Re: Unique Ways to Play Catch

2007-03-16 11:31 • by Akerbos (unregistered)
127123 in reply to 127078
guy:
What part of the name "Hashtable" suggests that there is a tree involved?

What part of the name "Vector" suggests that there is an array involved?

All of you that just use library funcions, be remembered that a library function also has to work for its effects.
Personally, I would only call a function/solution "bad" if it ends in undefined behaviour or it is significantly slower than another one. Optimization while compiling is your friend.
In dubio pro reo.

Anyway, funny idea of the original writer. Wonder if he feels dirty today.

Re: Unique Ways to Play Catch

2007-03-16 11:33 • by Aaron (unregistered)
127127 in reply to 127101
Pesho:
My gut reaction was pretty much the one proposed by the second smartass, but what's more important, the REAL WTF is, of course C#. Why? Because you can't call a xxx on Dictionary to do the job, like, say d.xxx( key, ... ). No, you have to do d[key] = ..., and it just plain sucks bigtime that exported functionality REQUIRES special syntax (i.e. you can't do it with a plain old function call.)

Hashtable.Add
Hashtable.ContainsKey
Hashtable.ContainsValue
Hashtable.Remove
Hashtable.get_Item
Hashtable.set_Item

The last two are hidden by C# but that's what you'll see in the CLI. Properties and default accessors are syntactic sugar that make our lives as developers easier, not harder. Not everybody loves verbosity for the sake of verbosity. Regardless, it's still fully compatible with languages that don't support that uh, "special syntax".

Mind you, only .NET 1.x programmers still use the Hashtable. The .NET 2.0 framework introduced Dictionary<TKey, TValue> which also has the TryGetValue method, a very convenient one that I use extensively.

Really, people need to read more than 3 lines from the first Google search result before they open their mouths.

For any .NET programmers who are really anxious to get sets, or bags, or non-unique dictionaries, etc., just get the free PowerCollections library. Or wait for 3.0 which will have most of these built in for LINQ (and you can run SQL-like queries on them).

Re: Unique Ways to Play Catch

2007-03-16 11:39 • by Aaron (unregistered)
127131 in reply to 127123
Akerbos:
guy:
What part of the name "Hashtable" suggests that there is a tree involved?

What part of the name "Vector" suggests that there is an array involved?

Aside from the two words being almost synonymous in the English language, the very first sentence in the class documentation is, "The Vector class implements a growable array of objects." Or, in J#, "Represents a dynamic array of elements."

Hashtable does not mention anything about a tree, nor should it, since it's implemented using a hash table.

Re: Unique Ways to Play Catch

2007-03-16 11:39 • by Artemus Harper (unregistered)
127132 in reply to 127115
If we assume we were using just the letters A-Z, then a simple solution that "inlines" the hashset would be:

bool[26] values = new bool[26]; //all values are false.

foreach(string s in array)
{
int index = s[0]-'A';
if(values[index])
{
values[index] = true;
Console.WritLine(s);
}
}

For anything else, just remember that java and c# every object has the hashcode/Hashcode method. But since hashcodes can collide, we need buckets. If any string value was allowed, then yes, the worst time for this is:O(n^2)

Re: Unique Ways to Play Catch

2007-03-16 11:41 • by Michael (unregistered)
127134 in reply to 127078
guy:
What part of the name "Hashtable" suggests that there is a tree involved?

Um, the "Hashtable" part? Seriously, how do you think a hashtable is implemented? Here's a hint: it's not a list.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment