- 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
Sorry, I have a hard time believing even a "highly-paid consultant" would write something this stupid.
Admin
You obviously don't understand the merits of 'case based' programming.
morons
Admin
It looks more stupid than it really is. request.querystring("category") is userinput, so must be checked at all times. A mere 'selectthese = request.querystring("category")' won't do.
--
cat = cint(request.querystring("category"))
if cat >= 1 and cat <= 9 then
selectthese = cat
else
selectthese = 1
end if
--
would be better.
Argh, selectthese is assigned a string, thus the above is not equivalent :(
Admin
Anonymous's code also doesn't work when "category" isn't an integer. Hmmm, perhaps that "highly paid" consultant wasn't so stupid after all!
Admin
Brushing up on VB here...
selectthese = "1"
Tmp = Request.QueryString("category")
If IsNumeric(Tmp) Then
Tmp = cInt(Tmp)
If Tmp > 0 And Tmp < 10 Then selectthese = cStr(Tmp)
End If
Admin
<FONT size=2>Nope:</FONT>
<FONT size=2>What if request.querystring["category"] is not numeric?</FONT>
Nope:
<FONT size=2>What if cint overflows?</FONT>
Actually, since vb casing doesn't short circuit, the "wtf" code isn't the worst per se. At least it won't break like y'all's solutions.
How 'bout this:
const ZeroCode = 48
const NineCode = 57
dim Category, CategoryCode, SelectThese
Category = Request.QueryString("category")
SelectThese = "1"
if len(Category) = 1 then
CategoryCode = asc(Category)
if CategoryCode >= ZeroCode and CategoryCode <= NineCode then
SelectThese = Category
end if
end if
Admin
IsNumeric() won't work, because this is just after single digits, not decimals, exponents, signs, etc. This should be quite sufficient:
Admin
Disregarding attempts to optimise the logic of the code, I think highly paid consultants should be aware of how the Select Case statements work.
Select Case Request.QueryString("category")
Case "1", "2", "3", "4", "5", "6", "7", "8", "9"
SelectThese = Request.QueryString("category")
Case Else
SelectThese = "1"
End Select
Happy New Year [<:o)]
Admin
My god this is so fucking lame. LAME!
Admin
<FONT style="BACKGROUND-COLOR: #efefef">Everyone is missing the point here!</FONT>
Admin
He was probably paid by the number of LOCs her created [:)]
Admin
Amazing that people are actually paid for writing silly code like this.
Btw... Doesn't the case statement have an Else branch in case the value matches none of the values? He should at least lose his job for not using that!
Then again, some poor guy now has to maintain it so he has already been taken off the job already...
I wonder if there's a relationship between the costs of the consultants and the quality of their work? Do better-paid consultants do a worse job?
Admin
I like the variable name:
"SelectThese"
[:)]
Admin
believe it!
I worked at a company once that made multi million dollar simulators for the military. When looking through the code, I saw switch statements just like that, and many other things that just made me want to scream! All done by highly paid apes.....err..I mean consultants.
Stephan
Admin
Why would some write code in a language that lets a variable just be any damn thing it wants to be? WTF!
Admin
I've written a lot of Smalltalk code, and... well, suffice it to say that dynamic typing is not a WTF in and of itself.
Admin
I have to use languages like that on a daily basis. It isn't really that bad.
PHP, for example, is another language that does not enforce variable type. While this can lead to bugs, these issues are more due to the programmer's ability (or lack thereof). If you really need a compiler that will catch such mistakes, you probably shouldn't be writing any code!
I can't think of any use for storing values of different types in the same variable, and I believe it's done to save overhead, especially in interpreted languages such as PHP.
Not to say that it isn't handy (to have a type-safe language), but I disagree that it's a WTF in and of itself.
-blue
Admin
Now THAT is funny - without even being obscure!! [:|]
Hard to believe somebody would actually even contemplate typing all of that for oh sooooo little output!
Admin
I don't beleive it, nobody could conciously do something like that.
I'm beginning to think some of these WTF's are just made up.
Admin
I'm telling you, believe it. I personally have seen stuff like this in production code with my own eyes!
Stephan
Admin
<FONT color=#ff00ff size=2>
<FONT color=#000000>Boy for all the no people that are hacking on over paid over priced consultants, not one person even thought of using exception handling, to solve a most simple task. WTF!</FONT>
<FONT color=#000000>Some of your solutions do not even work for all test cases, at least the sap who wrote the original code does. WTF!</FONT>
string</FONT><FONT color=#ffffff size=2> <FONT color=#000000>selectThese;</FONT>
</FONT><FONT color=#000000 size=2><FONT color=#ee82ee>t</FONT><FONT color=#ee82ee>ry</FONT></FONT><FONT size=2><FONT color=#ee82ee> </FONT>
<FONT color=#000000>{</FONT>
</FONT><FONT color=#000000><FONT size=2>int</FONT><FONT size=2> </FONT><FONT size=2>value</FONT></FONT><FONT color=#ffffff size=2> <FONT color=#000000>=</FONT> <FONT color=#000000>Convert.ToInt32(Request.QueryString</FONT><FONT color=#000000>[</FONT></FONT><FONT color=#00ffff size=2><FONT color=#808080>"</FONT><FONT color=#808080>category"</FONT></FONT><FONT color=#ffffff size=2><FONT color=#000000>])</FONT>;
</FONT><FONT color=#ff00ff size=2>if</FONT><FONT color=#ffffff size=2> <FONT color=#000000>(</FONT></FONT><FONT color=#ff00ff size=2>value</FONT><FONT color=#ffffff size=2> <FONT color=#000000>>=</FONT> </FONT><FONT color=#ff0000 size=2>1</FONT><FONT color=#ffffff size=2> <FONT color=#000000>&&</FONT> </FONT><FONT color=#ff00ff size=2>value</FONT><FONT color=#ffffff size=2> <FONT color=#000000><=</FONT> </FONT><FONT color=#ff0000 size=2>9</FONT><FONT color=#ffffff size=2><FONT color=#000000>) </FONT></FONT>
<FONT color=#ffffff size=2><FONT color=#000000> selectThese = Request.QueryString[</FONT></FONT><FONT color=#00ffff size=2><FONT color=#808080>"</FONT><FONT color=#808080>category"</FONT></FONT><FONT color=#ffffff size=2><FONT color=#000000>];</FONT>
</FONT><FONT color=#ff00ff size=2>else
</FONT><FONT color=#ffffff size=2><FONT color=#000000> selectThese = </FONT></FONT><FONT color=#808080 size=2>"1"</FONT><FONT color=#ffffff size=2><FONT color=#808080>;</FONT>
<FONT color=#000000>}</FONT>
</FONT><FONT color=#ff00ff size=2>catch</FONT><FONT color=#ffffff size=2>
<FONT color=#000000>{</FONT>
<FONT color=#000000>selectThese =</FONT> </FONT><FONT color=#808080 size=2>"1"</FONT><FONT size=2><FONT color=#000000>;</FONT>
<FONT color=#000000>}</FONT>
</FONT>Admin
You're joking, right?
#1 - We don't know if the original code is .Net. From the looks of it, it seems to be Classic ASP, and you don't wanna get near error handling in Classic ASP. (Let's just say anyone using a "GoTo" in code not written in VB 3.0 should be taken out back and shot.)
#2 - Causing exceptions as part of normal processing is just plain bad coding. What if you get a real exception in there that you might want to catch, like System.Exception.WereFuckedException?? (And yes, I know that in certain situations there are no other options besides generating exceptions in normal processing, like when we have to hack around the foolish lack of an IsNumeric function in C#.)
All things considered, the code posted with the Select Case "1", "2", "3", etc. is the simplest, easiest to read, and least likely to be fucked up by other people maintaining it down the line. This try...catch block is just abhorrent.
Admin
One possible explanation might be that, at some time in the past, <FONT color=#808080>selectthese</FONT> and <FONT color=#808080>category</FONT> didn't match one-to-one, or there were gaps in the sequence.
I agree, though, that the most probable explanation is that they didn't know they could put multiple cases on one line.
Admin
Re: "like when we have to hack around the foolish lack of an IsNumeric function in C#."
What about Char.IsDigit() and Char.IsNumber() ? (Note: They DO handle Strings as well as Chars)
-blue
Admin
And again, for those of us who are not extremely nearsighted:
Re: "like when we have to hack around the foolish lack of an IsNumeric function in C#."
What about Char.IsDigit() and Char.IsNumber() ? (Note: They DO handle Strings as well as Chars)
-blue
Admin
Not quite correct, while those functions will take strings as input, when you do give them a string, you also have to specify an index into the string. So you're still only checking 1 character.
The solution is to use double.TryParse, which takes a string, and NumberStyles Flags to specify what types of numbers you want to allow (hex, integer, float, etc.).
So you can even use that function to check for integers only. Why double is the only numeric value type class that implements TryParse...I doubt even MS knows =)
Stephan
Admin
Personally, I'd have said:
... but perhaps that's just me [a highly-paid consultant].
Admin
Kermos,
Good point. I agree that Double.TryParse (or any of the other parses like int.Parse with a try/catch if you want to be specific) is the better way, rather than iterating over all the characters. Iterating over the characters wouldn't work either, as then it would allow strings such as "-1-2-3"...
I've been working in .net for about a year now, and so far I'm pretty happy with the core classes they provide. My biggest beef so far has been the lack of an FTP protocol class. As if SMTP and HTTP/S were the only protocols worth supporting out of the box! Luckily, there are several open-source free ones out there to choose from...
-blue
Admin
Blue:
Those functions take a string, but you also have to specify a character index, like so:
So you'd have to test every character to see if it's a digit (and also disregard the first instance of "." -- and error out if there's a second instance of "."). In this case, it's far more efficient to use the exception-based method (try..catch block around Int32.Parse) to determine if a number is numeric.
Admin
Whoops, that's my post above. I didn't see Kermos' reply. Thanks for the information on Double.TryParse. In all my googling for an IsNumeric C# function, this is the first time I've heard of TryParse.
Admin
By the way, what is the order of evaluation in Or statements? If it's right to left, you're wasting time evaluating statements that can't be true if the len() <> 1 condition is false. Better to nest those If statements.
Also, what's the value of selectthese if "category" doesn't exist in the QueryString collection? Would it error out?
Admin
FWIW, in .NET 2.0 all the numeric classes support TryParse.
Admin
<FONT style="BACKGROUND-COLOR: #efefef">Someone mentioned that the .NET v1.x classes doesn't have FTP support. You find both System.Int32.TryParse you'll find FTP support in v2.0 :)</FONT>
Admin
Cool! (That was me RE: FTP)
Admin
Yes
using System;
public class IsNumberSample {
public static void Main() {
string str = "non-numeric";
Console.WriteLine(Char.IsNumber('8')); // Output: "True"
Console.WriteLine(Char.IsNumber(str, 3)); // Output: "False"
}
}
Admin
Someone who doesn't need crutches to develop software. (The term 'programming' is beginning to give me a bad taste, and the term 'hacking' has been polluted by the media.)
IMHO compilers have allowed a lot of people to program who should not. Unfortunately that has carried over to scripting languages, and it's taking the fun out of development for me.
Admin
Bwahahaha. The dumbest code on the face of this earth. It's like saying if today is Tuesday then today is Tuesday.
Maybe we could all adopt this approach. I already have:
'This is the deluxe Edition dim a as string="1" dim b as string="" Select case a Case "1" 'Whehey... b="1" 'Woohoo the same as (a) Case Else 'Wow! We have a case just in case a isn't 1 b="NULL" 'Intelligent stuff. It changes it to null End Select