Hacker News new | past | comments | ask | show | jobs | submit login

I think you missed the point.

This is not really about whether semicolons are required or not.

The point is that in order to ensure maintainability of code you should try to use language (and framework) in a way which ensures better maintainability, supportability, and portability of your code.

Look, the statement like "a && b" is 100% valid in many languages but in order to increase maintainability, supportability, and portability of your code it should be written like "if (a) { b; }".

The easiest way to understand the point of this rule is to get a job maintaing some old crappy code-base :) - I learned that way.




No no, I assure you that was the point I took away. And it seems you missed mine, which is that it's fine to make pronouncements like "don't use short circuit and as an infix if" for your own code. But flaming about them in public is rank pedantry. It's the kind of nonsense that the enterprise world has been dealing with for 20 years now: chasing the "maintainability rule of the week" is going to hurt you badly long term.

Learn to read and maintain the language you have, because you can't win this.


Okay, I'm going to bite.

&& is logical AND, and it operates on boolean expressions (or types).

So "a && b" evaluates to true if both a and b are true.

How can that be replaced by "if(a){b;}"?

Is it "r = a && b" being replaced by "if(a){r=b}"?

Using Chrome's javascript console:

> a = "asdas"; b="da"; a == b; false

> a = "asdas"; b="da"; if(a) { b }; "da"

Could you (or some other javascript expert) explain it to me?


In JavaScript, && does not evaluate to a boolean. It evaluates to the thing on the left if that thing is false (in which case the right hand side is not evaluated at all), otherwise to the thing on the right. So in said JavaScript console,

  "asdas" && "da"
evaluates to "da" and

  "" && "da"
evaluates to "".

So in an assignment context |r = a && b| would need to be replaced by: if (a) { r = a } else { r = b }, which may or may not be more confusing than the original statement with &&. But in a statement context, where the result of && is not being assigned to anything at all, "a && b" is exactly the same as "if (a) b;" except harder to understand.


Nitpick: you mean `if (!a) {r = a} else {r = b}` (or switched blocks).


Er, yes. Of course I do. ;)


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.


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..


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.


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 :-)


Going the other way: In languages where if is an expression

    if (a) { b; } 
would return b if a was true and nothing otherwise. Which is a similar to a && b.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: