Hacker News new | ask | show | jobs
by gruseom 4837 days ago
You're right and that's a fair point, but the argument still applies. The OP takes it as obvious that his rewritten jQuery snippet is better. In fact it's far from obvious, and learning the sense in which it's far from obvious is (in my experience) an important milestone, after which one stops arguing naively about readability and starts thinking more about the intelligibility of whole programs. If composed ternaries are an idiom that jQuery employs to do certain things compactly and repeatedly, that convention may well make it easier to understand jQuery. And that is what matters—not helicoptering into arbitrary bits of code.

In terms of lines of code—an imperfect metric but a useful one—his rewritten example is twice as long. That may seem like a small price to pay for an easier-to-understand snippet in isolation but if you start thinking about a complex system as a whole, and consider how complexity compounds as programs grow, that intuition changes considerably.

(I'm done being patronizing now.)

1 comments

You make great points, and I think you're right. While I think the OP's advice is appropriate for most situations, particularly in general JavaScript code you have to share with other developers, jQuery's use of it is probably entirely appropriate. I skimmed the article, and by the end, I was already starting to grok how they were using the ternary at first glance - and, as others have pointed out, they may have more important considerations, such as execution speed, driving this usage.

Tangentially, I think his point about the descriptive variable names is far more important than avoiding if...else aliases (though it's not an original point, and the examples he shows are appropriate uses of short variable names).

I didn't think you were particularly patronizing. If you were, then I didn't mind.