如何在 Coffeescript 中正确格式化长复合 if 语句

发布于 2024-11-18 21:30:13 字数 240 浏览 0 评论 0原文

如果我有一个复杂的 if 语句,我不想​​仅仅出于美观目的而溢出,那么分解它的最合理的方法是什么,因为在这种情况下,coffeescript 会将 return 解释为语句的主体?

if (foo is bar.data.stuff and foo isnt bar.data.otherstuff) or (not foo and not bar)
  awesome sauce
else lame sauce

If I had a complex if statement that I did not want to overflow simply for aesthetic purposes, what would be the most kosher way to break it up since coffeescript will interpret returns as the body of the statement in this case?

if (foo is bar.data.stuff and foo isnt bar.data.otherstuff) or (not foo and not bar)
  awesome sauce
else lame sauce

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦屿孤独相伴 2024-11-25 21:30:13

如果该行以运算符结尾,CoffeeScript 不会将下一行解释为语句的主体,因此这是可以的:

# OK!
if a and
not 
b
  c()

它会编译为

if (a && !b) {
  c();
}

您的 if 可以格式化为

# OK!
if (foo is 
bar.data.stuff and 
foo isnt bar.data.otherstuff) or 
(not foo and not bar)
  awesome sauce
else lame sauce

或任何其他换行方案只要行以 andoris==not 或一些这样的运算

符至于缩进,您可以缩进非第一个只要正文更加缩进,您的 if 行就可以了:

# OK!
if (foo is 
  bar.data.stuff and 
  foo isnt bar.data.otherstuff) or 
  (not foo and not bar)
    awesome sauce
else lame sauce

您不能做的是:

# BAD
if (foo  #doesn't end on operator!
  is bar.data.stuff and 
  foo isnt bar.data.otherstuff) or 
  (not foo and not bar)
    awesome sauce
else lame sauce

CoffeeScript will not interpret the next line as the body of the statement if the line ends with an operator, so this is ok:

# OK!
if a and
not 
b
  c()

it compiles to

if (a && !b) {
  c();
}

so your if could be formatted as

# OK!
if (foo is 
bar.data.stuff and 
foo isnt bar.data.otherstuff) or 
(not foo and not bar)
  awesome sauce
else lame sauce

or any other line-breaking scheme so long as the lines end in and or or or is or == or not or some such operator

As to indentation, you can indent the non-first lines of your if so long as the body is even more indented:

# OK!
if (foo is 
  bar.data.stuff and 
  foo isnt bar.data.otherstuff) or 
  (not foo and not bar)
    awesome sauce
else lame sauce

What you cannot do is this:

# BAD
if (foo  #doesn't end on operator!
  is bar.data.stuff and 
  foo isnt bar.data.otherstuff) or 
  (not foo and not bar)
    awesome sauce
else lame sauce
ゝ杯具 2024-11-25 21:30:13

这会在一定程度上改变代码的含义,但可能会有一些用处:

return lame sauce unless foo and bar
if foo is bar.data.stuff isnt bar.data.otherstuff
  awesome sauce
else
  lame sauce

请注意 is...isnt 链,它是合法的,就像 a a b< c 在 CoffeeScript 中是合法的。当然,蹩脚酱的重复是不幸的,你可能不想立即返回。另一种方法是使用 soaks 来编写

data = bar?.data
if foo and foo is data?.stuff isnt data?.otherstuff
  awesome sauce
else
  lame sauce

if foo and 有点不优雅;如果 foo 不可能是 undefined,那么您可以丢弃它。

This changes your code's meaning somewhat, but may be of some use:

return lame sauce unless foo and bar
if foo is bar.data.stuff isnt bar.data.otherstuff
  awesome sauce
else
  lame sauce

Note the is...isnt chain, which is legit, just as a < b < c is legit in CoffeeScript. Of course, the repetition of lame sauce is unfortunate, and you may not want to return right away. Another approach would be to use soaks to write

