- 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
Du-uh. "The square root of two" -> "eighteen" -> "eight" -> "five" -> "four", you dummy.
Admin
"for (var i:int = 0; i < 100; i++){ var rand:int = Math.random() * 6; } SoundHandler.playExternalSounds(["positive_override" + rand]); "
I found the addition of the loop in this the be extremely exquisite. And by exquisite I mean painful...
Admin
And just like that, you destroyed another mysterious wonder of this world.
Admin
Here's one from my previous gig
boolean[] booleans = new boolean[] { Boolean.valueOf(Boolean.TRUE, Boolean.valueOf(Boolean.FALSE) };
I'm not making it up
Admin
Who cares what the compiler does? This source code is here for US to read. If you want to follow that to its logical conclusion, throw the programming language away, and write opcodes directly to disc. That's what the compiler does.....
Admin
Apart from the missed closing bracket, of course
Admin
The string check should be: !string.IsNullOrEmpty(s as string) Period. Anthony's version is TRWTF and I would probably replace it if I found something like that in our code.
I've seen few other developers use the ??-operator and you can certainly live a happy and fulfilled life without it (and especially if you don't use Linq). I mainly use it for instantiating backing fields that require parameters, like:
DelegateCommand _cmd;
public ICommand BlahCommand { get { return _cmd ?? (_cmd = new DelegateCommand(this.DoStuff)); } }
void DoStuff() { }
Admin
Way, way back some early fortran compilers had the common integers predefined as variables whose values were themselves
(that is, '5' was a variable whose value was 0x05 binary).
Once, I sabotaged a "friend"s coding coursework by slipping in a header file which contained:
LET 1 = 3
The compile swallowed it without a complaint. However it does make debugging almost impossible until you track it down...
Admin
Argh - you sadist! Happy birthday.
Admin
In Russian "not" is "не", it can be transliterated as "ne".
Admin
I think you completely missed my point. The point is that since the compiler will inline the function, there is NO POINT in doing it yourself. I was proposing that you leave it as it was (using the original function call - perhaps fixing the parameter type) instead of replacing it with some unreadable monstrosity.
Admin
If we actually want to test that any random object's ToString() override returns neither null nor an empty string, the original code is correct.
Admin
bool memberHasFingerprint = !bool.Parse(!((bool.TrueString == "True") || (bool.TrueString == "true") || (bool.TrueString == "TRUE") || (bool.TrueString == "T")) == true);
Like this?
captcha: persto - a quick pesto sauce
Admin
The name CompareObjectAsIAlertDocumentOrNullIfNotCastable was probably written by a frustrated, sarcastic programmer whose co-workers don't, and apparently cannot be made to, understand that the as operator yields null on conversion failure.
I'm not saying I would do this myself, but I sympathize. Our code has as-abuse scattered throughout.
The RTWF is C#'s as operator.
Admin
All objects implement ToString() because object (the base class of all objects in .NET) implements ToString() (the base implementation just returns the type name). The only exception would be if you deliberately overrode ToString() to throw a NotImplementedException or similar (which would be a little perverse). It's not clear if the original author really intended to use object as the parameter type. It's a little messy, but if your objects override ToString() to return something meaningful (for example, a Person object might return the Name member from ToString()), then checking if that string is empty might be a valid (but awkward) way to check if the object has a particular state (maybe ToString will return an empty string until the object is initialized). It would, of course, be better just to check the object directly with either an IsInitialized property of a member function.
Admin
You're absolutely right, I didn't think of the case where using ToString() is actually a requirement...
Admin
.NET (at least) allows to code the condition as "AndAlso" (Or "OrElse", for or) which tells the compiler to use boolean shortcuts.
That's right, you have to manually instruct the language to use the more efficient mechanism which is the default in every other language.
Man, I hope that position in C# comes through... ;-)
Admin
I could be mistaken though. Did you really not get that he was joking about the "fix" (to be fair though, neither Alex nor Anthony ever called it a "fix")?
Admin
For trolls: "The square root of two" -> "lot" -> "lot" ...
For more matematically endowed: "lot" -> "few" -> "few" ...
Admin
Consider this:
Given that you did not care to specify the language in which this was written, I would not know what the latter does, whereas the former is at least understandable by someone with a little common sense.
There are several cases when you do not really want to know how the code works, but just want to know what it does. That is precisely why your so-called better solution is wrong.
TRWTF is that this submission was published.
Remember this: Code is written once, but read multiple times. By majority rule, readability wins!
Admin
Actually, a long time ago, I've been using a random number generator that always returned 0 in the first invocation after seeding it. For that one, calling random() and ignoring the result would indeed make the final result more random. However, ignoring one random value would be enough, not 99.
Admin
Yes. Just because you can pile 100 operations into a single statement doesn't mean you should.
Not only that but the alternative approach creates a heap item if S is null. I think the original form is better.
s doesn't have to be a string, you can ToString() anything. This is a legitimate function--it returns true if s.ToString() actually returns text.
And one should override ToString for anything non-trivial. It's a godsend when debugging.
Admin
Moreover, this is "inlining by hand", which should really always be left to the compiler.
DRY - Don't Repeat Yourself!
What if the expression changes, i.e. you want to check for something a little bit different or add something to the check?
In the original, there is exactly one place to change and test it.
In Anthony's version, you have to do a global search and replace - and either be sure (or more likely hope), that all instances of this expression are written exactly in the same way, or use a regular expression for the search (of course provided that you IDE supports this and you know what a regular expression is AND get it right...)
Admin
Admin
Oh, I see the problem.
There, that should be more efficient.
Admin
In Alabama they'd have written:
if (aHunnert == 100) { aHunnert /= 2; }
Admin
TRWTF is that all references or pointers in C, C++, Java, .NET, etc. are potentially null.
Admin
That number of character thing will only feels "OMG-marvelous!" to a natively-few-alphabet-character-minded person. Because, say, in Chinese, the word for 0 1 2 3 4 5 6 7 8 9 10 are 1 characters. 11 - 19: 2 characters, 20 - 99: 3 characters. So it's like it just goes to one one one one one, and it feels more like "wtf of course that will happen because the word has the same meaning as the character count"
Admin
bool memberHasFingerprint = true;
is a very bad way of setting it to false.
Admin
Yes, there is: "Awesome!"
Admin
Yup, I think many of the WTF's we see here could have the whole comment appended somewhere in the code.
Admin
All alphabets have relatively few characters and use them to represent phonemes. You're perhaps thinking of logographies, which aren't alphabets.
Admin
Admin
:(Enter)#######
Admin
May way isn't as bad as that, but would be slightly more successful:
since the result could be hardware specific.Admin
Agreed about inlining it. Alternatively,
is equivalent without requiring any obscure C#-specific operators or calling a function unnecessarily to check the length of the empty string.
Admin
Though obviously not a very good location to put a file that's used by multiple domains, I think the "WTF" in this is not in PHP, but with the server-admin. If the above is actually possible, the security issues are not caused by PHP itself.
Admin
Gah, this crap happens far too often. IT doesn't move at the speed of business unless your idea of quality is "the programmers told me it was done." People at the top who don't get that seem to excel at forcing their teams to produce crap and utterly fail at understanding why it keeps happening.
The most disturbing two things about that comment rant, to me, are that at some point in the past I'm going to write an ill-conceived XML engine and, at some point in the future, I'm going to become a time traveler and not have the good sense to write down a winning lottery number to take with me.
Admin
Admin
public sub Main() CallCFunctionPassFunctionPointer(AddressOf callback) end sub
Admin
That should be
#define TRUE 0; #define FALSE 1;
bool memberHasFingerprint = TRUE; // set to false
Admin
I can most certainly do worse -- though, admittedly, my example is based on Java.
String falseString; ... falseString = "false"; ...
falseString = falseString.toUpperCase();
boolean memberHasFingerprint; boolean tempBoolean = falseString.parseBoolean();
if(falseString.getCharAt(0) != "A" && falseString.getCharAt(0) != "B" && falseString.getCharAt(0) != "C" && falseString.getCharAt(0) != "D" && falseString.getCharAt[0] != "E" && falseString.getCharAt(0) != "G" ...){ //make sure the first character isn't anything except F if(falseString.getCharAt[1] != "B" && falseString.getCharAt[1] != "C" && ...){ //make sure the second character isn't anything except A ...//do this sort of thing for L, S, and E -- getCharAt for 2-4 (edited here for readability) { memberHasFingerprint.parseBoolean(tempBoolean.toString()); }}}}}//end if
It can ALWAYS be worse!
Admin
Oh, and of course this isn't actually based on any real code I've seen. It's just something I came up with to be an ass.
I don't think anyone would be either so stupid or sadistic as to do something like that in production code.
Admin
The null coalesce operator has been part of the C# language since its first version. It has nothing to do with nullable types, it returns the leftmost non-null value and it exists in many other programming languages.
Admin
Admin
You can do better:
public static class ObjectExtensions { public static bool IsRealString(this object instance) { return !string.IsNullOrEmpty((instance ?? "").ToString()); } }
and use it like:
if (obj.IsRealString())
Admin
Nice, but we can do so much worse. (please forgive my pseudocode)
Admin
Which can be simplified to:
Which looks very familiar. (And yes, I think keeping this function as is makes far more sense than trying to inline it, particularly with the proposed monstrosity.)
Admin