• (unregistered)

    Sorry, I have a hard time believing even a "highly-paid consultant" would write something this stupid.

  • (unregistered)

    You obviously don't understand the merits of 'case based' programming.

    morons

  • (unregistered)

    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 :(

  • RJ (cs)

    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!

  • hrmpf (cs)

    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

  • (unregistered)

    <FONT size=2>Nope:</FONT>

    <FONT size=1>cat = cint(request.querystring("category"))
    if cat >= 1 and cat <= 9 then
        selectthese = cat
    else
        selectthese = 1
    end if</FONT>

    <FONT size=2>What if request.querystring["category"] is not numeric?</FONT>

    Nope:

    <FONT size=1>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</FONT>

    <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

  • Irrelevant (cs)

    IsNumeric() won't work, because this is just after single digits, not decimals, exponents, signs, etc. This should be quite sufficient:

    selectthese = Request.QueryString("category")
    If Not(selectthese Like "[1-9]") Then selectthese = "1"
  • (unregistered)

    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)]

  • (unregistered)

    My god this is so fucking lame. LAME!

  • (unregistered)

    <FONT style="BACKGROUND-COLOR: #efefef">Everyone is missing the point here!</FONT>

  • (unregistered)

    :
    Sorry, I have a hard time believing even a "highly-paid consultant" would write something this stupid.

    He was probably paid by the number of LOCs her created [:)]

  • Katja (cs)

    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?

  • Jeff S (cs)

    I like the variable name:

    "SelectThese"

    [:)]

  • Kermos (cs)

    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

  • (unregistered)

    Why would some write code in a language that lets a variable just be any damn thing it wants to be?  WTF!

  • (unregistered)
    :
    Why would some write code in a language that lets a variable just be any damn thing it wants to be?  WTF!


    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.
  • Blue (cs)
    :
    Why would some write code in a language that lets a variable just be any damn thing it wants to be?  WTF!


    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

  • Deprecated (cs)

    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!

  • (unregistered)

    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.

  • Kermos (cs)

    :
    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.

    I'm telling you, believe it. I personally have seen stuff like this in production code with my own eyes!

    Stephan

  • (unregistered)

    <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>
  • (unregistered)
    :
    <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>

    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.

     

  • (unregistered)

    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. 

  • Blue (cs)

    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



  • Blue (cs)
    Blue:
    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





    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


  • Kermos (cs)

    Blue:
    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



    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

  • (unregistered)

    Personally, I'd have said:

    selectthese = Request.QueryString("category")
    if len(selectthese) <> 1 or selectthese < "1" or selectthese > "9" then selectthese = "1"

    ... but perhaps that's just me [a highly-paid consultant].
  • Blue (cs)

    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


  • (unregistered)

    Blue:

    Those functions take a string, but you also have to specify a character index, like so:

    [code language="c#"]

    <FONT color=#0000ff size=2>bool</FONT><FONT size=2> bIsNumeric = Char.IsNumber(</FONT><FONT color=#800000 size=2>"123456"</FONT><FONT size=2>, 0);

    </FONT>[/code]

    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.

  • (unregistered)

    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.

     

  • (unregistered)
    :
    Personally, I'd have said:

    selectthese = Request.QueryString("category")
    if len(selectthese) <> 1 or selectthese < "1" or selectthese > "9" then selectthese = "1"


    ... but perhaps that's just me [a highly-paid consultant].

    Well, string comparisons using < and > aren't really that great, nor are implicit string/number conversions. I'm also not a big fan of your shift key phobia, but I assume you were writing this post as quickly as possible.

    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?

  • (unregistered)

    FWIW, in .NET 2.0 all the numeric classes support TryParse.

  • andyandy (cs)

    <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>

  • Blue (cs) in reply to andyandy
    andyandy:

    <font style="background-color: rgb(239, 239, 239);">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>



    Cool!  (That was me RE: FTP)


  • Dike (unregistered) in reply to Blue

    Blue:
    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



     

    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"
        }
    }

  • Anon (unregistered) in reply to
    :
    Why would some write code in a language that lets a variable just be any damn thing it wants to be?  WTF!

    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.

  • cindy (unregistered)
    Comment held for moderation.
  • Sean (unregistered)

    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

Leave a comment on “Not Quite Selecting a Case”

Log In or post as a guest

Replying to comment #:

« Return to Article