News for hackers. Stuff that matters.
Re:Of parser-fetishists and semi-colons by ajross on Monday April 16, 2012 @03:30PM (#3848919)
The && operator short circuits. It evaluates its second operand only if the first is true (becuase if the first is false, then the expression result is known to be false). It thus acts pretty much exactly like "if", but with an infix syntax. This trick is used pervasively in shell programming, where the native if syntax sucks.
Re:Of parser-fetishists and semi-colons by radicalbyte on Monday April 16, 2012 @03:49PM (#3849015)
Thanks, I get it now :)

So the second operand can be a function / method, with any (or no) return type.

> function bob () { alert("bob") }; true && bob();

Alerts "bob".

Nice trick, though not something I'd personally ever us as it's not concise and isn't portable to C# (and I assume Java/C++).

Plus using the IF statement is a better showing of intent.. and I don't really like relying on job security through obscurity..

Re:Of parser-fetishists and semi-colons by ajross on Monday April 16, 2012 @04:22PM (#3849215)
It is concise. There are fewer tokens and bytes needed to write A && B than if ( A ) { B }. And the intent thing is, as I've tried to explain, very much situation-specific. There are many programmers out there very comfortable with this idiom. That it doesn't happen to be popular in the web development community doesn't mean it sucks or isn't worth learning.

Don't pass judgement on syntax you literally just learned, basically. Don't assume the world you know is the only one worth knowing. Good hackers know their tools.

Re:Of parser-fetishists and semi-colons by radicalbyte on Monday April 16, 2012 @05:41PM (#3849614)
I'm not judging it, I'm just saying that it's not something I'd use because it's confusing for my colleagues / anyone maintaining my work. Unless they're a pure Javascript person.

It is a neat trick though, and proof that I need to spend a little more time hacking in javascript to improve my knowledge :-)