-
Notifications
You must be signed in to change notification settings - Fork 0
숫자 야구 게임 [SangBeom-Hahn] #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- stream()을 사용하기 위해 String이었던 랜덤 수를 List로 변환 - 테스트를 위해 private을 public으로 변환
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.
필요한 부분
Optimize Import생활화- 매직넘버 및 코드 가독성에 대한 고민을 가지는 시간
- 파일 끝 EOF 신경써서 처리
- 기본적인 접근 제한자 개념
| ### 요구사항 내용 | ||
| 1. 컴퓨터 | ||
| - 1 ~ 9까지 서로 다른 임의의 수 3개를 선택한다. | ||
| - 사용자가 입력한 숫자에 대한 결과를 출력한다. |
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.
일상적인 관점에서는 컴퓨터가 결과를 출력하지만 이 게임상에서는 컴퓨터 & 사용자를 Player로 보고 결과를 도출하는 것은 다른 컴포넌트에게 위임하는게 좋지 않을까요?
| public void terminateGame(User user) { | ||
| if (isTerminate()) { | ||
| user.terminate(); | ||
| System.out.println("완전 정료"); |
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.
이런 출력은 요구사항에 없는데 임의로 넣으신건가요
| } | ||
| public boolean isTerminate() { |
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.
개행
| validateNotStringRestartStatus(restartStatus); | ||
|
|
||
|
|
||
| if(restartStatus.equals("2") ){ |
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.
2줄을 개행한 이유가 있나요
그리고 똑같은 검증을 2번하고 있네요
| public void validateRangeRestartStatus(final String restartStatus) { | ||
| if (Integer.parseInt(restartStatus) < 1 || Integer.parseInt(restartStatus) > 2) { | ||
| throw new IllegalArgumentException("재시작은 1, 완전 종료는 2 입니다."); | ||
| } | ||
| } | ||
|
|
||
| public void validateNotStringRestartStatus(final String restartStatus) { | ||
| if ( !(restartStatus != null && restartStatus.matches("[-+]?\\d*\\.?\\d+")) ) { | ||
| throw new IllegalArgumentException("재시작은 1, 완전 종료는 2인 정수입니다."); | ||
| } | ||
| } | ||
|
|
||
| public void validateNotDoubleRestartStatus(final String restartStatus) { | ||
| if (!restartStatus.chars().allMatch(Character::isDigit)) { | ||
| throw new IllegalArgumentException("재시작은 1, 완전 종료는 2인 정수로 소수를 입력할 수 없습니다."); | ||
| } | ||
| } | ||
|
|
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.
validateRangeRestartStatus를 가장 먼저 검증하고 있는데
- Int가 아닌 값
- 숫자가 아닌 값
- 중간에 Space Separator 존재
이 경우는 어떻게 대응하실건가요?
그리고 restart에 필요한 검증을 크게보면
- 사용자가 숫자를 입력했냐
- 그 숫자가 1, 2중에 하나인가
이렇게 나올거같은데 위의 3가지 검증은 포커스가 어긋난거 같은데 어떻게 생각하시나요?
- 정수냐 소수냐 -> 따로 정수 판별에 대한 boolean method로 판별하면 될거같은데
| public boolean hasDuplicateDigitInRandomNumber(Integer digit) { | ||
| if (randomNumber.contains(digit)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } |
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.
마찬가지로 1줄로 줄일 수 있네요
| public List<Integer> getRandomNumber() { | ||
| return randomNumber; | ||
| } |
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.
일반적으로 getter/setter/equals와 같은 메소드들은 클래스 가장 하단에 위치하는게 관례입니다
- 외부에서 변경할 이유가 없다면 mutable -> immutable
| public void validateNumber(final List<Integer> number) { | ||
| if (number.size() > 3 || number.size() < 3) { | ||
| throw new IllegalArgumentException("입력 숫자는 3자리입니다."); | ||
| } | ||
|
|
||
| if (!number.stream().allMatch(digit -> digit >= 1 && digit <= 9)) { | ||
| throw new IllegalArgumentException("입력 숫자는 3자리입니다."); | ||
| } | ||
| } |
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 인자로 stream을 한줄로 배치하셨는데 가독성이 너무 안좋지 않을까요?
차라리 메소드로 빼서 가독성을 좋게 하는게 나을거같네요
Exception Message만 보면 동일한 메시지를 넘기는데 2가지 검증의 차이가 드러나게 표현해주세요
| import java.util.List; | ||
|
|
||
| public class Computer { | ||
| public List<Integer> randomNumber; |
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.
이 필드는 왜 public으로 열어둔거죠?
|
|
||
| // then | ||
| assertThat(randomNumber.stream().allMatch(digit -> digit >= 111 && digit <= 999)); | ||
| System.out.println(randomNumber); |
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.
테스트 코드에서 sout으로 출력을 하는 이유가 있나요
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class ComputerTest { |
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.
각각의 테스트가 무엇을 위한 테스트인지는 알겠는데 private scope로 두어야 할 메소드들도 테스트를 위해서 public으로 열어둔 것처럼 보이네요
클래스의 모든 메소드를 테스트하기 위해서 public으로 열어두기 보다 private scope로 두어야할 메소드는 private scope로 두고 해당 메소드를 활용하는 의미있는 public method를 테스트하면 되지 않을까요?
| User user = new User(); | ||
|
|
||
| // when | ||
| ArrayList<Integer> number = new ArrayList<>(Arrays.asList(1, 2)); |
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.
타입을 List로 하지않고 ArrayList로 둔 이유가 있나요?
추가적으로 new ArrayList<>(Arrays.asList(1, 2))로 구현한 이유가 있나요? 그냥 Arrays.asList(1, 2)로 해도 되지않아요?
- 그리고
Arrays.asList와 List.of의 차이도 뭔지 알고계시나요?
| @Test | ||
| @DisplayName("RestartStatus 테스트") | ||
| void UserTest() { | ||
| // given | ||
| User user = new User(); | ||
|
|
||
| // when | ||
| user.terminate(); | ||
|
|
||
| // then | ||
| assertThat(user.restartStatus).isEqualTo(User.RestartStatus.RESTART); | ||
| } |
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.
이 테스트 깨지는데요
기본적으로 PR을 생성할때는 구현한 로직에 대해서 로컬에서 모든 테스트가 통과하는지 확인하고 올려주세요
- 종료 신호 사용자 입력 메서드 분리 - BaseballControllerTest 코드 terminateTest 메서드에서 console 입력 부분 String 변수 초기화로 대체
- 테스트를 위해 컴퓨터의 생성자에 save 메서드 주석처리
- controller의 playGame 메서드에 주석은 resultTest 출력을 보기 위한 것으로 이렇게 해도 되는지 질문하고 싶습니다.
- user의 RestartStatus에 재시작 여부를 입력하던 terminateGame 메서드를 삭제 - gameOver 메서드에서 terminateGame 메서드 제거 - 게임 재시작시 컴퓨터의 랜덤수가 동일한 문제 해결
|
일단은 제 방식대로 리팩토링했고 커멘트에 대한 답변과 리팩토링을 할 예정입니다. |
Uh oh!
There was an error while loading. Please reload this page.