Comment On YesNoReturn

I sat here for a few minutes trying to think of some insight or commentary to add to this code that Tim Cartwright sent in. Maybe I'm still in awe, but I don't think there is anything I could possibly say to augment this ... [expand full text]
« PrevPage 1Next »

re: YesNoReturn

2004-08-05 12:46 • by Patrick Sullivan
I'm at a complete loss for words, it is good to know that "Court Ordered" always returns a "positive" response though!

Also wouldn't returning strings or integers from the same function cause problems? Maybe I'm getting to c# on that but that looks like a big problem to me.

Oh one more thing, love the blog, it brightens all my days!! Keep it goin!

re: YesNoReturn

2004-08-05 12:55 • by Jake Vinson
Every post on this site has someone writing "well, this implementation is actually good if you need to x."

I'm going to preemptively call whomever defends this a douchebag.

re: YesNoReturn

2004-09-07 22:25 • by Ineffigy
On of the powerful (and deadly) features of VBScript is its loose type definitions. The function itself has nothing wrong except the fact that there is no safe exit. Use CASE ELSE to create a default incase the data does not match any of the values listed. Also, I agree that you should either return strings or integers from this function for the sake of clarity. But this also depends on the purpose for the function, if this value will just be assigned to a string then it is fine, but if you are to compare or calculate the returned value, you will want to make the values all the same type.

re: YesNoReturn

2004-09-09 04:29 • by Bleeding Eyes
Hassan.. can you please supply a quick resume of your work history so that we know which projects to avoid inheriting

re: YesNoReturn

2004-09-12 11:07 • by Alchemist
:D

re: YesNoReturn

2004-08-05 13:13 • by Hassan Voyeau
Without knowing what language this is, I see absolutely nothing wrong with this. A simple function that does some translation. Big deal!!!

re: YesNoReturn

2004-08-05 13:18 • by Tim Cartwright
It is VBScript written for an asp page that I inherited.

re: YesNoReturn

2004-08-05 13:41 • by Dave M.
OH ... MY ... GOD! Was this designed to output as random a value as what it expects as input? strTemp? A temporary parameter? :blink: Returns a String or an Integer?

Tim, I am truly glad I am not you! :)

re: YesNoReturn

2004-08-05 14:11 • by Jon Choy
Hmm. it almost made sense to me if VBScript Select Case evaluates ALL of the conditions in order... but then I found the "0" -> No mapping.

I think this might be a case for a "once wasn't quite enough" refactoring!

re: YesNoReturn

2004-08-05 14:14 • by Jake Vinson
Actually, the more I look at this code, it's a good way to get YES, NO, 1, 0, or an empty string returned randomly. Therefore, I've successfully branded myself a doucheback based on my previous comment.

YesNoReturn(YesNoReturn(YesNoReturn(YesNoReturn(InputString))))

Who knows what that'll return?

re: YesNoReturn

2004-08-05 14:35 • by Chris B. Behrens
Looks like a data scrubbing routine to me. I had to write one that took "St.", "St", "Street", "Str", "Strt", you get the idea, and transformed it to "Street" in a different field.

re: YesNoReturn

2004-08-05 15:15 • by Addy Santo
The first thing that passed through my head when reading this was the "Brilliant!" from the Guiness commercial :)

This one: mms://windowsmedia.dvlabs.com/adcritic/guiness-bread.asf

re: YesNoReturn

2004-08-05 15:32 • by cablito
Its a multi purpose two way function. You can call it with any kind of parameters and it will return a useless conversion either way.

YesNoReturn("Y") returns "YES" right?
now... Yes does not seem that he wants so here is the code...

If YesNoReturn("Y") = "YES" THEN

'now we get the integer
dim final as intenger
final = VAL(YesnoReturn("YES"))
If Cbool(Final) = True then
msgbox "User answered Yes",1,"Dan Appleman book Test program #1"
end if

end if

People actually never heard of switch... ooo asp vbscript support switch? I can´t remember.

re: YesNoReturn

2004-08-05 15:33 • by cablito
I hope Hassan was being ironic.

re: YesNoReturn

2004-08-05 15:50 • by Tim Cartwright
cablito switch == select case...

re: YesNoReturn

2004-08-05 16:19 • by qwerty
Actually that function does work pretty well. You just need the proper wrapping function. And I think I have it:

Function ScrubThisSchnizzel(TakeThis)
do
TakeThis = YesNoReturn(UCase(TakeThis))
loop while VarType(TakeThis) <> vbInteger And Not (VarType(TakeThis) = vbString and TakeThis="")

if VarType(TakeThis) = vbString then
ScrubThisSchnizzel = "WTF??"
else
ScrubThisSchnizzel = Chr(Asc("N") + TakeThis*11)
end if
End Function

