Conversation
- 랜덤으로 레이싱카를 전진 혹은 정지 시키는 기능 구현 - 레이싱카의 이름을 받는 생성저와 getter, setter 기능 구현
- 레이싱카 전진 및 정지 확인 - 레이싱카 유효하지 않은 이름 에러 발생 확인
- 주어진 횟수 round만큼 각 레이싱카를 무작위로 움직여 총 이동거리를 구하는 start 메소드 구현 - 주어진 레이싱 경기 후 총 이동 거리를 바탕으로 승자를 가리는 ranking 메소드 구현 - 각 메소드에 대한 테스트 코드 작성
| @Getter | ||
| public class RacingContest { | ||
| private static final Random random = new Random(); | ||
| private final RacingCar[] RACING_CARS; |
There was a problem hiding this comment.
어떤건 array를 쓰고 어떤건 List를 썼군요 가연님만의 기준이 있으신가요?
There was a problem hiding this comment.
전체 갯수를 이미 알고 있는 경우에는 array를 사용하고 레이싱 경기 우승자와 같이 미리 전체 갯수를 모를 땐 list를 사용했습니다. array, list 중에 무엇이 적절한 지 선택하기 위한 더 나은 기준이 있을까요?
| } | ||
|
|
||
| public int[] start(){ | ||
| int[] distances = new int[RACING_CARS.length]; |
There was a problem hiding this comment.
distance를 메서드 내부의 변수로 관리하셨군요.
만약 RacingCar 내부에 필드로 갖게된다면 어떨까요?
There was a problem hiding this comment.
생각하지 못했었는데 RacingCar 클래스의 필드로 사용하는 것이 테스트할 때 더 편할 것 같습니다!
|
|
||
| for (int r = 0; r < rounds; r++) { | ||
| for (int c = 0; c < RACING_CARS.length; c++) { | ||
| distances[c] += RACING_CARS[c].moveByRandom(random.nextInt(10)); |
There was a problem hiding this comment.
한줄에 너무 많은게 담겨 있는 거 같아요.
c번째 car가 움직이는 것을 랜덤값으로 뽑아서 움직인다.
하나의 줄에 여러개의 메서드가 포함되는 것은 가독성을 떨어뜨릴 수 있을 것 같아요. 어떻게 풀어볼 수 있을까요?
|
|
||
| public ArrayList<String> ranking(int[] distances){ | ||
| int maxDist = Arrays.stream(distances).max().getAsInt(); // winner decision | ||
| ArrayList<String> winners = new ArrayList<>(); |
|
|
||
| @Test | ||
| @DisplayName("경기 결과 모든 차의 주행거리가 0 이상이고 주어진 횟수 round 이하인지 확인") | ||
| public void testStart() { |
There was a problem hiding this comment.
start의 기능에 대한 테스트가 조금 모자라게 느껴지는 것 같아요.
조금 더 명확하고 효과적인 테스트는 어떻게 작성할 수 있을까요??
| } | ||
|
|
||
| public void setName(String name) { | ||
| if (name == null || name.trim().isEmpty()) { |
There was a problem hiding this comment.
setName이란 메서드가 RacingCar 내부 외에서 호출되는 일이 있나요?
| @Getter | ||
| public class RacingContest { | ||
| private static final Random random = new Random(); | ||
| private final RacingCar[] RACING_CARS; |
There was a problem hiding this comment.
어떤 변수는 소문자 어떤건 대문자 이건 가연님만의 기준이 있을까요?
개인적으로 현재 가연님 방식으로 사용한다면 차이가 없다고 생각합니다.
일단 for문을 사용 안하고 비교한다는 것부터 큰 장점입니다. assert문을 여러개 써보면 되게 관리하기가 어려운 면이 있거든요. |
| ArrayList<String> expected = new ArrayList<>(Arrays.asList(winners)); | ||
|
|
||
| //then | ||
| assertThat(actual).usingRecursiveComparison().isEqualTo(expected); |
There was a problem hiding this comment.
요렇게 쓰는 것은 순서에 영향을 받을 위험이 있습니다. 어떻게 해결할 수 있을까요??
(만약, winners가 new String[]{names[2], names[1]}) 인 경우 테스트 미통과 처리
|
|
||
| //when | ||
| ArrayList<String> actual = contest.ranking(distances); | ||
| ArrayList<String> expected = new ArrayList<>(Arrays.asList(winners)); |
There was a problem hiding this comment.
이런식으로 두번 초기화할 필요는 없을 것 같아요.
Array와 List를 초기화하는 방법에 대해 학습해보면 좋을 것 같습니다.
질문사항