Comment On Bulletproofed Boolean

"Some time ago I was checking the business logic that a friend had done for a system." writes Brian, "While I was debugging, I found this awesome piece of code. I understand that application logic should be bulletproofed to handle any kind of data condition passed to it, nulls, double and single quotes, etc., but I felt this to be an example of over-engineering a solution." [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Bulletproofed Boolean

2010-07-14 09:03 • by //Rumen (unregistered)
Wow.... why does this happen so often?

Re: Bulletproofed Boolean

2010-07-14 09:04 • by frits
That piece code is awesome!


Addendum (2010-07-14 09:58):
I see Mark B. fixed his typo. Oh well.

Re: Bulletproofed Boolean

2010-07-14 09:05 • by Severity One
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.

Reminds me of some code I saw once, which compared whether a certain integer was greater than 4 and less than 6.

Re: Bulletproofed Boolean

2010-07-14 09:12 • by Matt Westwood (unregistered)
314395 in reply to 314391
Severity One:
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.

Reminds me of some code I saw once, which compared whether a certain integer was greater than 4 and less than 6.


if (value == false) {
return true;
} else {
return false;
}

is shorter and doesn't include !

Re: Bulletproofed Boolean

2010-07-14 09:14 • by frits
314397 in reply to 314395
Matt Westwood:
Severity One:
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.

Reminds me of some code I saw once, which compared whether a certain integer was greater than 4 and less than 6.


if (value == false) {
return true;
} else {
return false;
}

is shorter and doesn't include !


return (value == false);

Re: Bulletproofed Boolean

2010-07-14 09:17 • by bryan986
314398 in reply to 314397
frits:
Matt Westwood:
Severity One:
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.

Reminds me of some code I saw once, which compared whether a certain integer was greater than 4 and less than 6.


if (value == false) {
return true;
} else {
return false;
}

is shorter and doesn't include !


return (value == false);




return !value;

Re: Bulletproofed Boolean

2010-07-14 09:17 • by Anonymous (unregistered)
It pains me to think that people like this are out there coding right now. Probably adding a few helper methods like IsIntEqualToOne(int i) and IsStringEqualToFudge(string possiblyFudge).

Re: Bulletproofed Boolean

2010-07-14 09:18 • by Pigeon
314401 in reply to 314395
Big bad boolean blunder built by belligerent baffoon.

Alliteration FTW

Re: Bulletproofed Boolean

2010-07-14 09:19 • by prionic6 (unregistered)
At least
response
was initialized.

Re: Bulletproofed Boolean

2010-07-14 09:20 • by Skilldrick (unregistered)
314403 in reply to 314400
Anonymous:
It pains me to think that people like this are out there coding right now. Probably adding a few helper methods like IsIntEqualToOne(int i) and IsStringEqualToFudge(string possiblyFudge).


At least they're using descriptive names and parameters. Even better: int2(int x) {return x == 1;}

Re: Bulletproofed Boolean

2010-07-14 09:21 • by srejv (unregistered)
public boolean isBooleanFalse(boolean value) {
boolean response = true;
if (value == true) {
response = false;
}
return response;
}

Re: Bulletproofed Boolean

2010-07-14 09:22 • by frits
314405 in reply to 314398
bryan986:
frits:
Matt Westwood:
Severity One:
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.

Reminds me of some code I saw once, which compared whether a certain integer was greater than 4 and less than 6.


if (value == false) {
return true;
} else {
return false;
}

is shorter and doesn't include !


return (value == false);






return !value;




Reading is fundamental.

Re: Bulletproofed Boolean

2010-07-14 09:32 • by ais523
314406 in reply to 314403
Skilldrick:
At least they're using descriptive names and parameters. Even better: int2(int x) {return x == 1;}

Better still would be int int86(int x) {return x == 86;}, just for the horrific breakage when ported to DOS (which probably wouldn't be noticed immediately).

Re: Bulletproofed Boolean

2010-07-14 09:33 • by Jukin (unregistered)
314407 in reply to 314397
frits:
Matt Westwood:
Severity One:
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.

Reminds me of some code I saw once, which compared whether a certain integer was greater than 4 and less than 6.


if (value == false) {
return true;
} else {
return false;
}

is shorter and doesn't include !


return (value == false);



return value ^^ true;

Re: Bulletproofed Boolean

2010-07-14 09:36 • by endre (unregistered)
we needed this, i stole it

Re: Bulletproofed Boolean

2010-07-14 09:37 • by THG (unregistered)
Where is its counterpart, isBooleanTrue() ?!?

(Do Code-Reviews, and you would catch these omissions)

Also, maybe somebody else there has already written isBooleanFalse(), and Brian's friend is reinventing the wheel.
__
/hg

Re: Bulletproofed Boolean

2010-07-14 09:37 • by Murf (unregistered)
Yes bulletproofed. As long as no-one passes a null of course.

Re: Bulletproofed Boolean

2010-07-14 09:39 • by Kim (unregistered)
314411 in reply to 314395
return value ^ true

is also shorter and doesn't contain !

Re: Bulletproofed Boolean

2010-07-14 09:42 • by nobulate
I can imagine calls to that function might look something like this:

if isBooleanFalse(foo) == true {

// foo is false, so do not do what we would have done.
};

if isBooleanFalse(foo) == false {
// foo is not false, or true, reindex the nasa mainframe.
};

Re: Bulletproofed Boolean

2010-07-14 09:44 • by TheRider
314413 in reply to 314410
Murf:
Yes bulletproofed. As long as no-one passes a null of course.
Language comprehension is fundamental.

To me, this code looks like Java. And Java distinguishes between primitive types and objects. boolean (lowercase) is a primitive type, where Boolean (uppercase) is an object. Objects can be null, primitives cannot. The parameter is declared boolean (the primitive type), therefore no null value possible there.

Re: Bulletproofed Boolean

2010-07-14 09:49 • by Murf (unregistered)
314414 in reply to 314413
Obviously you're not an auto-boxer.

Re: Bulletproofed Boolean

2010-07-14 09:50 • by Daniel Fountain (unregistered)
So - if that checks if a boolean is false, and returns a boolean - should i use this function to check if this function returns a false boolean?


Its a never ending coding loop!!!!!!!!!!!!!

Dan

www.3xg.co.uk

Re: Bulletproofed Boolean

2010-07-14 09:50 • by charles (unregistered)
314416 in reply to 314407
Jukin:
frits:
Matt Westwood:
Severity One:
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.

Reminds me of some code I saw once, which compared whether a certain integer was greater than 4 and less than 6.


if (value == false) {
return true;
} else {
return false;
}

is shorter and doesn't include !


return (value == false);



return value ^^ true;


return value.toString().toWoodenTable().toString().equals(Boolean.FALSE.toString().toWoodenTable().toString()) == Boolean.TRUE.toString().toWoodenTable().toString();

Re: Bulletproofed Boolean

2010-07-14 09:50 • by SuperJames74
I call bullshit! There's no friggin' *way* that someone smart enough to land a job could possibly be that fucked up.

Re: Bulletproofed Boolean

2010-07-14 09:51 • by Knux2 (unregistered)
Weak. We can do better.

public boolean isBooleanFalse(boolean value) {
log.info("Entered isBooleanFalse");
boolean response = false;
if (value == null) {
log.debug("value is NULL");
response = false;
} else if (value == true) {
log.debug("value is true");
response = false;
} else {
log.debug("value is not true or null");
response = true;
}
log.debug("response is " + response);
log.info("Leaving isBooleanFalse");
return response;
}

Re: Bulletproofed Boolean

2010-07-14 09:53 • by tragomaskhalos (unregistered)
314419 in reply to 314417
SuperJames74:
I call bullshit! There's no friggin' *way* that someone smart enough to land a job could possibly be that fucked up.
You must be new to theDailyWTF my friend !

Re: Bulletproofed Boolean

2010-07-14 09:53 • by George Fitch (unregistered)
Obviously, the correct way of doing this would be to replace this function with a call to a web service that submits the boolean value to a MS SQL Server 2008 via stored procedure. The database would then return the correct value by means of a VarBinary(Max) value of a JPEG image of a scanned Polaroid photo of the word 'True' or 'False' written on different colored index cards photographed on a wooden table.

... or just not use the function in the first place.

Re: Bulletproofed Boolean

2010-07-14 09:55 • by Anonymouse (unregistered)
Defensive programming.

Re: Bulletproofed Boolean

2010-07-14 09:56 • by Burpy (unregistered)
314422 in reply to 314417
My senior developer coworker once asked me what I meant when I was refering to "boolean"... and I've already seen this kind of crap in his code (but as he's a C# developer he uses "bool" and never asked himself where does this 4 letters word comes from.)

Re: Bulletproofed Boolean

2010-07-14 09:58 • by Jaime
314423 in reply to 314389
//Rumen:
Wow.... why does this happen so often?
My guess is that somebody is having an internal conflict between strongly-typed code and dynamic code, and they aren't smart enough to realize it. This code looks like an attempt to coerce anything to a boolean (poorly).

It's funny to watch people go back and forth between "strong typing seems to help the compiler find my errors" and "strong typing requires me to do these annoying casts". The result is often pseudo dynamic code in a strong typed language.

These people need to read an article in a programming journal at least once per decade. Or maybe even go to school.

Re: Bulletproofed Boolean

2010-07-14 09:58 • by ell0bo (unregistered)
Did you by chance arrange it so that his genetic material no longer has any change of propagating through the gene pool?

Re: Bulletproofed Boolean

2010-07-14 10:00 • by monkeyPushButton (unregistered)
314425 in reply to 314419
tragomaskhalos:
SuperJames74:
I call bullshit! There's no friggin' *way* that someone smart enough to land a job could possibly be that fucked up.
You must be new to theDailyWTF my friend !
That's right SuperJames, no one that stupid could get a job in development.
And santa is watching you.
And the toothfairy brought you that shiny new quarter.
And scruffy is playing happily on a farm in the country.

Re: Bulletproofed Boolean

2010-07-14 10:05 • by b0b g0ats3 (unregistered)
34th!!!!!!!!!11!!11!!!##!@##@ELEVEN

Re: Bulletproofed Boolean

2010-07-14 10:06 • by frits
314427 in reply to 314417
SuperJames74:
I call boolshit! There's no friggin' *way* that someone smart enough to land a job could possibly be that fucked up.


FTFY

Re: Bulletproofed Boolean

2010-07-14 10:06 • by CodeCotopus
I love this site...

Re: Bulletproofed Boolean

2010-07-14 10:07 • by Anonymous (unregistered)
314429 in reply to 314417
SuperJames74:
I call bullshit! There's no friggin' *way* that someone smart enough to land a job could possibly be that fucked up.

Ahh, a newcomer. Welcome my friend, pull up a seat and prepare to lose any preconceptions you have of professionalism in the IT industry.

Re: Bulletproofed Boolean

2010-07-14 10:24 • by Antony Koch (unregistered)
return !(false & !!!x)

Re: Bulletproofed Boolean

2010-07-14 10:25 • by Antony Koch (unregistered)
314431 in reply to 314430
oops!

return !(false & !!!value)

Re: Bulletproofed Boolean

2010-07-14 10:29 • by Anon (unregistered)
There you go, much better (and more complete):

public boolean isBooleanFalse(boolean value) {
return !isBooleanTrue(value);
}

public boolean isBooleanTrue(boolean value) {
return !isBooleanFalse(value);
}

Re: Bulletproofed Boolean

2010-07-14 10:32 • by JohnMcG (unregistered)
The great thing about this is that it can be re-used to write isBooleanTrue...


public boolean isBooleanTrue(boolean value)
{
return isBooleanFalse(isBooleanFalse(value));
}


Or, to keep the style consistent


public boolean isBooleanTrue(boolean value)
{
boolean response = true;
if (isBooleanFalse(value)) {
response = false;
} else {
response = true;
}
return response;
}



Re: Bulletproofed Boolean

2010-07-14 10:32 • by Some Wonk (unregistered)
314434 in reply to 314408
endre:
we needed this, i stole it


No, no, no. The proper response is "Plz send teh codez"

Re: Bulletproofed Boolean

2010-07-14 10:33 • by Professor Shitston Piss Phd. (unregistered)
314435 in reply to 314418
Knux2:
Weak. We can do better.
...crap...


You forgot to use IoC


public boolean isBooleanFalse(boolean value) {
log.info("Entered isBooleanFalse");
boolean response = IoC.Resolve<IBoolean>("FalseBoolean");
if (value == null) {
log.debug("value is NULL");
response = IoC.Resolve<IBoolean>("FalseBoolean");
} else if (value == true) {
log.debug("value is true");
response = IoC.Resolve<IBoolean>("FalseBoolean");
} else {
log.debug("value is not true or null");
response = IoC.Resolve<IBoolean>("TrueBoolean");
}
log.debug("response is " + response);
log.info("Leaving isBooleanFalse");
return response;
}

Re: Bulletproofed Boolean

2010-07-14 10:37 • by CaptainOblivious
314436 in reply to 314391
Severity One:
It's not easy to make this code shorter and not use an exclamation mark or question mark.
public boolean isBooleanFalse(boolean value) {

return value == false
}
Placing the code inline is even shorter, without ! or ?

Re: Bulletproofed Boolean

2010-07-14 10:39 • by Markp
314437 in reply to 314414
Murf:
Obviously you're not an auto-boxer.


And obviously you don't have the slightest clue how auto-boxing works. Well, auto unboxing since that's the only thing that would be affected here.

In Java, even if you have a Boolean that needs to be auto unboxed into a boolean, it will happen in client code before the call to isBooleanFalse. There is positively NOTHING the library function could do, or should do, to change that.

Re: Bulletproofed Boolean

2010-07-14 10:45 • by SR (unregistered)
314438 in reply to 314417
SuperJames74:
I call bullshit! There's no friggin' *way* that someone smart enough to land a job could possibly be that fucked up.


You're very lucky to work in such a competent team.

Re: Bulletproofed Boolean

2010-07-14 10:51 • by Jason Y (unregistered)
314439 in reply to 314402
That depends on the language. If it requires initialization before use, then initializing it to a potentially wrong value is _bad_. If it does not require initializing, then initializing to a potentially right valid value is OK (though initializing to an invalid value like null is better, if you have the option).

Anyway, this example code isn't over-engineering, and isn't making anything more bullet-proof. It's just a very verbose way to implement the not operator.

Re: Bulletproofed Boolean

2010-07-14 10:54 • by Jason Y (unregistered)
314440 in reply to 314418
Now _that_ is over-engineering!

captcha: jumentum - momentum initialized by a jump.

Re: Bulletproofed Boolean

2010-07-14 11:18 • by neminem (unregistered)
314443 in reply to 314429
Anonymous:
Ahh, a newcomer. Welcome my friend, pull up a seat and prepare to lose any preconceptions you have of professionalism in the IT industry.

Well, I'm a newcomer too. I just started by reading the entirety of the back archives, so that I can reference all your memes: it's not enterprisey enough - where's the xml?

See?

Re: Bulletproofed Boolean

2010-07-14 11:23 • by Uncle Al (unregistered)
314444 in reply to 314391
Severity One:
Maybe the friend doesn't like the exclamation mark in code? It's not easy to make this code shorter and not use an exclamation mark or question mark.


This is actually quite common in embedded systems where exclamation points and question marks have been removed to produce a reduced character set.

Re: Bulletproofed Boolean

2010-07-14 11:25 • by Anonymous (unregistered)
314445 in reply to 314443
neminem:
Anonymous:
Ahh, a newcomer. Welcome my friend, pull up a seat and prepare to lose any preconceptions you have of professionalism in the IT industry.

Well, I'm a newcomer too. I just started by reading the entirety of the back archives, so that I can reference all your memes: it's not enterprisey enough - where's the xml?

See?

Not bad but you need to add some embedded file systems, a bit of EVE Online, some Irish Girl and an honorable mention to Paula, Lyle and Rumen. Additionally, you must always remember that the true values of a boolean are TRUE, FALSE and FILE_NOT_FOUND.

There, I think that just about covers it.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment