"코드 동작은 그대로 두고, 구조를 더 깔끔하게 또는 재사용하기 쉽게 바꾸는 작업"
가독성 향상: 다른 사람이 보더라도 쉽게 이해할 수 있도록.
중복 제거: 반복되는 코드를 줄임.
버그 발생 가능성 감소: 로직이 명확하면 실수도 줄어듦.
유지보수 쉬움: 기능 추가/수정 시 영향 최소화.
모듈화: 공통 기능을 분리하여 재사용 가능하게 함.
1 . 중복 제거 & 모듈화
Before (중복된 코드)
if (HitActor)
{
UGameplayStatics::ApplyDamage(HitActor, 30.f, nullptr, this, nullptr);
UE_LOG(LogTemp, Warning, TEXT("데미지 30 적용"));
}
if (AnotherActor)
{
UGameplayStatics::ApplyDamage(AnotherActor, 30.f, nullptr, this, nullptr);
UE_LOG(LogTemp, Warning, TEXT("데미지 30 적용"));
}
After (함수 분리 - 리팩토링)
void AMyItem::ApplyStandardDamage(AActor* Target)
{
if (!Target) return;
UGameplayStatics::ApplyDamage(Target, 30.f, nullptr, this, nullptr);
UE_LOG(LogTemp, Warning, TEXT("%s에게 데미지 30 적용"), *Target->GetName());
}
ApplyStandardDamage(HitActor);
ApplyStandardDamage(AnotherActor);
반복 코드를 함수로 분리하여 코드 길이 감소, 유지보수성 향상
2 . 하드코딩 제거
Before
Player->LifeCount += 1;
After
const int32 ReviveBonus = 1;
Player->LifeCount += ReviveBonus;
의미 있는 상수 사용으로 코드 의도 명확화 + 변경 시 유연성 증가
Before
void AMyItem::DoThing() {
// ...
}
After
void AMyItem::ApplyKnockbackEffect(ACharacter* TargetCharacter) {
// ...
}
함수/변수 이름만 바꿔도 이해도, 협업 효율 향상
Before (모든 아이템에 중복 구현)
void ABombItem::ServerThrowItem(FVector Direction) { /* ... */ }
void ABatItem::ServerThrowItem(FVector Direction) { /* ... */ }
After (부모에서 구현, 자식은 Override만 할 수 있게)
// AEquipableItem (부모 클래스)
virtual void ServerThrowItem(FVector Direction);
// 자식 클래스에서만 특이한 부분만 override
void ABombItem::ServerThrowItem(FVector Direction)
{
Super::ServerThrowItem(Direction);
// 폭탄만의 특수 효과
}
중복 로직 제거, 확장성 있는 구조로 개선