중첩 조건문을 Guard Clause 로 가독성을 높여보자
“In computer programming, a guard is a boolean expression that must evaluate to true if the program execution is to continue in the branch in question. Regardless of which programming language is used, guard code or a guard clause is a check of integrity preconditions used to avoid errors during execution.” — Wikipedia
if Guard {
...
}
if not Guard {
return ...
}
...
async def verify_token(email: str, token: str, purpose: str):
user = await user_service.get_user_by_email(email)
if user:
token = await user_service.get_token(user)
if token :
if token.purpose == 'reset':
return True
return False
async def verify_token(email: str, token: str, purpose: str):
user = await user_service.get_user_by_email(email)
if not user:
return False
token = await user_service.get_token(user)
if not token or token.purpose != 'reset':
return False
return True