Comment On CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

"I found this line of code while doing some cleanup," Adam Aldrich writes, "this has to be the worst way to set a boolean variable to false." [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:24 • by serguey123
First?

And no, this is not spam, you soulcrushing scrip!

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:25 • by Mythran (unregistered)
This comment is very important. DO NOT TOUCH!

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:26 • by NotFirst (unregistered)
bool memberHasFirstComment = bool.Parse(bool.FalseString);

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:31 • by Power Troll
The colon is very important. DO NOT TOUCH.


That's what she said.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:31 • by Wormlore (unregistered)
That is not the worst way to set a boolean to False.
Something like this would have been "less good"

bool memberHasFingerprint = !bool.Parse(bool.TrueString);

And I'm sure someone creative enough could come up with something worst yet.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:31 • by Rictor (unregistered)
Seems like this programmer had a bit of a potty mouth...

Private Function FnPtrToLong(ByVal lngFnPtr As Integer) As Integer

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:32 • by Bobbo (unregistered)
The Article:
# The colon is very important. DO NOT TOUCH.


throw new AccessViolationException("Ouch, that's cold");

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:33 • by DOA
unless custId is 1521.
The one constant in every software development department.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:34 • by frits
The Article:

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"

public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}


.

Really? Replace them when you see them? You would think there would be an automated way to "find and replace" such "atrocities" with such a useful "fix". Nevermind the fact that this fix is as least as dumb as the original. At least.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:35 • by trwtf (unregistered)

for (var i:int = 0; i < 100; i++){
var rand:int = Math.random() * 6;
}
SoundHandler.playExternalSounds(["positive_override" + rand]);



The loop makes it more random, you see...

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:37 • by tim (unregistered)
Actually I remember doing something like the 'pointer to long' in fortran on VMS once

IIRC fortran didn't have pointers but it was possible to call C functions like malloc. however, the only way to get the return value from malloc into an int was to hide it inside an additional layer of function call because fortran is less funny about type coercions across function boundaries

but of course if that example wasn't VMS fortran then it is a WTF ;-)

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:40 • by XX (unregistered)
332491 in reply to 332483
A good developer never forgets about FileNotFound.

bool memberHasFingerprint;
try {
memberHasFingerprint = bool.Parse(FileNotFoundString);
catch (FormatException) {
memberHasFingerprint = !bool.Parse(bool.TrueString);
}

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:41 • by tim (unregistered)
332492 in reply to 332483
Wormlore:
That is not the worst way to set a boolean to False.
Something like this would have been "less good"

bool memberHasFingerprint = !bool.Parse(bool.TrueString);

And I'm sure someone creative enough could come up with something worst yet.


oh go on then

bool memberHasFingerprint = (bool.Parse(bool.TrueString) == !bool.Parse(bool.TrueString))

(i can feel the slippery slope coming on)

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:41 • by luis.espinal
Aaron's code comment feel deja vu to me from one project I endured many moons ago... like man, it does speak to me, like, literally!

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:42 • by boog

/*
* See, this is why I hate my job. We start out doing some magical end-all solution for a may be customer, the
* "XML Engine" hazily devised by IT iliterate decision makers without a clue what it's should do or who wants it. Ignore
* that the codebase as is needs refactoring and fixing, let's ignore that we have a todo list a mile long, let's make
* up requirements on top of that! Then, when reality kicks in and the may be customer isn't, we have semi-finished
* shit clogging up all code arteries with poison. But noooo, don't clean it up, let's hack something else together
* like this cover control thing, blah blah boohoo blah...
*/

Sounds like this crap should be in his journal. Or if you check there maybe you'll find useful comments about the code?

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:48 • by Gary (unregistered)
332495 in reply to 332489
trwtf:

for (var i:int = 0; i < Math.random() * 100; i++){
var rand:int = Math.random() * 6;
}
SoundHandler.playExternalSounds(["positive_override" + rand]);



The loop makes it more random, you see...


FTFY. Even more random now.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:49 • by ShatteredArm
if (oneHundred == 100) {

oneHundred /= 2;
}


Looks like oneHundred can be anything except 100.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:49 • by digitalwitch (unregistered)
The Article:

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"


public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}


There is something to be said for readability. Just because you can do something in one line doesn't mean you should.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:51 • by Anything you can do, I can do worse (unregistered)
bool memberHasFingerprint = true; // set fingerprint presence to false...

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:53 • by Honest Guy (unregistered)
332499 in reply to 332487
The Article:

public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}

public static bool IsRealString(object s)
{
return s // removed crap I don't understand, for readability
}

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:53 • by Jellineck (unregistered)
332500 in reply to 332497
digitalwitch:
The Article:

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"


public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}


There is something to be said for readability. Just because you can do something in one line doesn't mean you should.


