2016년 9월 26일 월요일

guard 랑 if는 같습니다.

Swift에서 guard랑 if는 같습니다. guard는 else가 무조건 붙고 else에 doSomething 을 하는 것 빼고요.

guard reading은 쉬우니 polyglot programmer의 장점을 발휘하여 if 문으로 대체하여 구현하면 됩니다. 그러나 왜 신생언어에 condition 구문이 추가 되었는지, 또 이름은 하필 guard 인지 알아 볼 필요가 있습니다.


References to a versioned API must always be guarded with the appropriate
availability checks.
A guarded operation can never be executed before its
guard.

guard는 gurad 뜻 그대로 인 것 같습니다. 지키는 것이죠. null pointer Exception이나 resource leak, overrun, 잘못된 배열 index(outbound, negative return에서 오는 unbound) 등 에서 지켜주던 if 문... 이제 guard에게 나누어 주어야 겠습니다. 


#### Completeness of Guard Conditions
Guard conditions can `continue` (mimicking the current use of `where`), `break`, `return`, or otherwise exit scope. This offers more flexible and complete behavior.
```swift
for x in sequence {
    guard condition else { continue } // current where behavior
    guard condition else { break } 
    guard condition else { return } 
    guard condition else { throw error } 
    guard condition else { fatalError() } // etc.
}
```
Removing where from for-in loops reduces cognitive burden when interpreting intent. The logic is easier to read and follow. And the Swift grammar is simpler. 

continue, break, return 이야 if에서도 쓰던 거니, if else 로 쓸 것 같습니다. error 처리는 guard에게 주려고 합니다. JAVA에서 assert 쓰는 느낌을 가져야 겠네요.

문득 궁금해져서 소스 코드를 뒤져 봤습니다. Swift main source에서 if testing source보다 guard testing source가 너무 비루하여 여전히 if 로 구현하는게 맞겠다는 생각도 듭니다.

폴리글랏 프로그래밍에서 가장 중요한 것은 자신이 writing한 언어와 reading 할 언어를 구분하는 것인데 swift를 주(主) 언어로 하였다면 정적 분석 상 error 발생을 막기 위해 적는 condition 파트에는 if 대신 guard else 를 쓰는 것이 좋아 보입니다. -> 폴리글랏 프로그래밍에서 가장 중요한 것은 자신이 writing할 언어와 reading 할 언어를 구분하는 것 입니다. 그렇게 해야 더 많은 언어를 섭렵할 수 있습니다. swift를 주(主) 언어로 하였다면 guard를 쓰는 편이 좋겠습니다. 정적 분석 상 error 발생을 막기 위해 적는 condition 파트에는 if 대신 guard else 를 쓰는 것이 좋아 보입니다.


사족입니다. 칼새과로 동물 중 가장 빠른 170Km의 속도를 내는(치타는 120) SWIFT 의 매력에 빠지고 싶은데 일은 SWIFT로 하는게 하나도 없네요. 사실 주황색에 새 모양이 너무 좋습니다. (특이하게도 최강의 언어는 C 로 생각합니다.)

큰 산, 키 큰 나무들 세상에서 가장 높은 줄 알았는데,
작은 새 한마리 허공을 날아
산을 넘네.

댓글 없음:

댓글 쓰기

국정원의 댓글 공작을 지탄합니다.

UPBIT is a South Korean company, and people died of suicide cause of coin investment.

 UPBIT is a South Korean company, and people died of suicide cause of coin. The company helps the people who control the market price manipu...