re: YesNoReturn

2004-08-05 16:59 • by Hassan Voyeau
No. I was not being ironic. Thus far, no one has shown me what is wrong with this function. I would be nice if we were told how the function was used but as it stands, I see absolutely nothing wrong with it...

re: YesNoReturn

2004-08-05 17:28 • by riviera
Well, the first thing that's wrong with it is that it's written in an ugly language :)

But seriously, good code should _tell_ you what it's meant to be doing (as my favourite saying goes: a programming language is a form of communication -- but not just with a computer, but with other people [who read your code]).

This code neither have any visual structure, nor it does tell you anything about it's purpose, than it returns some values to some input, but none of them makes any sense (and it's not because it's taken out of it's context).

As previously said, one of the very first things that strike the reader is the various return values (strings and numbers). Why don't simply return just a boolean, for instance?

Anyway, I've seen worse, but it's still a kind of shame. There are a lots of people who'd need to learn how to program properly.

re: YesNoReturn

2004-08-05 18:00 • by Kettle Korn
Hassan, Since you don't see what's wrong with that function, you probably won't see anything wrong with the code below:
Private Sub csiteid_Chk(ChkStrg As String, retval As Integer)
If (ChkStrg <> "") Then
Dim myuserid As String
Dim cursorNbr As Integer
Dim isAuthorised As spResults

Call GetBufferValue("bpes.userid", myuserid)
Call SqlCursorEx(cursorNbr, NOLEVEL + SqlFastReadOnly, "custom", "", "")
Call SqlFetch1(cursorNbr, "System.dbo.sp_UserAccess_UserId_Site" + SParm(myuserid) + SParm(ChkStrg), isAuthorised, LenB(isAuthorised))
Call SqlFree(cursorNbr)
If StrComp(Trim(isAuthorised.results), "TRUE", vbTextCompare) = 0 Then
Else
MsgBox "You are not authorised to use this site. Contact HCL IT if this is a problem for you."
Call SetObjectValue("csiteid", "")
retval = -1
End If
End If
End Sub

re: YesNoReturn

2004-08-05 20:35 • by qwerty
Nice code Kettle! Am I reading that right, did they really have a separate stored proc for every user/site combination? Or does SParam somehow make them parameters?

re: YesNoReturn

2004-08-05 21:29 • by The Wolf
I have to agree with Hassan, this code could be doing something completely valid, it might be designed to convert values that users have entered that are incorrect. Say the user typed in 'n' instead of what the program is expecting, then it converts it to 'yes'.

re: YesNoReturn

2004-08-05 22:11 • by Prakash
WTF is this.

re: YesNoReturn

2004-08-06 02:40 • by Nick
Uh, OK, Wolf, whatever you say. . .

1. The function name doesn't tell us what it does. It may return "YES", "NO", 0 or 1 depending on the input value.
It should be two functions at least - such as "makeYesNo" for when you want a string and "makeZeroOne" for when you want a psuedo-bool.
A function which returns entirely unrelated types, depending on the input's *value* is seriously bad ju-ju.

2. WTF do "COURT ORDERED" and "NV" have to do with anything?

3. Hmm, doesn't VB let you match multiple values in a single case (i.e. Case "X", "Y", "Z")? Then there should only be one case for each possible return value. . .

re: YesNoReturn

2004-08-06 03:53 • by RJ

This could be a great hiring technique. Show people these snippets, if they can't tell you why they are monstrosities or if they try to defend the code you can show them the door straight away and save everyone time :)

re: YesNoReturn

2004-08-06 04:19 • by Ray S
If it output EVERYTHING as Y/N or TES/NO or True/False whatever, this would be perfectly fine. Random input types matching to random output types with some odd possibilities for recursion? Yikes...


And hey, where's the "CASE ELSE"? What if someone enters "maybe" or "I'm not really sure, let me ask my boss"? :-)

re: YesNoReturn

2004-08-06 05:10 • by Simon
On the face of it it looks awful returning different values when you would have thought a simple true/false return value would be in order.

But taken out of context you have no idea what the guy was trying to achieve, Hassan is correct it looks fine on the face of it, do any of you bitches have the spec ? No you dont, except maybe Tim who posted it, perhaps if he elaberated in the first place about its function, or maybe even tell us what did he change it to ! Did you change it Tim or just leave this supossed abhoration as is.

re: YesNoReturn

