Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You'd exaggerate the problem at hand too if you were pushing some strange variant of the language criticized. Any programmer experienced with C-style languages recognizes logical constructs with && and || immediately.

If

  function (x) {
    x = x || someDefaultValue;
for setting non-falsy default argument values isn't a JavaScript idiom, I don't know what is.


But that's not the code I was criticizing. In fact, it's precisely because that's the idiom I associate with boolean operators in JavaScript that the code snippet I posted struck me as unintuitive.

As to "pushing some strange variant": Guilty as charged. Have you tried CoffeeScript? It's fantastic. The code you gave above would be written as

    (x) ->
      x ||= someDefaultValue


> Have you tried CoffeeScript?

Nope. I'm apprehensive about adding another layer to the development process when I know JS very well and don't find it at all limiting. So I'm probably not part of your target audience.


I was skeptical at first too, and I wouldn't recommend it to anyone who doesn't already have a pretty good understanding of JavaScript. But I'd urge you to try it for a week or so. It's not a layer of abstraction like GWT; it's just a nice, succinct syntax that compiles statement-by-statement to JavaScript.


i much prefer x || (x = someDefaultValue) because it doesn't require backtracking. by that i mean, when i read x = x || somedefaultValue, i am thinking x = "okay, we are in an assignment) and then i see x || and then i have to adjust and think "oh, well, this is a conditional so now i have to think about that situation."

with x || (x = someDefaultValue), what you read is how it is evaluated. if x is true, the rhs is never evaluated. with the above, the assignment happens either way.




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

Search: