Skip to content
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

도서 대출 연장 기능 추가 및 테스트 #14

Merged
merged 11 commits into from
Jan 31, 2025
4 changes: 2 additions & 2 deletions src/main/java/com/study/bookcafe/domain/borrow/Period.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public Period createExtended(Level level) {

public boolean isExtendable() {
long epochDay = (from.toEpochDay() + to.toEpochDay()) / 2;
LocalDate targetDate = LocalDate.ofEpochDay(epochDay);
LocalDate targetDate = LocalDate.ofEpochDay(epochDay).minusDays(1);
LocalDate now = LocalDate.now();

return targetDate.isEqual(now) || targetDate.isBefore(now);
return now.isAfter(targetDate);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now 를 객체 내부에서 직접 얻으면 객체가 결정적이지 못하게 됩니다.
무슨 뜻일까요? 해결할 수 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

항상 동일한 입력 값에 동일한 출력이 나온다는 뜻입니다. (순수함수)
위 코드로는 now가 시간이 지나면서 다른 결과를 응답해줄 수 있기 때문에 결정적이지 못한 메서드입니다.

메서드에서 LocalDate 를 매개변수로 받는다면, 메서드를 호출할 때 파라미터로 오늘 날짜를 넣어야 하는데 다른 날짜를 넣어서 유효성 검사를 통과하여 검증이 제대로 이뤄지지도 않을 수도 있을 것 같아서 now를 내부에서 직접 얻었습니다.

해결방법으로

  1. 호출하는 곳에서 유효성 검사 진행
  2. 제 3의 객체에서 유효성 검사 진행
  3. 객체 내부에 매개변수가 오늘 날짜인지 판별하는 private 메서드를 선언하여 유효성 검사 진행

2번이나 3번이 괜찮아보이는데,
만약 매개변수 now가 오늘 날짜가 아닌 다른 날짜로 넘어왔지만 연장이 가능한 날짜라면 위 방법으로 유효성 검사를 진행했을 때 false 라는 결과를 응답해주는 것도 잘못된 것 같습니다.

그럼 예외처리...? 한다고해도 매개변수를 오늘날짜로 강제한다는 건 지금의 코드랑 다를 게 없어 보입니다...😵

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋ 그럼 상상력이 등장할 때죠.

자 정리해봅시다. 먼저 연장가능하다 는 것은 Period 에 어울리는 역할일까요?
기간아 연장가능하니? 인지 대출기간아 연장가능하니? 인지 살펴봅시다.

기간이 곧 대출기간인가? 맞다면 이름을 좀 더 구체적으로 지을 필요가 있겠습니다.
따로 대출기간을 다루는 책임은 다른 객체에 있다면 기간은 좀 더 범용적으로 동작하는 방식을 고려해야 좋습니다.

연장 말고 기간에 입장에서 무엇을 검사하고 싶었나 하는거죠.

더.. 얘기하면 뭔가 상상이 줄어들 수 있으니 한번 고민해보세요

}

}
Loading