He just learned the coalesce operator and he wants to use it everywhere.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:54 • by Rand Dumm (unregistered)
332501 in reply to 332489
trwtf:

for (var i:int = 0; i < 100; i++){
var rand:int = Math.random() * 6;
}
SoundHandler.playExternalSounds(["positive_override" + rand]);

The loop makes it more random, you see...
Shouldn't you loop a random number of times, so you don't get the same result every time you run it?

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:56 • by Anon (unregistered)
332502 in reply to 332487
The Article:

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"

public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}



Why test if an empty string is empty (if s is null)? Seem like inlining the code from the original functions would be better. Which is probably what the compiler does anyway. And it's more readable. Once again the suggested fix is TRWTF.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:58 • by Dazed (unregistered)
Well, I'm all in favour of comments which explain why code was written in a particular way. Aaron's predecessor's comment isn't quite what I usually have in mind, but at least it heads off all the folks around here who cry "what the hell were they thinking?"

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 11:59 • by Bosshog (unregistered)

if (oneHundred == 100) {
oneHundred /= 2;
}

In Soviet Russia, Integer divides *you*.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:01 • by Bobrot
332505 in reply to 332486
DOA:
unless custId is 1521.
The one constant in every software development department.


I remember that one. French guy had to see the site in English. (some sort of language preference was just too easy)

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:05 • by Ikkonoishi (unregistered)
332506 in reply to 332492
tim:

oh go on then

bool memberHasFingerprint = (bool.Parse(bool.TrueString) == !bool.Parse(bool.TrueString))

(i can feel the slippery slope coming on)

bool memberHasFingerprint;
WebClient wc = new WebClient();
string[] fs = wc.DownloadString("http://msdn.microsoft.com/en-us/library/system.boolean.truestring.aspx").Split("\"",3000);
foreach (string fss in fs)
{
if (s == bool.TrueString) memberHasFingerprint = !bool.Parse(fss);
}

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:07 • by kastein (unregistered)
332507 in reply to 332481
NotFirst:
bool memberHasFirstComment = bool.Parse(bool.FalseString);


bool memberHasFirstComment = bool.Parse(bool.FalseString);

users.NotFirst.comment = bool.Parse(memberHasFirstComment);

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:09 • by Patrek (unregistered)
332508 in reply to 332483
Got that in code I had to maintain (another language, but still):
boolean isVerified = temp.equalsIgnoreCase("yes") ? new Boolean(true).booleanValue() : new Boolean(false).booleanValue(); 

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:10 • by Timmorn (unregistered)
332509 in reply to 332499
Honest Guy:

public static bool IsRealString(object s)

{
return s; // removed crap I don't understand, for readability
// The colon is very important. DO NOT TOUCH.
}


Fixed it for you

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:13 • by RBiter (unregistered)
332510 in reply to 332486
.. whew! cuz I thought it was from someone in my place. I certainly recognize that number!
;

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:16 • by Pyrexkidd (unregistered)
332511 in reply to 332483
Wormlore:
That is not the worst way to set a boolean to False.
Something like this would have been "less good"

bool memberHasFingerprint = !bool.Parse(bool.TrueString);

And I'm sure someone creative enough could come up with something worst yet.




unless ($five_thousand != m//){
open $FHIN, '<', "list_of_bool_values.file";
foreach(<$FHIN>){
&return_false;
if ($five_thousand != $five_thousand){
$five_thousand = "false";
} else {
$five_thousand = "true";
}
$five_thousand ? s/true//g : s/false//g;

}

sub return_false($){
#So it MIGHT look like
#this sub doesn't do anything;
#well, actually you're right
# but that's ok, because I checked it
#in and our of source control for every line.
return;
}

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:17 • by Ancient Greek Guy (unregistered)

if(person.age>18 || person.gender == female)
{
ewww();
}
else
{
touchColon();
}

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:18 • by ContraCorners
But, FnPtrToLong neither accepts nor a returns a long. Both are integers, no?

Or was that the WTF?

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:39 • by Anon (unregistered)
332514 in reply to 332512
Ancient Greek Guy:

if(person.age>18 || person.gender == female)
{
ewww();
}
else
{
touchColon();
}


Priest function?

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:39 • by Bob (unregistered)
"[D]evised by IT iliterate[sic] decision makers without a clue what it's[sic] should do or who wants it."

Was he being ironic?

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 12:50 • by forgottenlord (unregistered)

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"

public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}


Ok, I've never seen this operator before. Yes, it is a flash of ignorance on my part, though a quick check through the code base of my company's code (and there have been a fair number of C# developers here) I can't find a single instance of it being used. Why, we just didn't, collectively, know it. Mind you, most of us got our degrees while programming in Java or C/C++ so maybe it is just that it wasn't part of those languages. Either way, it's not really a WTF. It's a failure of knowledge rather than technique.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:07 • by the beholder (unregistered)
332518 in reply to 332487
frits:
The Article:

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"

public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}