data = bar?.data
if foo and foo is data?.stuff isnt data?.otherstuff
  awesome sauce
else
  lame sauce

The if foo and is a little inelegant; you could discard it if there's no chance that foo is undefined.

终陌 2024-11-25 21:30:13

就像任何其他语言一样,首先就没有它们。给不同的部分命名并分别对待它们。要么通过声明谓词,要么只是创建几个布尔变量。

bar.isBaz = -> @data.stuff != @data.otherstuff
bar.isAwsome = (foo) -> @isBaz() && @data.stuff == foo
if not bar? or bar.isAwesome foo
  awesome sauce
else lame sauce

Like any other language, by not having them in the first place. Give names to the different parts an treat them separately. Either by declaring predicates, or by just creating a couple of boolean vars.

bar.isBaz = -> @data.stuff != @data.otherstuff
bar.isAwsome = (foo) -> @isBaz() && @data.stuff == foo
if not bar? or bar.isAwesome foo
  awesome sauce
else lame sauce
儭儭莪哋寶赑 2024-11-25 21:30:13

对我来说,转义换行符看起来最具可读性:

if (foo is bar.data.stuff and foo isnt bar.data.otherstuff) \
or (not foo and not bar)
  awesome sauce
else lame sauce

Escaping the linebreak looks most readable to me:

if (foo is bar.data.stuff and foo isnt bar.data.otherstuff) \
or (not foo and not bar)
  awesome sauce
else lame sauce
夏末 2024-11-25 21:30:13

当出现大量低级样板文件时,您应该提高抽象级别

最好的解决方案是:

  • 使用好的命名变量和函数

  • if 中的逻辑规则/else 语句

逻辑规则之一是:

(不是 A 也不是 B)== 不是(A 或 B)

第一种方法。 变量:

isStuff              = foo is bar.data.stuff
isntOtherStuff       = foo isnt bar.data.otherstuff
isStuffNotOtherStuff = isStuff and isntOtherStuff
bothFalse            = not (foo or bar)
if isStuffNotOtherStuff or bothFalse
  awesome sauce
else lame sauce

这种方法的主要缺点是速度慢。如果我们使用 andor 运算符功能并用函数替换变量,我们将获得更好的性能:

  • C = A 和 B

如果 A 为 false 运算符and 不会调用

  • C = A 或 B

如果 A 为 true 运算符 or 不会调用

第二种方式。 功能:

isStuff              = -> foo is bar.data.stuff
isntOtherStuff       = -> foo isnt bar.data.otherstuff
isStuffNotOtherStuff = -> do isStuff and do isntOtherStuff
bothFalse            = -> not (foo or bar)
if do isStuffNotOtherStuff or do bothFalse
  awesome sauce
else lame sauce

When a lot of low-level boilerplate occurs you should increase level of abstract.

The best solutions are:

  • to use good named variables and functions

  • logic rules in if/else statements

One of logic rules is:

(not A and not B) == not (A or B)

The first way. Variables:

isStuff              = foo is bar.data.stuff
isntOtherStuff       = foo isnt bar.data.otherstuff
isStuffNotOtherStuff = isStuff and isntOtherStuff
bothFalse            = not (foo or bar)
if isStuffNotOtherStuff or bothFalse
  awesome sauce
else lame sauce

The main disadvantage of this method is its slowness. We will get better performance if we use and and or operators features and replace variables with functions:

  • C = A and B

If A is false operator and wouldnt call

  • C = A or B

If A is true operator or wouldnt call

The second way. Functions:

isStuff              = -> foo is bar.data.stuff
isntOtherStuff       = -> foo isnt bar.data.otherstuff
isStuffNotOtherStuff = -> do isStuff and do isntOtherStuff
bothFalse            = -> not (foo or bar)
if do isStuffNotOtherStuff or do bothFalse
  awesome sauce
else lame sauce
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文