- 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
if (comment == "Frist") { comment = "Frist"; }
Admin
Even in languages with truthiness, their check of
if(value == true)
may fail ifvalue
is something like number 1.Admin
In yesterdays WTF where they were generating JavaScript code from server-side code, this function would be extra useful because it works in both C# and JavaScript (and probably PHP and java as well for all I know)
Admin
It makes me cringe when I see a boolean variable compared to a boolean literal.
Admin
NOPE:
Admin
That could have some moderately surprising results in truthy languages as something that was true (or at least truthy) would become untrue after that.
Admin
If
isValid
is a nullable boolean, this is a (rather unclear) way of setting it to false if null. I'd rather doisValid ??= false
but there could be times in C# that the basic logic is requiredAdmin
They forgot to add this block between the
if
and theelse
:Admin
TRWTF is this employer using Lines of Code as a productivity metric.
Admin
In truthy languages, this will clear out what non-boolean value was in isValid for the simple fact, isValid will be converted to boolean before being compared to true because it used == instead of ===.
PHP has some oddness with string variables, in this case, IIRC. The string "0" is false! I think an array with a single 0 or other falsey value is false.
Admin
Javascript has some interesting behaviour with "0" as well - whilst boolean("0") is false, "0" == false is true
Admin
Greg’s coworker can’t handle truth. They’re pretty
!!paranoid
too.Admin
Useful if using something like NCrunch (or equivalent code coverage tool) as you can then see which tests hit which lines
Admin
Even in a truthy language, the easiest way to ensure strict boolness would be something like
Admin
In PHP,
value == true
is true when value is any truthy value. In Python and JavaScript, it succeeds when value is 1, but not other truthy values.Admin
I wanted to store this solution for future reference, but I couldn't find the file.
(also can't believe how long this meme/joke has been around.)
Admin
It's a solution to a race condition. Between the time the first line is evaluated and the if/else is started, some other process might have changed the value of 'value' . Now it gets fixed right properly
Admin
Typical case of "rewriting code would be refactoring and they don't pay me enough to do this because I never enter into the field because I liked it" syndrome :-)
Admin
This snippet is valid C and valid C++, and it might change the value of isValid if it is an integer. Depending on the compiler you might get a warning.
R3D3
I've read about this on the internet, but have never seen in production. Do people really do this? I feel an explicit cast is usually clearer.
Admin
Just drop the else branch. Validating someone who already has been is polite, but invalidating someone who already has been is bullying. Positive feedback or none at all.
Admin
Of course they do. It's valid syntax and plenty clear after you've seen it.
OTOH casting functions in JavaScript break if you plausibly, but mistakenly call them as constructors: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
Admin
isValid = isValid ? true : false;
Admin
isValid = isValid == true
Admin
My bet is it's a debug breakpoint trap. They needed a NOP inside the braces to set their breakpoint on, and those two NOPs were the ones they came up with. Since it's an assignment, it ought not be optimized out. OTOH, compilers are getting smarter all the time about what they'll do to your code. Then again, typically one uses a debugger with optimizations turned off.
IME it's truly amazing how many people don't understand conditional breakpoints.
Admin
The Real WTF is C# brace style.
Admin
@wtfguy: do you understand conditional break points? In languages I know (Java, Python, Javascript, etc. or even hardware debuggers like xilix ILA) they are emulated by actually evaluating the expression first and then deciding to break or not, which is equivelant to what that code those (minus the pointless assignments)
Admin
The underlying storage of a .NET Boolean is an integer, so it's theoretically possible to get a value other than true or false in there, especially if it's coming from something less strict than C#.