Nevermind the fact that this fix is as least as dumb as the original. At least.
See, our store is having this great sale on sarcasm detectors...

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:14 • by NOT an ancient Greek guy (unregistered)
332519 in reply to 332514
Anon:
Ancient Greek Guy:

if(person.age>18 || person.gender == female)
{
ewww();
}
else
{
touchColon();
}


Priest function?


Nah, he's just an ancient Greek. It's cool. They touch the colons of boys all the time.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:18 • by Andrew (unregistered)
332521 in reply to 332513
ContraCorners:
But, FnPtrToLong neither accepts nor a returns a long. Both are integers, no?

Or was that the WTF?

It looks like RealBasic to me. RB allows mixing and matching of numeric datatypes through implicit coercion so that you can pass a 64 bit signed number (Long) to a function that accepts 32 bit signed numbers (Integer), return an Integer and treat it like a Long.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:22 • by frits
332522 in reply to 332518
the beholder:
frits:
The Article:

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"

public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}


Nevermind the fact that this fix is as least as dumb as the original. At least.
See, our store is having this great sale on sarcasm detectors...


Yeah I don't think so. Sarcasm involves intentional irony and humor. Show me those two elements.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:43 • by Mordred (unregistered)
What we are looking at here folks are samples from the pioneers of coding. If it weren't for these idiots, why we wouldn't have the super idiots we have today.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:44 • by Coyne
Actually, it turns out that the longer version of the IsNullOrEmpty() test that Anthony Arnold reported was needed in earlier versions of .NET. See:

DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

So which is the bigger WTF here: Writing it this way? Or having to write it this way because your compiler doesn't optimize right?

P.S. ...and, to top it all off, Anthony might be doing the wrong thing. Let's hope his compiler works right.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:47 • by Anon (unregistered)
332525 in reply to 332516
forgottenlord:

"This is used all over our app," writes Anthony Arnold, "I get rid of them when I see them. What's wrong with !String.IsNullOrEmpty((s ?? "").ToString())"

public static bool IsRealString(object s)
{
return s != null && !String.IsNullOrEmpty(s.ToString());
}


Ok, I've never seen this operator before. Yes, it is a flash of ignorance on my part, though a quick check through the code base of my company's code (and there have been a fair number of C# developers here) I can't find a single instance of it being used. Why, we just didn't, collectively, know it. Mind you, most of us got our degrees while programming in Java or C/C++ so maybe it is just that it wasn't part of those languages. Either way, it's not really a WTF. It's a failure of knowledge rather than technique.


I don't think it existed before the introduction of nullable types in C#. I wasn't familiar with it either having start with .NET 1.0.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 13:56 • by frits
332526 in reply to 332524
Coyne:
Actually, it turns out that the longer version of the IsNullOrEmpty() test that Anthony Arnold reported was needed in earlier versions of .NET. See:

DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

So which is the bigger WTF here: Writing it this way? Or having to write it this way because your compiler doesn't optimize right?

P.S. ...and, to top it all off, Anthony might be doing the wrong thing. Let's hope his compiler works right.


Anthony's method is functionally equivalent as far as null checking goes. TRWTF is having a method called "IsRealString" that checks an object instead of a string. In other words, the call to .ToString() is the fly in the ointment here.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 14:01 • by boog
332527 in reply to 332522
frits:
Yeah I don't think so. Sarcasm involves intentional irony and humor. Show me those two elements.

Not an easy task. Sure, one can show humor simply by laughing (ha), and it's clear that the "fix" was ironically as bad or worse than the original (you've admitted that yourself). However, the challenge lies in proving that these traits were intentional. Certainly the reader inferring the submitter's intentions would be subjective, and therefore worthless. What we need is some form of empirical evidence proving that the submitter intended to be ironic and humorous, or that said irony/humor was purely accidental. This may take a while.

Oh, wait. This is a forum on the internet, so no one cares.

My mistake.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 14:03 • by ÃÆâ€â„ (unregistered)
What, so you guys don't touch your colons? Boy, are you missing out.

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 14:12 • by Two (unregistered)
332529 in reply to 332486
DOA:
unless custId is 1521.
The one constant in every software development department.


oracleDbPortNum = 1521

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 14:15 • by yetihehe (unregistered)
<?php include sitename(__COMMENT__).'/../../../omgponies.com/public_html/comment_view.php'; ?>

on the other hand, they could do it like this:
eval(file_get_contents('othersite'));

Re: CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

2010-12-20 14:15 • by Two (unregistered)
332531 in reply to 332496
ShatteredArm:
if (oneHundred == 100) {

oneHundred /= 2;
}


Looks like oneHundred can be anything except 100.


Unless it starts out at 200 ...
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment