- 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
Not random enough!
for (var i:int = Math.random(); i < ((Math.random()+Math.random()+Math.random()+Math.random()+Math.random())* (Math.random()+Math.random()+Math.random()+Math.random()+Math.random()* (Math.random()+Math.random()+Math.random()+Math.random()+Math.random()* (Math.random()+Math.random()+Math.random()+Math.random()+Math.random()); i += Math.random()*Math.random()*Math.random()){
var rand:int = Math.random() * 6;
}
SoundHandler.playExternalSounds(["positive_override" + rand]);
Or perhaps I misread the code and the numbers 0 and 100 are generated by fair dice roll.
Admin
I first read it as
for (var i:int = 0; i < 100; i++){ var rand:int = Math.random() * 6; SoundHandler.playExternalSounds(["positive_override" + rand]); }
which would have been much funnier
Admin
This one is scattered through one of our product's code base.
bool someVar = thisVar ? false : thisVar;
Admin
Interesting...
Admin
I think a certain dev heard "You should leave comments in the code" and kind of misunderstood what was meant by "comment".
Admin
Admin
What language is number 2?
Admin
The colon is very important. DO NOT TOUCH!
Admin
I've touched your colon. Pray I don't...
Wait... Blech... Never mind.
Admin
Admin
More so. The function is poorly named, certainly, but the code in my workplace is riddled with similar calls. And for good reason - it's amazing how many developers can stuff up something as straightforward as:
In contrast, a call to isNotEmpty(string) is almost impossible to get wrong, and is actually somewhat easier to read at a glance. A fix for the deficiencies of the language, granted, but still useful.
Admin
Don't stop, Darth bluesman.
Captcha: transverbero - when you put a colon in front of a verb
Admin
Which Operator? ?? ?
Admin
Hmmm... It seems that folks are missing the WTFs here.
I don't recognize that specific language, so I can't say for sure, but in C/C++/C#/Java, the variable rand set inside the loop is out of scope when used in playExternalSounds(). Some other "rand" defined elsewhere is being used. So, not only are they trying to make it pointlessly "very random", a flaw is preventing it from doing anything at all.
Here, the code is actually not that bad, and the real WTF is that apparently no one on this forum can look at a fragment of code and figure out it's purpose.
Clearly, at some point they have an object, which may or may not be a string, and they have to determine if it is. String.IsNullOrEmpty() can't do that by itself, as it takes string, not objects. So, he calls ToString() to convert the object into string (if it's already a string, ToString is a NOP). But, if the object is null, ToString() will throw a NullReferenceException, so it added a separate null check at the start.
So, the only real problem is that ToString will always return a valid string, so this will only return false if the object is null.
So, the real correct version:
(for those tht don't know C#, "(s as string)" is an expression of type string, if s is a string; or null, if s is any other type.)
Admin
We should also point out that he is passing an Object not a String.
What is the result of passing 's' (without ToString()) to IsNullOrEmpty ?
What is the result of passing s.ToString() in the case where s is null?
I think what he was doing here is not quite as stupid as some people seem to think.....
<disclaimer> I am fairly new to c#, but I assume that passing a non-String object to a method that expects a String (IsNullOrEmpty) is a no-no, and that we can't do NULL.ToString();
Admin
I wonder how many times the humour/irony/sarcasm debate will return...
(I wouldn't have thouoght sarcasm needs necessarily to involve humour...)
Admin
For those who are interested, the VB function that does nothing (and is incorrectly declared to take and return Integer when it should take and return Long) is a (moderately) well-known workaround. If you ever need to get the address of a procedure, for example, to pass to a Windows API function, you can't do it directly. VB has an AddressOf operator, but it's only usable as a parameter to a procedure.
In other words, you can't do
but you CAN do this
assuming it were declared correctly.
So now you can make the tired "TRWTF is VB" comment and, in this case, be right.
Admin
I can actually understand the logic on the PHP one at the end. It's still very much a WTF, but here's my take on it:
Apparently he used the same script on 2 completely different websites, with different domains, but hosted on the same machine. It's not at all uncommon for a bunch of small sites to be on the same machine, using a fairly standard directory structure of something like /home/username/public_html/domain/subdirectory/scriptname.php.
In an effort to ensure updating the script on either would update both, he used some Directory Fu to go so far up on the directory tree that he left the site he's doing the include from entirely, and then down into the other domain to include the script from there. PHP won't reject this, because you haven't left the machine, and of course, it has no way of knowing that you're pointing it at a completely different site - there's no formal standard for how to structure a directory tree for websites, just some common sense in organizing it logically and intelligently.
So it'll work... and it'll do what the guy intended... but this is still colossally stupid, and for far more reasons than the clusterfuck of an include statement he ended up with. There's a good chance that other site will grow large enough to need its own machine, or that the sites will later change enough that the exact same script can no longer be used. When this happens years from now, with a completely different coder involved, the new guy is going to wonder why the site is suddenly throwing all kinds of errors, get stuck digging through the code, and then realize that not only is it trying to include a file that's no longer on that machine, but he doesn't have access to wherever that other site went, leaving a gaping hole in the site, and likely days of downtime as they explain why they need code from a completely unrelated site. Even worse, if they SOLD that other site, they're just plain shit out of luck, and are going to have to play the "guess everything that script did, and rebuild it" game.
So yeah, MAJOR WTF, but unlike the weird bool stuff, there was at least some semblance of logic involved.
Admin
Did anyone else notice the "(TM)" in the rant comment?
At least he is respectful of the copyright laws...
Admin
Awesome comment! +1 for you.
Admin
I love it. Reads a little like the obligatory bit in a 19th-century adventure novel where the hero's in some remote jungle and doesn't know if he'll ever make it out alive, but he leaves this elaborate letter explaining his predicament for whomever, if anyone, happens to find it.
Admin
Does this even compile? Variable scope? Unless "rand" already exists outside... and then the loop is really useless.
Admin
In that case, it would remain at 200.
Admin
Yes, this is most likely the intended purpose of IsRealString(), but you have changed the behavior slightly. If s is a non-string object that implements ToString(), the original IsRealString() may return true, whereas yours will always return false. Again, this is probably a bug in the original, but who can tell for sure?
Admin
I really feel that Alex, or perhaps Anthony, is trolling us. Becuase the suggested "fix" isn't better, and it doesn't even really do the same (it has the same end-result, but does it in a less efficient way). Also it's way less readable.
Why doesn't Anthony give us hints as to why his solution is supposed to be better, and then we'll talk again?
Admin
Could've sworn there was a way to tell if something was a String built into the language...
Generally, though, if you're needing to specify whether something is or isn't a class, something's Bad-Wrong with your design.
Admin
this shit Private Function FnPtrToLong.....
was from vb6, and converted do vbnet.
Private Function FnPtrToLong(lngFnPtr As Integer) As Long ' Given a function pointer as a Long, return a Long. ' Sure looks like this function isn't doing anything, ' and in reality, it's not. FnPtrToLong = lngFnPtr End Function
it is used to call a C Dll function, passing a pointer to vb function. it isnot useless, i has a purpose
public sub Main() Dim P as Long P = FnPtrToLong( AddressOf callback ) CallCFunctionPassFunctionPointer(P) end sub public sub callback(byref p as long) as long 'pointer to function, dah end sub
Admin
If the designer of the random number function was really bright, you may not have broken it totally, but surely it doesn't improve randomness.
This may be obvious to some people in here, but I guess not everybody in here realizes, that using a random number of itterations like that can actually reduce randomness.
Admin
This isn't that much of a WTF. It's a bit dangerous is all. Assuming that include is a common config file with e.g. database credentials which are shared between several sites, it would be better to put the file higher up in the directory structure, at or above the otherdomain.dom level, assuming they're actually separate domains. If this file exists in e.g. a subdomain (many hosts set up subdomains as if they were separate domains, so sub.otherdomain.com would be a directory at the same level as otherdomain.com) then pulling in a config file from the main directory is quite possibly the correct thing to do, so it can be changed once in the main domain and any subdomains will also pick up the change. Ideally he should also be using require or ideally require_once, because require will terminate the script if it couldn't pull in the file, where include will only throw a warning (which in a production environment could be suppressed).
Admin
Maybe a scaled-down illustration is in order. Pick a number. Any number. It can be a positive integer or something like "the fifth root of 1,627,803.4".
Spell that number out using the normal rules of spelling out numbers. Count the number of letters in the spelling, and use that number in the next step.
Spell that number normally. (By normally, I mean if you get sixteen letters, don't get cute and spell it as "the age I was when I lost my virginity"; just spell out the number like a civilized human being.) Count the number of letters in that spelling, and repeat until you get tired.
Your final answer is "four".
Admin
Seeing as you're already checking for a null object with "??" (and changing it to an empty string), why bother with string.IsNullOrEmpty?
Admin
Is this because "four" is the only fixed point (and number n tends to have < n letters), or is there a deeper thing at work?
Admin
It's because "four" is an attractor, but I'll only certify that in English. In some other languages you end up with things like endless cycles between the equivalents of "three" and "five".
A more complex sequence of numbers involves two operations. Beginning with any number n:
o If n is even, divide by two. o If n is odd, multiply by three and add one.
then repeat.
At present, every case that anyone has tried has eventually converged to n=1 (followed by n=4, n=2, and then repeat the cycle).
They've been trying to prove this since 1937, and so far all they've managed to do is establish that if there is a starting number that doesn't converge to this cycle, it's larger than 20×2^58. But it works for every number they've tried so far.
Admin
Admin
Agreed:
If s is null, execution will do the s != null bit, see the && symbol, and not even test the rest of the code, because the condition (s != null) is false. Why should you test something after an && operator when the first bit is false? You don't need to, and neither does the compiler/justintimeexecutor.
So this function: a) Won't throw an error. b) Is more readable.
TRWTF is the person who didn't understand why the function was there in the first place...
Admin
You'd think, huh? Either VB (.asp) or VB.NET (.aspx) will test the rest. (Not sure at the moment, but I know I've encountered that in one of those two situations.)
Admin
Not really, your way tries to execute the output of the php script, most of the time, HTML, that wouldn't work.
Admin
SOP in most companies, I thik you'll find.
"Aaargh! Customer 1521 won't eat his rice pudding!"
"So fix it so he doesn't get rice pudding."
"But - but - but we have no concept of not giving rice pudding! Everybody loves rice pudding! It'll take months to program an option in: "Do you want rice pudding?" and think of the DBA work needed ..."
"Bah. Put in a bodge for now. We'll address the Generalized Rice Pudding Option later. Oh by the way, when did you say your last day was?"
Admin
Admin
+1. Read the first chapter of Knuth's TAOCP Volume II for a truly awe-inspiring foul-up of a random number generator which by a bizarre series of coincidences isn't. Essential reading for anyone who wants to use random numbers in anger.
Admin
Try it in different languages. The same principle applies, except you'll probably end up on a different number (e.g. Russian: 3) may get into a loop (e.g. French: 5-4-6-3-5-4-6-3 ...) and it is possible you will end up with more than one attractor (e.g. if there were a language that had 4 = "fawr" and 5 = "fiyev" and 6 = "sickss" or whatever - there may a language with such a property but without investigating in detail I don't know).
Admin
I believe this method in its original form may be useful when working with untyped DataTables, while your "correct" version will break behaviour.
Admin
Sorry, Russian ends up in a loop that goes 4-6-5-4-6-5 except for 2 and 3 which end up as 3. Sorry, forgot that 7 has an extra silent letter at the end.
Admin
Why did you lose your virginity at the age of 16? Are you a 50 year old or something?
Admin
Bad idea to create a new string object, just for detecting an empty string -- better check the length of the original string:
Admin
No. My final answer is "five". Now if you'd said "...until you get a repeated number" you'd be right, but I get tired easily.
Admin
I'd like to use the square root of 2. How do I get to the point where I start counting letters?
Admin
Yo conseguí "cinco". それから私は"一"を得た。 κατόπιν πήρα πέντε πάλι. Et finalement je me suis coincé dans une boucle entre trois, cinq, quatre, et six…
Admin
Wait, what? If I do it I'll see smart comments? Are you insane?
Of course not. If you do it TDWTF's commenters will start to look bright by comparison.
Admin
You so totally didn't get what he was saying! He was talking about comments in source code, and not about TDWTF comments! Learn to read!