Backend 백엔드
[02] Node.js 활용2 - early exit 방식으로 리팩토링하기
박민우_
2024. 2. 26. 18:09
핸드폰 자릿수 검증 후 토큰 만들어 전송하기
// if 문에 긍정을 쓰는 방법
function createTokenOfPhone(num){
if(num.length>=10){
if(num.length<=11){
let result = String(Math.trunc(Math.random() * 10 ** 6)).padStart(6,"0")
console.log(result);
// 핸드폰번호에 토큰 전송하기
console.log(`${num} 번호로 인증번호 ${result} 를 전송합니다`)
}else{
console.log("ERROR 발생 . 핸드폰 번호 확인해주세요")
}
}
console.log("ERROR 발생 . 핸드폰 번호 확인해주세요")
}
if 문에 긍정을 쓰면 모든 코드가 if문 안으로 들어가기 때문에 코드의 복잡성이 올라간다
if문에 부정을 쓰면 코드가 훨씬 깔끔해진다
early-exit 방식 적용
// early-exit 방식!
function createTokenOfPhone(num2){
if(num2.length < 10 || num2.length > 11){
console.log("ERROR 발생 . 핸드폰 번호 확인해주세요")
return;
}
let result = String(Math.trunc(Math.random() * 10 ** 6)).padStart(6,"0")
console.log(result);
// 핸드폰번호에 토큰 전송하기
console.log(`${num} 번호로 인증번호 ${result} 를 전송합니다`)
}
728x90