2004-08-06 06:12 • by Wolfgang
The problem I see with this code is that, besides it returning different types of return values, there doesn't seem to be a clear need for some result. All entered values are mapped to some pseudo boolean. The strange thing is, if you feed it "Y" it will tell you "YES". But if you feed it "YES", it will tell you 1 and if you feed it 1 it will tell you "YES". Any function that eats whatever this function spews must do some translation yet again, while this little schizzle could just return either true or false, based on a variety of input, like so (C#):
bool TranslateInput(string input)
{
switch (input.ToLower())
{
case "y":
case "yes":
case "1":
case "true":
case "court ordered":
return true;
case "n":
case "no":
case "0":
case "false":
case "nv":
return false;
default:
throw new NotSupportedException("Encountered unexpected value '" + input + "'");
}
}

re: YesNoReturn

2004-08-06 07:34 • by cablito
I still can´t believe people saying it looks fine.

re: YesNoReturn

2004-08-06 08:33 • by Hassan Voyeau
And no, I don't see anything wrong with my code either. And no there is no separate stored proc for each site/user combination. SParm obviously attaches a paramter and its the function that is supplied with Microsoft's Solomon API.

re: YesNoReturn

2004-08-06 12:22 • by Andy
Data scrubbing? This is more like data *laundering*. Couple passes through this puppy and Feds'll have no idea where the value came from...

re: YesNoReturn

2004-08-06 14:31 • by Metallisoft
Wow, yes, this would be a great interiew tool!

"How does this look?" ::passes paper with that, um..code..on it

"Looks fine?"

"You're fired."

"But this is an interview!"

"Exactly!"

Haha!

re: YesNoReturn

2004-08-06 15:59 • by Phil Scott
Simon's quote "do any of you bitches have the spec" should go down as the subtitle of this site.

re: YesNoReturn

2004-08-07 05:31 • by The Wolf
Nick: Yeah, you wouldn't do it writing a function like that.

re: YesNoReturn

2004-08-09 17:56 • by Tim Cartwright
K, The code :
1) was sent is as is.
2) is not used for data scrubbing.
3) is used in production, not in a test.
4) has many different uses.

Hopefully I answered every ones questions.

re: YesNoReturn

2004-08-09 18:04 • by Tim Cartwright
From Hassan's BLOG :

"Active Server Pages
I create my first ASP pages this week. A simple login
screen, some parameters and 2 reports using tables, although I guess I should be
using CSS, but tables are the first thing that comes to my mind. All I need to
do now is add some encryption!"

So, Hassan, feel free to use this function in your pages for encrypting since
you see nothing wrong with it.

RE: YesNoReturn

2004-08-10 10:55 • by hassan.voyeau@gmail.com (Hassan Voyeau)
I'm sorry but i will not be using that function but I still don't see anything wrong with it, and you stil did not show us how you fixed that function.

re: YesNoReturn

2004-08-10 13:28 • by Tim Cartwright
Hassan, I could not fix it, it was in use in so many different places that to
re-factor would cause me more time and pain than it was worth. I did not explain
what was wrong with it, as several people did above.

To summarize what they have said above (although not in the same words):

A function should take in and return a well defined type(s) with repeatable
results that are of the same type(s). The argument strTemp can be, and is
several different types, and values. Integer String, String, Empty String, and
Boolean String. The function can then return many different things : "YES",
"NO", 1, 0, and "". Can you imagine if Microsoft decided to change the
CreateObject method to return an object sometimes, and a string other times, and
a boolean at other times? The CreateObject API would be horrendous to use. When
creating a method, the input, and return should be well defined. It is an
unspoken contract between your code, and the consumers of your code.

re: YesNoReturn

2004-08-10 19:02 • by Robert Sharp
Since Hassan can't seem to understand the myriad explanations of the ridiculousness of this code, let me take a crack at it...

You see, it stands to reason that: if given the input Y we want the output to be the "full form" response YES, then the function would be used the other way around: to return the short-form Y when given YES.

However, that's not what happens here. What we have here is an instance of "scratch my back and I'll, *shrug*, give you cholera." Or more simply, a case of

a == b,
b == c,
therefore
a != c

which is a paradox and more than slightly impossible. It just doesn't make any sense to expand Y to YES but not contract YES to Y.

Besides, as everyone has been mentioning, it would have made things easier and less silly if there would have been a ResponseToBool() conversion function. But hey, then we wouldn't have gotten to laugh at it. Perhaps when he programmed it, the guy was in one of those "seemed like a good idea at the moment" mentalities, sort of like asbestos in walls, or CFCs as propellants.

re: YesNoReturn

2004-08-17 02:46 • by nec
you don't know programmer's expect or the purpose of function while he/she is coding that crap, so it is impossible o say whether that is good or bad coding.

re: YesNoReturn

2004-08-19 05:42 • by Shambo
Yes you can.

I don't mean to go on a rant here but it is 4:30 in the morning, so here goes.

English is your second language, isn't it? You don't have a first. Clearly, you spend way too much time in darkened rooms in front of your seven-year-old computer turning a whiter shade of pale. Go outside once in a while and breathe, before your brain starts to rot from all that festering stagnation and cognitive dysfunction

