-
Notifications
You must be signed in to change notification settings - Fork 42
[배민지] Sprint4 #146
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
The head ref may contain hidden characters: "Basic-\uBC30\uBBFC\uC9C0-sprint4"
[배민지] Sprint4 #146
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수고하셨습니다~!
주요 리뷰 포인트
- 유지보수를 고려한 개발
- 함수 네이밍
- 모듈화
".form__password--wrapper .input-icon" | ||
); | ||
|
||
function checkLoginFormValidity() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 함수는 로그인 버튼의 enabled 상태를 업데이트하는 역할이라 updateButtonState 정도가 적당할것같네요!
그리고 더 개선해보자면 이메일, 비밀번호뿐만 아니라 다른 요소가 폼 필드에 추가될때 해당 함수를 상당부분 변경해야할텐데 좀 더 확장성을 고려해 구조를 바꿔보면 어떨까요?
- 정해진 유효성 검사 규칙대로 요소에 대한 유효성 검사를 수행
- 버튼 UI를 업데이트
이렇게 함수를 분리해볼까요?
const emailValue = e.target.value.trim(); | ||
|
||
if (emailValue === "") { | ||
inputEmail.style.border = "1px solid red"; | ||
emailErrorMessage.style.display = "block"; | ||
emailInvalidMessage.style.display = "none"; | ||
} else if ( | ||
!/^[A-Za-z0-9_\.\-]+@[A-Za-z0-9\-]+\.[A-Za-z0-9\-]+$/.test(emailValue) | ||
) { | ||
inputEmail.style.border = "1px solid red"; | ||
emailErrorMessage.style.display = "none"; | ||
emailInvalidMessage.style.display = "block"; | ||
} else { | ||
inputEmail.style.border = "1px solid var(--gray-100)"; | ||
emailErrorMessage.style.display = "none"; | ||
emailInvalidMessage.style.display = "none"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 내용이 여러곳에서 중복될 가능성이 커보이고, if 조건문을 여러가지 사용해 코드가 직관적으로 읽히지않는 단점이 있어요.
이렇게 따로 이벤트 리스너를 붙이지않고 아래 예시처럼 관련 요소에 대한 유효성 검사 결과를 알아내고 단순히 UI를 업데이트하는식으로 바꿔보는건 어떨까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 요소별 유효성 검사 수행
export const validators = {
email: (value) => {
if (!value) return { isValid: false, message: "이메일을 입력해주세요" };
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
return { isValid: false, message: "올바른 이메일 형식이 아닙니다" };
}
return { isValid: true, message: "" };
},
password: (value) => {
if (!value) return { isValid: false, message: "비밀번호를 입력해주세요" };
if (value.length < 8) {
return { isValid: false, message: "비밀번호는 8자 이상이어야 합니다" };
}
return { isValid: true, message: "" };
},
};
- 유효성 검사 결과에 따른 UI 업데이트
if (result.isValid) {
clearError(inputEl);
} else {
showError(inputEl, result.message);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 구조를 바꿔보면 이런 장점이 생기겠죠?
- 각 함수가 { isValid, message } 형태로 일관된 형태로 결과를 반환해 코드를 좀 더 예측이 쉽고 안정적인 흐름을 가질수있도록 만들수있습니다.
- 객체를 통한 명확한 매핑을 사용해 조건문을 사용하는것보다 훨씬 코드의 의도를 명확히 드러낼 수 있습니다.
- 각 함수가 독립적이어서 개별적인 동작을 테스트하기쉽고, 수정 및 확장에 용이합니다.
- 재사용 가능한 로직을 쉽게 분리할 수 있습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 파일에서도 login.js와 겹치는 로직들의 경우 코드를 중복해서 사용하는것보다는 모듈화해서 사용하면 훨씬 코드가 간결해보일수있겠죠? 다음 미션에서 나오겠지만 미리 알아두는것도 좋을것같아요!
이 아티클 참고해보세요 :)
요구사항
기본
로그인
회원가입
심화
멘토에게
아직 미션 3때 리뷰주신부분들은 수정하지 않았습니다
미션 3에 미션4만 추가해서 올려드립니다!
하였습니다