TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Say What You Mean (in JavaScript)

17 点作者 TrevorBurnham大约 15 年前

7 条评论

jlongster大约 15 年前
Replacing "if" constructions with boolean operators is powerful. The simplistic example in the article doesn't really show the purpose of using || and &#38;&#38; to control flow.<p>It's scalable. What if you have to test a third expression? Surely this:<p>foo() &#38;&#38; bar() &#38;&#38; buzz()<p>is more readable than<p>if(foo()) if(bar()) buzz()<p>It's flexible. Since an "if" statement is not an expression in most languages, you have to be careful where you place it. For example, what if you wanted to pass the result to a function?<p>qux(foo() &#38;&#38; bar() &#38;&#38; buzz());<p>Javascript (and a few other languages) have the neat feature with boolean operators that they return the result of the last tested expression. So the above example would pass the result of buzz() to qux if all statements are true, otherwise it would pass false. Here is the same code with "if" statements:<p>var res = false;<p>if(foo()) if(bar()) res = buzz();<p>qux(res);<p>There are many more examples of why you should be used to constructing expressions with boolean operators. It doesn't matter much for the simplistic example in the article, but the same technique allows you do write more complex code in succinct ways.
评论 #1358988 未加载
评论 #1359017 未加载
jackfoxy大约 15 年前
While I endorse the argument for coding for readability (all things being equal), I don't agree with his example. I find the &#38;&#38; syntax in javascript completely readable.
评论 #1358852 未加载
评论 #1358944 未加载
评论 #1358919 未加载
评论 #1358862 未加载
iamwil大约 15 年前
I guess what you find readable also depends on where you're coming from. When I first saw that foo() &#38;&#38; bar() line in the OP, I knew what it meant immediately. It's because I use it myself, and I've seen raganwald's andand operator and heard of Haskell's Maybe monad.<p>I've used it for so long that it's come to mean "if the first exists, then do the second", and not just "this expression is true if both are true", and I don't really confuse the two.
评论 #1359030 未加载
评论 #1359010 未加载
chime大约 15 年前
I prefer this:<p><pre><code> if(foo()) bar(); </code></pre> No {} necessary if it's just a single command.
评论 #1359044 未加载
评论 #1358920 未加载
bsaunder大约 15 年前
I think sometimes what's "readable" to one person isn't to another. The more you hit a coding convention, the more readable it becomes (as with anything). So noted. Move along. There are bigger fish to fry.
评论 #1359066 未加载
tlrobinson大约 15 年前
&#38;&#38; and || are commonly used as "short circuit" operators in JavaScript. I have no problem with this.
edw519大约 15 年前
The old rule of thumb was, "Online, go for readability. Batch, go for speed." This almost always translates into, "Online, use 'if' or 'case'. Batch, use Boolean."<p>In the first case, maintenance programmers will thank you. In the second, users processing millions of records will thank you.