- 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
Yes, because obviously this is the 10% of the code where the application spends 90% of its time... Long live premature optimization!
First of all, your "optimization" is semantically incorrect:
Second of all, your claim about the speed of "==" is unsubstantiated; I have run the test above with various values for s2 in a loop of 10 millions iterations and measured the timings in each case:
Third of all, this block of code is most certainly not worth optimizing anyway: unless there are thousands or millions of such tests performed sequentially within this method, the time spent in this particular piece of code per se is ridiculously small. And thinking about cases where the whole method itself would be invoked thousands of times sequentially is irrelevant, since the execution time of the whole method is largely dominated by the time during which it waits for the user input.
In any case:
Now, to come back to the original article, maybe the author took inspiration from the infamous over-simplistic example of the XP practice of writing the simplest code that passes test cases (the stupid example in which to write a function add(x, y), you start by returning 2 because your test case only exercises (1,1); and then you fix it when you add more test cases), and along the way forgot the word "simplest"... was impregnated with XP practices such as "write the simplest code that passes the test cases
Admin
It doesn't accept "indoubetedly"? Tsk.
Admin
if ( "maybe".equals(answer) ) { return ( rand() > 0.5 ); }
Admin
I can also remember writing comments like, "PHB thinks this need to be done this way, but I disagree. It is wrong for reason ______".
Admin
even when XP is involved :eyeroll:
Admin
Why didn't the dev just use a table of strings? I mean really, look at it.
Admin
The real wtf is where are the software patterns here?
We can introduce the ChoiceAbstractFactory class and interface. We can follow this up with derived YesFactory, NoFactory, and FileNotFoundFactory classes and interfaces. Of course the factory should create instances from an XML config file, something like
<choices> <choice> <choiceid>somebigguid</choiceid> <choiceValue>yup</choiceValue> </choice> <choice> <choiceid>someotherbigguid</choiceid> <choiceValue>si</choiceValue> </choice> <choices>Of course lets not forget everything should be in a resource file, so we can handle international choices via satellite assemblies. I'm sure we can fit in more XML everywhere.
Admin
This is a small part of the code. Presumably the string has already been cast to lower case.
Although you might not want to create a new array every time you call this method. It looks like it is intended to be used throughout the application.
Not all applications have GUI's.
'y' + Enter = 2 keystrokes 'yes' + Enter = 4 keystrokesStill, your boss will be pleased, and you can leave the office at 1pm each day.
Ridiculous is assuming that the code didn't make such an obvious cast to lower case considering that there were no reports of it having problems with this in the story. The code is silly, however there is nothing mentioned to lead us to believe that it is wrong.
Because every single word beginning with the letter 'y' means 'yes'? This code might be called by critical areas of the system and doing such a naieve test for confirmation might not be wanted.
Oh my. When measuring the length of time it takes for different parts of a program to run, user input is pretty much as slow as it gets. You could test for .equals() thousands of times without having any noticeable effect on the time it takes to run the application.
..Then there is the fact that == and .equals() are very different things. == tests for equal memory addresses, .equals() tests for whatever the object tells it to test.
Admin
The real WTF is not that they used all these options for yes and no, provided that that was required in the specification.
The real WTF is not using a hashtable with the user response as the key and the return value as the value.
O(1)
Admin
How do you know he didn't lower case the string stored in answer before the comparision?
Admin
You missed "narp".
Admin
Admin
So what made it hang?
Admin
From now on I figure all text-prompts most respond affirmatively to the piratical "Yar", otherwise the programmers shall be confirmed racist against proud Buccaneer-Americans.
Admin
You forgot "negatory"
Admin
What's Pirate for "no"? "Yar with a more negative inflection"
Admin
I say, you really want a (private final static unmodifiable) Set here, HashSet's lookup is in O(1) while Array#indexOf should be in O(n). Also, Set#contains would return a boolean (simpler code) and marks your intent much more clearly than Array#indexOf does.
Admin
Apart from the fact that it's not very elegant code, I don't see where time is an issue. Okay, getting 26 words might take a few moments, but even with cut and paste it's not even a minute to enter them. And it's even faster with a macro.
Admin
Nah Nope Nada Uh ah no way negative absolutely not Fer sure Non Don't think so Maybe Ofcourse I will If time permits 110%
Admin
What if you ask him "Are you going to stab me?"
Admin
couldn't he at least use else ifs?!?!
Admin
Try this language http://www.tlc-systems.com/babbage.htm
"Structured languages banned GOTOs and multi-way conditional branches by replacing them with the simpler IF-THEN-ELSE structure. Babbage has a number of new conditional statements that act like termites in the structure of your program: WHAT IF - Used in simulation languages. Branches before evaluating test conditions.
OR ELSE - Conditional threat, as in: "Add these two numbers OR ELSE!"" ...
Admin
Um, supprised no ones mentioned that equalsIgnoreCase(String str) might work a little more often :P
Admin
If you wonder why it hung after the prompt - I think the user made a typo and the whole code looks like this:
Admin
This story - like all the stories on this site - is just made up out of whole cloth. Never happened. But, what the heck, I'll occasionally dip in here to see what kind of BS people are willing to believe.
Is the funny part of the story the fact that it can't deal with "answer" being equal to null?
The code should be, e.g.:
if("yar".equals(answer)) { }
etc., ...
Admin
I just couldn't help myself. I had to rewrite it. He forgot the foreign versions of true and false. oiu, ja, si, the list could go on forever. no???? (meaning yes)
while (! ( String.Compare(answer, "y", true) || String.Compare(answer, "yes", true) || String.Compare(answer, "n", true) || String.Compare(answer, "no, true) ( { System.out.println("dude it's either yes or no!!!");
System.out.println("Try again!!!"); -- insert code for getting input
}
if ( String.Compare(answer, "y", true) || String.Compare(answer, "yes", true) { return true; } else return false; }
Admin
Rumors of your intelligence, like all the rumors you ever hear, are just made up out of whole cloth. Not a fact. But what the heck, we'll occasionally allow you to visit and post nonsense just to see what kind of idiotic things you'll say.
Nah. Nope. No. Never. Nil. Nyet. Nanu nanu.
The funny part of the story is that you obviously have no real experience with bad code (other than that you yourself have written, of course). Anyone with real experience has seen stupid source code like this (and worse), because they've dealt with the type of programmer that would do this - you know, people like yourself. (I include you because you obviously haven't seen bad code like this before, which means you don't know how to recognize it when you see it, which means it's probably your code. HEY! I just figured out where the code from the OP came from! YOU WROTE IT!!!!)
Admin
its Nay moron
Admin
You mean FILE_NOT_FOUND?
Admin
Perhaps instead of suicide, Requirements would be in order? That way, the mean QAer wouldn't have to propose so many solutions.
... I'm just sayin'
Admin
now you are talking risk processing
Admin
return FileNotFound;
Admin
Admin
Does that mean its pirate software?
Admin
"Code rage" made me laugh.
Did you just make that up or is it already a meme?
Admin
Never mind. I should have Googled it first. Is should Google everything first. But then I would never have time to ask any questions.
Admin
IMHO this looks like a pragmatic approach to code around user input to me... This shows an ability to be creative and consider an end user. I'd hire this individual, in lieu of the non-creative 'One way to skin a cat' coder.
Admin
Why not use a real programming language which has a simple messagebox class which returns constants... Hell, you could even localize it then as well, imagine that.
Some people should never be allowed near a compiler, then end up draining the world economy :P
Admin
What about the Cowboy Neal option?
Admin
Your code is crazy too, so I fixed it:
Admin
Admin
Admin
I was almost impressed except for the lack of 'yarp' and 'narp'.
Admin
The Real Wtf is that such answers must be placed in arrays like (sorry for my python, i'm lazy to rewrite it to java):
answers_yes=['yap', 'yes'] answers_no=['no','nope'] if answer in answers_yes: return True if answer in answers_no: return False return FileNotFound
Admin
I wrote something similar once, although it was more multi-lingual ("da", "si", "oiu" and so on) than slangish. My favorites, though, were "perhaps" and "maybe". I implemented these for Monte-Carlo testing, where "perhaps" returned true 25% of the time, and "maybe" returned true 50% of the time.
Admin
Admin
string[] yes = new string[]{ "y","yes,"yep","ok", "proceed","affirmative","positive", "correct","aye","fine", "yar" };
void DoStuff() { // shorter . if( yes.Contains(Console.ReadLine()) ) { //more random stuff. }else Usage(); }
Admin
string[] yes = new string[]{ "yep","ok","proceed","affirmative", "positive","correct","aye","fine","yar" };
void doSomething() { if(yes.Contains(answer)) { //programs should be less forgiving. } }
Admin
Why didn't this guy use an array of all the yes words and loop through them (or hash, no looping required but a pretty pathetic excuse to waste memory) so we don't have to be forced to read his stupidity.
Admin