- 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
but you can debug the code better :D
Admin
<FONT face=Georgia color=#ff0000>You gotta ask yourself though, which looks prettier? [;)]</FONT>
Admin
Obviously there are cases for code like this.
Take VB, for example, which does not have short-circuit evaluations. This could lead to much more efficient code if the function in the if statement actually takes a certain amount of time.
Perhaps the author if this nugget of clean, efficient code came from a Visual Basic background and understood this, and was in the habit of creating hand-tooled short-circuit evaluations such as the one you see above.
Of course, any sufficiently trained monkey could look at the documentation for the language and see that it does support evaluations of this type.
BTW, what language is this? It looks like psuedo-c...
Admin
It's an optimization. The second version doesn't need to execute all the tests when one of the earlier ones fails.
Admin
it looks like c# so this type of optimization is inherent in the language.
but thanks for playing today's exciting game of "excuse the inexcusable." we have a very lovely consolation prize for you...
Admin
Nested ifs, early return statement, magic strings, using a string when an int would work... It's all right there for ya. I give it a 4.5/5
Admin
Dude, you are kidding, right! That code looks like C++, Java or C#. All three support short-circuiting.
Admin
It looks like C++ to me.
Admin
WTF is wrong with an early return?
Admin
VB.NET now supports short-circuiting operations as well, so even that isn't an excuse.
In VB.NET
AndAlso is the short circuiting AND
OrElse is the short circuiting OR
Admin
Boolean operators
&&
and||
stop executing as soon as the truth of the final expression is determined. This is the C standard (if the code fragment is C).The two forms are 100% equivalent.
Might be style preference, or easyer debugging.
But I do not think is a WTF.
Admin
The real shame here is that there's a MUCH more elegant way to do it.
<FONT face="Courier New">switch(typec)
{
case 20:
break;
case 13:
break;
case 5:
break;
case 4:
break;
default:
SelectType("ALLOC");
break;
}
</FONT>
<FONT face="Courier New">[:D]<FONT face=Arial size=1>(the above was a joke btw)</FONT>
</FONT>Admin
Heh I'd probably write it like this:
if ((typec!="20") && (typec!="13") && (typec!="5") && (typec!="4"))
if (typec == 20 || // 20 means...
typec == 13 || // 13 means...
typec == 5 || // 5 means...
typec == 4 // 4 means...
)
{ // we don't need to SelectType for these
// because...
}
else
{ SelectType("ALLOC");
return;
}
there's also a typo, Alex - you've got an extra ) after the 4
Admin
I hereby propose that this website be renamed to the above
Admin
Hahaha! You can't be serious... can you?
Admin
Some people believe functions should have one and only one way into and out of them. That way if you have foolishly written a monsterously long function, you won't have to read the whole thing to see if you have missed a return statement that prevents your changes from getting executed.
Forcing functions to be short and do only one thing is too much of a burden. Best to just outlaw all types of branching, including if-then, loops, and switch-case statements with more than one possible case.
Admin
Because it's not 'structured programming' and structured programming is a religion of which there is no part that can be questioned without the believers covering their ears and yelling "nanananana!"
Therefore, we must accept the horrible obfuscations that are often required by having a single return statement as being superior to the easily readable early return versions.
Admin
No good language implementation will in the first one either.
Actually, in any half-decent implementation of just about any language, the second one is quite a few more op codes.
This is why you can use && and || for ternary-esque operations (although, not always returning a value, depending again on language and implemenation), by doing if("stupid") && then("kill them") || exit()
exit() is only called if("stupid") returns a false value, and then("kill them") is only called when if("stupid") returns a true value.
Any fucking programming 101 class or operator reference or a bazillion other things should have told you this, what the fuck, guy.
Admin
Now that's efficiency [:D]
My comments on the WTF:
It makes me wonder why none of today's languages have a construct like SQL has, that will let you do things like SELECT (...) WHERE @whatever NOT IN (1, 3, 5, 7, 9). Of course you can use an array or whatever else, but a language construct would be pretty cool. Maybe like:
<FONT face="Courier New" color=#000000>if (typec !in ("20", "13", "5", "4")) {
DoStuff();
}</FONT>
or something.
Admin
Okay this just makes me giggle like a schoolgirl.
Admin
There are 4 possibilities here:
1. There was a requirement to enter comments so what better comment than code to explain what the code should do.
2. Was getting paid by the line and needed more beer and pizza than the 1 line paid so went with the 6 lines.
3. Was getting paid by the line and it required comments.
4. WTF
[8-|]
</FONT>Admin
Looks like any one of:
C++
C#
Java
Pike
D
ECMAScript
But is definitely not C
Additionally, in any of these languages the expression could have been wonderfully optimized by performing a type conversion and making an integral comparison instead of a string comparison (which requires far more operations). Wow, the WTF-posters are almost worse than the WTFs.
Admin
PHP has that and it's pretty handy for data validation and whatnot.
<FONT face="Courier New">if (!in_array($typec, array(4, 5, 13, 20)))</FONT>
<FONT face="Courier New"> complain();</FONT>
Admin
I meant "something like that using arrays", not having read your post too carefully :)
Admin
Yeah, someone should invent some sort of "search" tool for source files or even make some sort of editor that makes control statements such as return stand-out somehow. Like, maybe they could be a different color or something.
The flip side to that argument is when you ae modifying some stupidly long method and you want to change what it returns under certain conditions. Since the developer has some dumbass return variable, you can't be sure you've done it without walking though the entire method to verfiy that nothing else modifies that return variable.
Yeah, these kinds of rules are pretty worthless. People should just keep things short and code it the most natural and understandable way.
Admin
I see this, and while I accept it's not the best hting to do, I also don't see it as the worst thing. What's worse would be someone subscribing to the "Only 1 line perstatement" philosophy, and then doing something like that. I've had to fix up people code where they have if statements go on 3 screens wide (and I run my screen at 1600 x 1200 rez), and there I'd just wish that they had used separate if clauses if they aren't going to break the statement up.
Admin
How could you leave out JavaScript?
Also if it is Java, there is another WTF.
Admin
Absolutely serious
MSDN article describing this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconLogicalOperators.asp
Admin
This is David Koontz the original poster of the code. I agree that because of the lack of short circuting (which I was not aware of in VB) that the expanded block could possibly be faster, but all that extra code to save just a few small equality checks? Anyway, I don't know what happened when the example got "anonymized" but originally all those AND's in the commented out line were OR's which somewhat changes the WTF. When I found it I immediatly saw that whoever was coding the thing didn't understand the difference between OR and AND and therefore did it the second way to acheive the result they wanted instead of just changing a few keywords.
Admin
It's not Java. In Java, we would generally use !(string1.equals(string2)) to compare strings, rather than string1 != string2.
Admin
I believe ECMAScript was meant to stand in for JavaScript since it's sort of the "official" version now. I do find it rather annoying though to have two names for basically the same language.
Admin
I work with David, and I can assure you that the programmer who wrote that code didn't take into account short circuit evaluation, or logic for that matter.
I think the greater WTF is that while David cleans this stuff up, the other "programmers" are still there leaving more of this code behind!
From one code janitor to another, I feel for you David. At least your job isn't going anywhere! d:
Admin
He mentioned ECMAScript; JavaScript would be pretty much implied with that. Not to mention it's an incomplete list of possible languages this could could be written in. It doesn't even matter what language it's in, its stupidity transcends language.
Admin
It looks to me like the coder had it coded the right way, but during development the check was always failing so they expanded it to debug it and forgot to put it back.
Admin
OrElse is offically my new favorite operator.
Admin
Two parentheses too many.
Admin
What do you mean none of today's languages? [:P]
Admin
Surely you meant: "your job isn't going away..."
Admin
No, working on this code my job surely isn't going anywhere. Career either for that matter. :'(
Admin
How is that even possible? [:(]
Admin
Don't forget python!
if(x not in (1,2,3,4,5)):
doSomething()
else:
doSomethignElse()
Admin
A WTF in AndAlso of itself OrElse its an elegant solution to the sins of VB past.
Admin
People get "promoted" out of customer service positions because "they know Excel macros". Now they have an initiative here to move the tech support people into programmer positions which I suppose is a better plan, but they're having the current programmers teach these up and coming ones. "The blind leading the blind" springs to mind.
Admin
Admin
My version (just comment the f*** line for the magic numbers or at least
delcare constants)
// We need to call out to SelectType() if typec is not:
// 20 - blah
// 13 - blah blah
// 5 - blah blah blah
// 4 - blah blah blah blah
if ((typec!="20") && (typec!="13") && (typec!="5") && (typec!="4"))
Admin
"JavaScript" is a proprietary and trademarked language belonging to Netscape Communications Corporation, and includes not only the core language but the browser object bindings for the Navigator browser (and the Netscape Server, when server-side JavaScript was allowed). "JScript" is a syntactically similar language implemented by Microsoft, but with a legally distinct name for distinctly legal reasons. ECMAScript is an international language standard based on the core JavaScript language; it includes no object bindings other than the core language objects. (The W3C defines the ECMAScript object bindings for HTML 4.x/XHTML and XML in general in their Recomendations.)
Admin
There is a much better implementation:
}
else {
if (typec=="13") {
}
else {
if (typec=="5") {
}
else {
if (typec=="4") {
}
else {
SelectType("ALLOC");
return;
}
}
}
}
It is much cleaner and easier to read. Plus, you can much more easily add code to do something if typec is 20, 13, 5, or 4.
Admin
You just made my day. =D Google before post plz.
Thank Sun for that, they trademarked JavaScript(tm), and apparently no one could come up with a better alternative than the ECMA's name.
Some nights I lie awake and wonder what Sun's engineers and designers were smoking when they thought that up. Does object-oriented paint thinner exist?
Admin
My bad, I thought it was Sun.
I wonder how JScript, a mediocre name at best, has died out while ECMAScript, the ugliest language name that doesn't start with X I know of, has become so popular. There must be a lot of standard-whores. The rest of the world just sticks to javascript or javascript1.2. =p
Admin
It was Sun's originally (Oak/Live Oak renames), but was sold (probably for an obscene amount of money). I mean, the code is just "out there", no proprietary runtime required or nuthin'. How else are you supposed to make money off of something like that?