There are lots of different ways to move "if" statements in CoffeeScript into multiple lines. Pick the one you like the best!
Creating if statements that span multiple lines in CoffeeScript can be hard to remember, but it's quite easy. The trick is to make sure you
Each of these examples will work, but they aren't necessarily pretty.
Use a backslash to continue the line:
if a == 1 && b == 1 \
|| c == 1 && d == 1
doSomething()
Use an "or" operator at the end of a line:
if a == 1 && b == 1 ||
c == 1 && d == 1
doSomething()
Use parentheses (and an "or" operator):
if(
a == 1 && b == 1 ||
c == 1 && d == 1
)
doSomething()
Let's do a bit of refactoring to see how we can make these a little cleaner.
Although it may seem odd, the indentation in CoffeeScript is looking for two spaces for it subsequent line. So if you use more than two, it will work as a continuation of the previous line. For example:
if a == 1 && b == 1 ||
c == 1 && d == 1
doSomething()
If that's hard to read, you can include parentheses.
if(a == 1 && b == 1 ||
c == 1 && d == 1)
doSomething()
Or, you could break up the logic into multiple statements.
e = a == 1 && b == 1
f = c == 1 && d == 1
if e && f
doSomething()
Note: I prefer symbols for operators, such as &&
vs. and
as I actually find it easier to read, even though it's less semantic. You can substitute my &&
for and
and ||
for or
and achieve the same effect.
References: