클린 코드 특강 정리 part.1 (Unreal C++ 스타일)

eomcri·2025년 3월 27일

이 글은 클린 코드를 작성하기 위해 안좋은 패턴과 수정해야하는 패턴을 정리한 내용을 다루고 있습니다.

1. 의미 없는 이름 (Mysterious Name)

❌ Bad

void X1();
int a, b, c;

✅ Good

void CalculatePlayerScore();
int32 KillCount;
float CurrentMana;

2. 중복 코드 (Duplicated Code)

❌ Bad

void ApplyFireEffectToPlayer()
{
    Player->bBurning = true;
    Player->BurnDamagePerSecond = 10.f;
    Player->StartBurning();
}

void ApplyFireEffectToEnemy()
{
    Enemy->bBurning = true;
    Enemy->BurnDamagePerSecond = 10.f;
    Enemy->StartBurning();
}

✅ Good

void UBurnEffectComponent::ApplyBurning(AActor* Target)
{
    if (IBurnableInterface* Burnable = Cast<IBurnableInterface>(Target))
    {
        Burnable->ApplyBurn(10.f);
    }
}

3. 긴 함수 (Long Function)

❌ Bad

void ABattleSystem::Tick(float DeltaSeconds)
{
    UpdateTimer();
    CheckStatusEffects();
    ValidateEnemyState();
    HandleRespawn();
    ProcessLoot();
    UpdateUI();
    // ... 수십 개의 기능
}

✅ Good

void ABattleSystem::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
    UpdateBattleState(DeltaSeconds);
}

void ABattleSystem::UpdateBattleState(float DeltaSeconds)
{
    ProcessEnemyBehavior();
    RefreshCooldowns();
    UpdateHUD();
}

4. 매개변수 과다 (Long Parameter List)

❌ Bad

void SetupLevel(FString Name, int32 EnemyCount, float TimeLimit, FVector SpawnLocation, FRotator Rotation);

✅ Good

USTRUCT()
struct FLevelConfig
{
    FString Name;
    int32 EnemyCount;
    float TimeLimit;
    FVector SpawnLocation;
    FRotator Rotation;
};

void SetupLevel(const FLevelConfig& Config);

5. 전역 상태 (Global Data)

❌ Bad

int32 GScore = 0;

void AddScore(int32 Amount)
{
    GScore += Amount;
}

✅ Good

UCLASS()
class UScoreSubsystem : public UGameInstanceSubsystem
{
    GENERATED_BODY()

private:
    int32 TotalScore = 0;

public:
    void AddScore(int32 Amount) { TotalScore += Amount; }
    int32 GetScore() const { return TotalScore; }
};
profile
게임 개발자가 꿈인 게이머

0개의 댓글