- 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
whats wrong with that? This library of booleans manipulation should also have a not_false() function
Admin
But return !$val is so much less readable... :-)
the real wtf is the ugly need for '$'!
Admin
Hey give the coder a break. Maybe the 1 key on thier keyboard was broken!!!!!!!
Admin
Admin
While I won't knock the usefulness of this (think std::not1 in STL), but how this was done is asinine.
Admin
What about invert_bool(5) ?
Admin
<font size="2">Well, that's easy. It will obviously return File Not Found.</font>
Admin
function invert_bool($val) {
return $val ? false : true;
}
Admin
Ah, the joys of PHP.
You know if you had mentioned the lack of type checking, then he probably would end up with:
Admin
Actually, I think that Joshua had it right but at the same time I want to use a tertiary operator :P
function invert_bool($val) {
return ($val == true) ? !$val : !$val;
}
Admin
He's just future proofing his code incase the specifications for '!' change! He can simply change one 4 line function while all you suckers have to go through every single file finding-replacing exclamation marks.
Admin
Admin
== means "like". "$x == true" is always true if $x is boolean true or the number 1.
=== checks for type too.
switch(gettype($val))
{
case 'boolean': return $val == true ? true : false; break;
default:
return $val; // or return false, handle all types differently or what you want to do
}
yeah... bad php ^^.
dont accept anything with a different type or do something like:
function bla((boolean) $val)
than you end with the same thing as in every other language so whats the problem? ;)
Admin
Admin
Ah, this is very good error handling. The function returns nothing when the caller accidentally passes a $val that is not a bool. That way it's easy to tell when you've passed it bad data. Clearly the exclamation point has none of that kind of power.
Admin
WOW!!!!!!
[:P]
Admin
You're all missing the point... he's avoiding programmers double negative confusion syndrome. How many of you get flustered when your other half asks "aren't you going out?"
Admin
Then he could have written it shorter (1 line instead of 4!):
function invert_Bool($val) {
return $val?false:true;
}
Admin
Time for the inevitable "What I would do if I saw this in code at my company" jokes:
I wouldve given the coder a sheet of paper that said simply "You are !smart" to see how long it took him to catch on.
Admin
I really hope that people are joking when they suggest using a ternary to perform a negation. That's right up there with: return condition ? true : false instead of plain old return condition.
That being said, we can now implement isTrue as invert_bool(invert_bool(foo))
Admin
While there are simpler ways of doing it, I don't really think this is a WTF. The number rounding thing yesterday was a WTF. This is just some guy who isn't aware of the negation operator (although I suppose you could argue that in itself is a WTF). The code he wrote isn't going to blow up the space shuttle; it's just a few lines longer than it has to be. In my mind this is akin to using a simple if-else instead of the ternary operator.
Admin
And the corresponding "return_bool" function would be:
function return_bool($val) {
return !invert_bool($val);
}
Admin
holly php!
Admin
Maybe its a string variable from the Commodore PET days, circa 1978
Admin
If this is indeed PHP, then everything has a boolean value, so one of "== true" and "== false" will always return true.
Now, if he had written the function like this, then a non-boolean would get passed through with nothing changed (again, assuming this is PHP):
Of course, maybe that is the behavior he was trying to get. You can tell from the javadoc-style comment that his primary language is Java, and he wasn't used to a weakly-typed language like PHP. Maybe he was afraid that ! would return something weird for a non-boolean? Hard to say, but I think he was probably a Java programmer who was learning a new language when he wrote this.
Admin
I have to agree with some of the suggestions here that he is planning for the eventual change of the meaning of boolean. When it changes to true, false and unknown we will be scrambling to update all our code and this brillant fellow will only need to add another if statement, recompile and take a vacation.
Admin
You laugh, but a product I used to support had a boolean configuration variable that contained a double negative in its name. So, to turn this feature off, you'd set the variable to true. It was something like:
DontNotDoSomethingSomething = 1
We eventually had to ask the development team to change the name of the config variable, since this name prompted no end to the confusion. It was one of those things that we had to touch just often enough to know it was confusing the last time, but not remember what the solution was.
Admin
Admin
Maybe the guy hated Anthrax and got the heebies just thinking about using NOT!
Admin
What kind of comment it is!
desc: Returns the what?
WTF
Admin
Should be rewritten as:
return isFalse($val);
Admin
You're right, it is a WTF.
Admin
It would be better with even more:
function invert_bool($val){ if(!$val != !true) return !false; if(!$val != !false) return !true; }
Admin
Wasn't the $ after the variable name on the PET? Oh well, thanks for the flashback.
Admin
Actually, that'd be phpdoc. It's pretty standard and is based on javadoc. It's quite possible that he hasn't had any experience with Java.
Admin
Tell us all about it dinosaurs ;)
Admin
Cool, 11 more lines of code, and yet none of it is obviously useless. Plus it won't be hard at all to use this function to create even more lines of code, and yet none will be obviously redundant.
Instead of: if(!$foo) he can do: $invertedFoo = invert_bool($foo); if($nvertedFoo)
See, 1 extra line of code, and the "style checker" won't flag it as needless, and thus cut into my wages. $invertedFoo = !$foo; if($invertedFoo) as bad style.
Personally I'd rather be paid by number of bugs fixed than lines of code, but either way he is well on his way to that new Corvette.
Admin
The real wtf is a function to invert a bool in the first place.
Admin
I am glad to see someone taking the time to make sure the dreaded "Maybe" Bool value doesn't creep into our programs and databases. Me, I would have just done this:
if(x == true){
return true;
}else{
return false;
}
And think of the carnage that might have ensued.
Admin
I don't understand why there's so much checks and stuff.
The @desc part of the comment clearly states that the function should actually contain just this one line:
Admin
A useful feature of this code is that it always returns a boolean, even though its parameter may be a string, integer, or anything else. This provides a useful way to convert something to a boolean type:
$myvar = invert_bool(invert_bool($myvar));
Admin
Err, flip that: return false/return true. More wtfy than I first appreciated.
Admin
No references to Terry Pratchett yet?
"Five exclamation marks, the sure sign of an insane mind." etc.
One of my customers seems to know only two different punctuation marks: tripple+ exklamation marks and tripple+ question marks. His emails read like this:
Hello Mr. Kitzmueller !!!!
I have a question to you!!! Can we change xxx in program yyy????
etc.
Always a funny read.
Admin
I'd sure hope it is. I'd sure hope this "developer" would think that there's got to be a better way first time he did this:
if ($foo){
} else {
print $bar
}
Admin
Now for those loves of exclemation points:
Admin
Oh, look! Another hairless monkey we forgot to eat. This old age bit is wearing. At least we have our several hundred million year record to fall back on.
Sincerely,
Gene Wirchenko
Admin
You'll have to forgive us kids. We haven't been around long enough for the old age bit to start wearing.
Sincerely,
!dinosaur
Admin
String variables like A$ were common in all basic dialects of the home computer era, e.g. Commodore 64, Timex/Sinclair ZX81, Acorn BBC, TI 99/4A, MSX, CPC464 etc.
The opposite style, e.g. $A is a relict of the unix shell (can't say if it appeared even earlier) and it is definitely a WTF in languages like PHP and Perl, because the $ sign is meaningless there. In the unix shell,
$foobar
is a variable butfoobar
is a string (identically to"abc"
) but in PHP (I think also in Perl, correct if I am wrong), it has no such meaning.Admin
Ooops.
but
foobar
is a string (identically to"foobar"
) but ...Admin
I really don't see what all the fuss is about. There's nothing wrong with the code (yes, I know if you pass 1 or null or whatever into it, but lets assume he doesn't). The measely 6 lines of code replacing an operator isn't that bad... hell, it's better than brillant Paula.
It's not wrong, just a little pointless. But for the amount of effort that would've been put into that function, it doesn't really matter that much eh.
I think the real wtf is, "what is this doing here as a wtf?"