RORO 패턴

최건·2026년 1월 15일

RORO 패턴이란?

RORO = Receive an Object, Return an Object

  • 함수의 파라미터를 여러 개 나열하지 않고, 하나의 객체(Object)로 받고, 반환값도 객체(Object)로 반환하는 패턴

왜 쓰는가?

  1. 가독성 증가
  2. 파라미터 순서 의존 제거
  3. 확장에 강함
  4. TypeScript와 매우 궁합이 좋음

❌ 안 좋은 예

findVenueParticipationByUserAndDate(
  venueId: number,
  userId: number,
  scheduledDate: string,
) 

⭕ RORO 적용 예

findVenueParticipationByUserAndDate({
  venueId,
  userId,
  scheduledDate,
}: FindVenueParticipationByUserAndDateParams)

RORO 패턴 적용해보기

기존 호출 코드

const participation =
      await this.venueParticipationRepository.findVenueParticipationByUserAndDate(
        venueId,
        userId,
        scheduledDate,
      );

...

async findVenueParticipationByUserAndDate(
  venueId: number,
  userId: number,
  scheduledDate: string,
) {
  return await this.venueParticipationRepository.findOne({
    where: {
      venue: {
        id: venueId,
      },
      user: {
        id: userId,
      },
      scheduledDate: new Date(scheduledDate),
    },
  });
}

RORO 패턴 적용 후 호출 코드

const participation =
      await this.venueParticipationRepository.findVenueParticipationByUserAndDate({
        venueId,
        userId,
        scheduledDate,
      });

...

interface FindVenueParticipationByUserAndDateParams {
  venueId: number;
  userId: number;
  scheduledDate: string;
}

async findVenueParticipationByUserAndDate({
  venueId,
  userId,
  scheduledDate,
}: FindVenueParticipationByUserAndDateParams) {
  return await this.venueParticipationRepository.findOne({
    where: {
      venue: {
        id: venueId,
      },
      user: {
        id: userId,
      },
      scheduledDate: new Date(scheduledDate),
    },
  });
}

네이밍 규칙 추천

목적추천 네이밍
Repository 입력~Params
Service 입력~Command
조회 조건~Query
반환값~Result

예시

SaveClusterCommentParams
FindClusterCommentsQuery
CreateClusterCommentResult

언제 RORO를 쓰는 것이 좋을까?

✔ 파라미터가 3개 이상일 경우
✔ 나중에 확장될 가능성 있을 경우
✔ TypeScript 사용 중일 경우

profile
개발이 즐거운 백엔드 개발자

0개의 댓글