re: YesNoReturn

2004-08-19 05:43 • by Shambo
BTW... that was for nec. I need more dew.

re: YesNoReturn

2004-08-26 00:10 • by Distilled Software Hate
For the people who still don't understand...

Here's what's wrong with this code:

No matter WHAT the correct value is supposed to be, this routine will always produce the incorrect value for some inputs, and what is worse it is guaranteed to produce almost all the possible wrong input values even when the correct input was entered.

Which means that the routine that calls it has to do more work to validate the output of this routine than to validate the input of this routine.

Re: YesNoReturn

2006-07-21 02:37 • by Ronabop
I've seen this before. By people with the best of intentions, and the slightest mistakes made in over-assumption. (I also saw it shut out 16,000 live user sessions, and the phone switchboard go absolute batshit as a result).

Okay, a bizarre hypothetical (based on a real problem): Say for example, a programmer has a series of designer-created web pages that they cannot change, but *must* write back-end code for, gluing into functions that they *also* cannot change (a very typical 3-tier-hell problem, if there are 3 groups of even slightly disconnected people). While the programmer *cannot* change the values being fed to them by the page, or the values in the storage handling tier, the programmer still does need to properly handle them, for each given scenario.

Examples:
1. A login page feeds them a "remember login" value of Y/N, but the arguments to AuthenticateUser(arg) requires arg to be of the format of "YES"/"NO".
2. A preference page feeds them a "show last login" value of YES/NO, but the arguments to DisplayLastLogin(arg) requires the args to be of the format of 1/0.
3. A record changing page feeds them a "Legally bound" value of "COURT ORDERED"/"NV", but the arguments to AttendanceRequired(arg) requires the args to be the format of 1/0.

Now, if one was working with a small flock of page designers, and a small flock of backend programmers, and was stuck in the middle (while trying to glue 20K plus web pages), I can see how it might be truly tempting to go with this route, a universal "rewrite" function. Rather than write cleanups each freaking time a "bad" value comes in, feed it to the library.

However....

As soon as a designer Feeds them a value of "False", that *has* to go into the database as '0', this whole clever glue plan rapidly unwinds.... *unless* the HTTP_REFERER (or some other argument) is taken into account, in which case the "False" text can be handled with an additional check (ugh...).

It rapidly decends into a massive bloat library of "if diplay tier says $this, it means $that in the data tier".

The solution to this scenario, however, is more political than programmatic.

-Bop


Re: YesNoReturn

2006-08-15 02:46 • by Noizee Brat
Alex Papadimoulis:

I sat here for a few minutes trying to think of some insight or commentary to add to this code that Tim Cartwright sent in. Maybe I'm still in awe, but I don't think there is anything I could possibly say to augment this ...






Am sorry but i still can't figure out Why/How/When/Where this Omnipotent function would be called.
oh wait mebbe if i augment it with ..
Case "wtf"
    YesNoReturn = "YES"


Yea i get it now!

Re: re: YesNoReturn

2006-08-15 10:25 • by mnature
86187 in reply to 22999

Anonymous:


1. The function name doesn't tell us what it does. It may return "YES", "NO", 0 or 1 depending on the input value.
It should be two functions at least - such as "makeYesNo" for when you want a string and "makeZeroOne" for when you want a psuedo-bool.
A function which returns entirely unrelated types, depending on the input's *value* is seriously bad ju-ju.


You forgot one, I think


Case "" ----> YesNoReturn = ""


So if you put nothing in, you get nothing out.


Just as a dumb question:  Does upper- or lower-case matter in this?  I notice that everything is in upper-case except for the False and True cases.

Re: re: YesNoReturn

2006-08-16 03:16 • by Some Idiot
86321 in reply to 22994

Anonymous:
Well, the first thing that's wrong with it is that it's written in an ugly language :)

But seriously, good code should _tell_ you what it's meant to be doing (as my favourite saying goes: a programming language is a form of communication -- but not just with a computer, but with other people [who read your code]).
..........


I agree. Look at the source code for this flight simulator by Carl Banks. It clearly shows the reader what the program does.


Unlike the drivel above. It's written some VB scripting thingy for chrisakes!!


 

Re: YesNoReturn

2007-05-13 15:43 • by Igor (unregistered)
I get it, you simply run this function recursively on its own result until you get either true or false. And if someone passes in "" then it gets stuck in a loop and you easily find out who passed an empty string.

Re: YesNoReturn

2007-05-16 10:51 • by Jan (unregistered)
Isn't the problem with this function, that imagining the system in which it would make sense, would give you nightmares for weeks?
« PrevPage 1Next »

Add Comment