Comment On Not Quite Selecting a Case

Well, I was going to try to do some fun, year-end post. But, with server troubles and aparantly no one working today, I figured I'll just share with you code left behind for Rob to maintain from a (you guessed it) highly-paid consultant. [expand full text]
« PrevPage 1Next »

Re: Not Quite Selecting a Case

2004-12-31 13:40 • by
Sorry, I have a hard time believing even a "highly-paid consultant" would write something this stupid.

Re: Not Quite Selecting a Case

2004-12-31 13:52 • by

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


morons

Re: Not Quite Selecting a Case

2004-12-31 13:58 • by
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 :(

Re: Not Quite Selecting a Case

2004-12-31 14:18 • by RJ
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!

Re: Not Quite Selecting a Case

2004-12-31 14:31 • by hrmpf

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

Re: Not Quite Selecting a Case

2004-12-31 14:46 • by

Nope:



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


What if request.querystring["category"] is not numeric?


Nope:



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


What if cint overflows?


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

Re: Not Quite Selecting a Case

2004-12-31 15:43 • by Irrelevant
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"


Re: Not Quite Selecting a Case

2005-01-01 06:13 • by
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)]

Re: Not Quite Selecting a Case

2005-01-01 16:32 • by
My god this is so fucking lame. LAME!

Re: Not Quite Selecting a Case

2005-01-02 14:44 • by

Everyone is missing the point here!

Re: Not Quite Selecting a Case

2005-01-03 04:08 • by

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

Re: Not Quite Selecting a Case

2005-01-03 05:06 • by Katja
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?

Re: Not Quite Selecting a Case

2005-01-03 07:36 • by Jeff S

I like the variable name:


"SelectThese"


[:)]

Re: Not Quite Selecting a Case

2005-01-03 11:14 • by Kermos

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

Re: Not Quite Selecting a Case

2005-01-03 12:18 • by
Why would some write code in a language that lets a variable just be any damn thing it wants to be?  WTF!

Re: Not Quite Selecting a Case

2005-01-03 12:49 • by
:
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.

Re: Not Quite Selecting a Case

2005-01-03 18:05 • by Blue
:
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



Re: Not Quite Selecting a Case

2005-01-03 19:22 • by Deprecated

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!

Re: Not Quite Selecting a Case

2005-01-03 22:08 • by
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.

Re: Not Quite Selecting a Case

2005-01-04 10:05 • by Kermos

:
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

Re: Not Quite Selecting a Case

2005-01-04 11:12 • by

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!


Some of your solutions do not even work for all test cases, at least the sap who wrote the original code does.  WTF!


 


string selectThese;


try


{


int value = Convert.ToInt32(Request.QueryString["category"]);


if (value >= 1 && value <= 9


   selectThese = Request.QueryString["category"];


else


   selectThese = "1";


}


catch


{


selectThese = "1";


}

Re: Not Quite Selecting a Case

2005-01-04 13:34 • by
:

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!


Some of your solutions do not even work for all test cases, at least the sap who wrote the original code does.  WTF!


 


string selectThese;


try


{


int value = Convert.ToInt32(Request.QueryString["category"]);


if (value >= 1 && value <= 9


   selectThese = Request.QueryString["category"];


else


   selectThese = "1";


}


catch


{


selectThese = "1";


}



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.


 

Re: Not Quite Selecting a Case

2005-01-04 13:56 • by

One possible explanation might be that, at some time in the past, selectthese and category 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. 

Re: Not Quite Selecting a Case

2005-01-04 14:36 • by 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







Re: Not Quite Selecting a Case

2005-01-04 14:37 • by 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











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





Re: Not Quite Selecting a Case

2005-01-05 10:19 • by Kermos

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

Re: Not Quite Selecting a Case

2005-01-05 10:24 • by
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].

Re: Not Quite Selecting a Case

2005-01-05 11:42 • by Blue
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





Re: Not Quite Selecting a Case

2005-01-05 14:17 • by

Blue:


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

[code language="c#"]

bool bIsNumeric = Char.IsNumber("123456", 0);

[/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.

Re: Not Quite Selecting a Case

2005-01-05 14:20 • by

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.


 

Re: Not Quite Selecting a Case

2005-01-05 16:56 • by
:
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?

Re: Not Quite Selecting a Case

2005-01-05 16:58 • by
FWIW, in .NET 2.0 all the numeric classes support TryParse.

Re: Not Quite Selecting a Case

2005-01-05 19:11 • by andyandy

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

Re: Not Quite Selecting a Case

2005-01-06 14:42 • by Blue
27419 in reply to 27367
andyandy:

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





Cool!  (That was me RE: FTP)





Re: Not Quite Selecting a Case

2005-04-27 22:01 • by Dike
33450 in reply to 27289

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

Re: Not Quite Selecting a Case

2009-05-06 06:11 • by Anon (unregistered)
259980 in reply to 27231
:
Why would some write code in a language that lets a variable just be any damn thing it wants to be?&nbsp; 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.

Re: Not Quite Selecting a Case

2011-03-02 03:11 • by cindy (unregistered)
« PrevPage 1Next »

Add Comment