- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
"Let me tell you a story of a man named Jed. Poor mountaineer, but he kept his family fed..."
Admin
Admin
My reverse polish notation calculator has a customary complaint about vbnet being trwtf...
And go with the code that works better.
Admin
Admin
This really p!sses me off:
If matches.Count > 0 Then Return True Else Return False End If
Its so pointless. Why do sheeple do it? Can they not see the total redundancy of 5 lines of code? Do they really think its "more readable"?
Return matches.Count > 0
Admin
Wait, is that regex solution supposed to elegant?
Admin
... except Jed's solution doesn't work. Suppose I want to search for "$10". Not to mention all the string manipulations, use a StringBuilder.
Admin
If you do didn't immediately think that this should be done with a hash, a sort, and a binary search, kill yourself now and save us all future aggravation.
Admin
I believe this is one of those cases where, once you've made a certain mental step, you simply can't go back to thinking the way you did before.
In this case, the mental step is that 'a < b' is really something that results in a boolean value, and is not 'some special syntax that can be used in if and while constructions'.
Admin
Management can always 1up you.
Admin
Admin
There - fixed that for you...
Public Function DocContainsAtLeastOneTerm( _ ByVal searchTerms() As String, _ ByVal searchText As String) As Boolean
Admin
That looked a lot more elegant in SharpDevelop lol...
Admin
Can anyone say N*M complexity?
Admin
minuses for Jed:
In the end Jed should just have shut up!
Admin
The second solution doesn't look much better than the first one to me.
Creating a regular expression by concatenating strings without escaping them is dangerous. When the strings contain any characters which have a special meaning in regular expressions, they will break everything.
Also, I don't know much about VB, but why are they both passing the arguments by value ("ByVal")? They seem to be pretty large (an array of strings and a very long string) and aren't changed in the code. Wouldn't it be better to pass them by reference?
Admin
Admin
Anyone who responds like that needs to be fired swiftly and brutally. This industry has no place for people who think their code is infallible and refuse to take critiques.
Admin
Admin
need to push this update
If searchTerms.Length >= 1 Then For i As Integer = 1 To searchTerms.Length - 1 Step 1 regexString += "|" + searchTerms(i) Next End If
or
If searchTerms.Length > 0 Then For i As Integer = 1 To searchTerms.Length - 1 Step 1 regexString += "|" + searchTerms(i) Next End If
Admin
Admin
A regex wtf, and nobody mentioned the perl oneliner yet? :)
return grep { $searchText =~ $_ } @searchTerms;
It continues to amaze me how verbose other languages are for the most basic operations...
Admin
PiisAWheeL - read it again... I think you'll find I actually do return a boolean, and not matches.
Martin.
Admin
Why would need a sort and binary search? Just put all the words in the text into a hash set and do a contains (or whatever the VB equivalent is) for each word.
Admin
The documented requirements:
Write a function that returns true if at least one string in the /search terms/ array is found in /bigstring/, otherwise returns false.
So, my version, written in pseudocode because I'm lazy:
No regex, no stacks, no hash tables, nothing. Just enumerate the search terms and return true as soon as you find one that's in the big string. If you don't find one, return false.
Jed is an idiot, as is Bob. So are most of you.
Admin
TRWTF is that Jed thought that his solution was right.
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.
Admin
Is this a WTFer competition? Both solutions are awful
Admin
Admin
And don't forget the Linq query
Admin
Ah, that's true. It's not clear how you can divide the text into discrete elements to be hashed, since the terms you're looking for can be pretty much anything.
Admin
This illustrates the point as to why using built-in library functions is considered harmful. It short-circuits critical thought and analysis.
Admin
Please read up on what a hash set is.
Admin
Wait a minute, I'm being trolled, right?
Admin
TRWTF is that Bob thinks splitting the big string m*n times (where m is the number of search terms and n is the number of words in the big string) isn't insanely stupid. If he pulled that out of the loop then his code wouldn't be great, but it would probably be perfectly adequate for expected usage.
Admin
Oh, but he isn't splitting it nm times, he's splitting it nn times. Not that it makes that much of a difference if you have at least as many search terms as words in your document, and regardless of the number of search terms it is still insanely stupid.
Admin
+1
Admin
For me, TRWTF in the second solution was the building of the regex.
Dim regexString As String = "(" + String.Join("|", searchTerms) + ")"
Look ma, no hands!
Admin
Hear hear! I about choked trying to figure out what that verbose code was trying to do, but this one line makes sense!
Admin
better:
Return (New Regex(regexString)).Matches(searchText) > 0
Admin
Nope - but I can say "quick, simple, and exits on first match"...
Admin
Sorry, but I got stuck on the fact that the other developers inboxes were on the floor. Did I miss a wild party?
Admin
Admin
Admin
DocContainsAtLeastOneTerm(("Wal*Mart", "$49.95"), searchText)
Jed's "solution" makes Little Bobby Tables cry.
Admin
Admin
And why didn't Jed use a StringBuilder?
Admin
Whoa...
For i As Integer = 0 To searchText.Split(" ").Length - 1 Step 1 searchTable.Push(searchText.Split(" ")
...
For i As Integer = 1 To paramTable.stackHeight() - 1 Step 1 If searchTable.Contains(paramTable.Pop()) Then
Way to go! NN complexity just for the sake of it, followed by MN to top it off.
No wonder they had a performance bottleneck, if everything else was written like that.
Admin
Too bad Jed didn't remember to sanitize the search terms... Try and search for 1.56 and you shall fail when matching with 1056. That should be nice if we are speaking of figures.
Admin
Admin
RegEx initial parsing is somewhat expensive. RegEx execution on the other hand, is log(N), blazing fast for large amounts of search terms.