IsTrue@O'Reilly
-
2.5 Literals
Boolean Literals
The boolean data type in JavaScript represents a "truth value"--i.e., whether something is true or false. Any kind of comparison operation in JavaScript yields a boolean value that specifies whether the comparison succeeded or failed. Since there are two possible truth values, there are two boolean literals: the keywords true or false. These literals are commonly used in JavaScript code like the following:
while(done != true) {
...
if ((a == true) || (b == false) || (i > 10)) done = true;
}
(From the JavaScript: The Definitive Guide)
-
Translation:
while(!done) {
...
if (a || !b || (i > 10)) done = true;
}
-
or even better
while(a || !b || (i>10))
{...}
-
WTF?! Better? That's not at all better!
For a start, the original would enter the loop at least once, no matter
what values of a, b and i. It's a do...while construct, and
replacing it directly with a while construct is a mistranslation.
Plus you got the condition inverted, because that was the condition for
exiting the loop, and you've made it the condition for continuing the
loop. You need to invert the sense of a and b, turn the
logical-ORs into logical-ANDs, and replace the GT with LE to get the
same condition.
Plus this whole item makes no sense. Where's the WTF? It's
a code sample from a textbook, of course it's not optimised, it's a
contrived case to illustrate the point. Are you seriously telling
me that if /you/ were writing the textbook, the chapter about boolean
literals would read
" Since there are two possible truth values, there are
two boolean literals: the keywords true or
false. These literals are commonly used in
JavaScript code like the following:
while (a || !b || (i < 10)) { ... }"
because you'd optimised away all mention of 'true' and 'false'?
It wouldn't really be a chapter on boolean literals then, would it?
Also in any real-world system, the compiler would generate the /exact/
same output regardless of whether you write "if (a == true)" or "if (a
!= false)" or just "if (a)", so it's not really an optimisation - it
doesn't affect the generated code, it just saves a couple of bytes in
the source. It wouldn't be fair of me to say it's obfuscated if
you do write it the shorthand way, but writing it the longhand way is
perfectly clear, and only takes a few bytes in the source, and has no
effect on the compiled code.
Complaining about the code examples in this textbook not having
replaced "if (a==true)" with "if (a)" makes about as much sense as
complaining that those three dots between the opening brace and the
if-clause would cause a syntax error.
-
@mallard said:
Translation:
while(!done) {
...
if (a || !b || (i > 10)) done = true;
}
while(!done) {
...
done = (a || !b || (i > 10));
}
-
@your mom said:
@mallard said:
Translation:
while(!done) {
...
if (a || !b || (i > 10)) done = true;
}
while(!done) {
...
done = (a || !b || (i > 10));
}do {
...
} while (!(a || !b || (i > 10)))
or, (another twist!)
do {
...
} while (!a && b && (i<=10));
all of this, assuming that "done" was originaly false before entering the loop [:P]
the ultimate version would be
if(!done) {
do {
...
} while (!a && b && (i<=10));
}
Cheers!,
Guido
-
You have some extra brackets and curlies there. And I'm pretty sure you don't need all that wasteful whitespace in those expressions - saving a few bytes is more important than being readable, right?
-
"Complaining about the code examples in this textbook not having replaced "if (a==true)" with "if (a)" makes about as much sense as complaining that those three dots between the opening brace and the if-clause would cause a syntax error."
True, although I suppose it would be a small WTF if they didn't mention that you can abbreviate if (a == true) to if (a).
-
@Iago said:
You have some extra brackets and curlies there. And I'm pretty sure you don't need all that wasteful whitespace in those expressions - saving a few bytes is more important than being readable, right?
Blame the buggy-almost-WTF'ed HTML Editor [:P]
(Sorry for that, I didn't have the preview feature nor could I edit the post).Cheers,
Guido