Coffeescript에서 긴 복합 if 문을 올바르게 형식화하는 방법
단순히 미적 목적으로 오버플로하고 싶지 않다는 복잡한 if 문이 있다면,이 경우 커피 스크립트가 반환을 진술의 본문으로 해석하므로이를 분해하는 가장 정결 한 방법은 무엇일까요?
if (foo is bar.data.stuff and foo isnt bar.data.otherstuff) or (not foo and not bar)
awesome sauce
else lame sauce
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
또는 줄이 and
또는 or
또는 is
또는 ==
또는 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
이로 인해 코드의 의미가 다소 변경되지만 유용 할 수 있습니다.
return lame sauce unless foo and bar
if foo is bar.data.stuff isnt bar.data.otherstuff
awesome sauce
else
lame sauce
CoffeeScript의 is...isnt
합법적 인 체인과 마찬가지로 합법적 인 체인에 유의하십시오 a < b < c
. 물론의 반복 lame sauce
은 불행하며 return
당장 원하지 않을 수도 있습니다. 또 다른 접근 방식은 담그기를 사용하여
data = bar?.data
if foo and foo is data?.stuff isnt data?.otherstuff
awesome sauce
else
lame sauce
은 if foo and
조금 세련이다; 기회가 없다면 당신은 그것을 폐기 할 수 foo
있다가 undefined
.
다른 언어와 마찬가지로 처음부터 사용하지 않음으로써. 다른 부분에 이름을 지정하고 별도로 취급하십시오. 술어를 선언하거나 몇 개의 부울 변수를 작성하십시오.
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
줄 바꿈을 이스케이프하는 것이 가장 읽기 쉬운 것 같습니다.
if (foo is bar.data.stuff and foo isnt bar.data.otherstuff) \
or (not foo and not bar)
awesome sauce
else lame sauce
낮은 수준의 상용구가 많이 발생하면 abstract 수준을 높여야합니다 .
최상의 솔루션은 다음과 같습니다.
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
'Nice programing' 카테고리의 다른 글
CMake target_include_directories 범위의 의미 (0) | 2020.11.08 |
---|---|
jQuery : mousemove 이벤트 중 눌린 마우스 버튼 감지 (0) | 2020.11.08 |
Node OAuth 서버 구현이 있습니까? (0) | 2020.11.08 |
ggplot2 플롯 영역 여백? (0) | 2020.11.08 |
"abstract : true"상태에 URL을 지정하는 이유는 무엇입니까? (0) | 2020.11.08 |