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

일급 컬렉션 사용하여 중복 코드 제거 #116

Merged
merged 3 commits into from
Jun 17, 2024

Conversation

Hanjaemo
Copy link
Contributor

💡 작업 내용

  • �일급 컬렉션 Tags를 통해 중복 코드 제거
  • 위도와 경도를 래핑하여 코드 가독성 개선

💡 자세한 설명

일급 컬렉션 적용

@Transactional(readOnly = true)
public List<CollectingBoxResponse> findCollectingBoxesWithinArea(final double longitude,
                                                                 final double latitude,
                                                                 final List<Tag> tags) {
    if (tags.isEmpty()) {
        throw new CollectingBoxException(ErrorCode.NOT_SELECTED_TAG);
    }
    return collectingBoxRepository.findAllWithinArea(longitude, latitude, radius, tags);
}

위와 같이 수거함 태그의 유효성을 검증하는 로직이 서비스 계층 여러 메서드에 존재했습니다.
따라서 List<Tag> 타입의 변수를 가지는 일급 컬렉션 Tags를 만들고, 유효성 검증 로직을 해당 객체에 위임했습니다.

public class Tags {
    private final List<Tag> tags;

    public Tags(List<Tag> tags) {
        if (tags == null || tags.isEmpty()) {
            throw new CollectingBoxException(NOT_SELECTED_TAG);
        }
        this.tags = tags;
    }

    public List<Tag> getTags() {
        return List.copyOf(tags);
    }
}

이를 통해 서비스 계층의 중복 코드를 제거할 수 있었습니다.

위도, 경도 래핑

기존 모든 계층의 메서드에서는 다음과 같이 위도와 경도를 전달 받고 있었습니다.

@Transactional(readOnly = true)
public List<CollectingBoxResponse> findCollectingBoxesWithinArea(final double longitude,
                                                                 final double latitude,
                                                                 final Tags tags) {
    return collectingBoxRepository.findAllWithinArea(longitude, latitude, radius, tags);
}

이렇게 위도와 경도를 전달 받게 되면, 파라미터 순서를 실수할 위험이 있습니다.
그래서 GeoPoint라는 클래스로 래핑하였습니다.

public class GeoPoint {
    private final double longitude;
    private final double latitude;

    public GeoPoint(double longitude, double latitude) {
        if (invalidLongitude(longitude) || invalidLatitude(latitude)) {
            throw new IllegalArgumentException("Invalid parameter value for longitude or latitude");
        }
        this.longitude = longitude;
        this.latitude = latitude;
    }

    private boolean invalidLongitude(double longitude) {
        return longitude < -180 || longitude > 180;
    }

    private boolean invalidLatitude(double latitude) {
        return latitude < -90 || latitude > 90;
    }
}

이를 통해 순서를 틀릴 수 있는 문제를 방지할 수 있으며, 코드의 가독성도 향상시킬 수 있게 되었습니다.

✅ 셀프 체크리스트

  • PR 제목을 형식에 맞게 작성했나요?
  • 브랜치 전략에 맞는 브랜치에 PR을 올리고 있나요?
  • 이슈는 close 했나요?
  • Reviewers, Labels, Projects를 등록했나요?
  • 작업 도중 문서 수정이 필요한 경우 잘 수정했나요?
  • 테스트는 잘 통과했나요?
  • 불필요한 코드는 제거했나요?

closes #115

@Hanjaemo Hanjaemo added the refactor 리팩토링 label Jun 16, 2024
@Hanjaemo Hanjaemo requested a review from Doyeon04 June 16, 2024 16:32
@Hanjaemo Hanjaemo self-assigned this Jun 16, 2024
@Hanjaemo Hanjaemo linked an issue Jun 16, 2024 that may be closed by this pull request
@Hanjaemo Hanjaemo merged commit 98dfcca into main Jun 17, 2024
1 check passed
@Hanjaemo Hanjaemo deleted the feat/115-first-class-collection branch July 6, 2024 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor 리팩토링
Projects
None yet
Development

Successfully merging this pull request may close these issues.

일급 컬렉션 사용하여 중복 코드 제거
1 participant