[38] 메서드보다 람다 표현식이 낫다

다음 본문은 도서 이펙티브 C# (빌 와그너)에서 나오는 주제를 다룹니다.

 

 

람다 표현식을 사용하여 코드를 작성하면 동일한 코드를 반복하게 될 때가 있다.

// 20년 이상 근속자
var earlyFolks = from e in allEmployees
                 where e.Classification == EmployeeType.Salary
                 where e.YearsOfService >= 20
                 where e.MonthlySalary < 4000
                 select e;

// 20년 미만 근속자
var newest = from e in allEmployees
                 where e.Classification == EmployeeType.Salary
                 where e.YearsOfService < 20
                 where e.MonthlySalary < 4000
                 select e;

 

성능

where절을 여러 번에 걸쳐 나누어 썼기 때문에

성능을 우려해 하나의 where절로 변경을 하겠는가?

 

하지만 단순 조건문은 인라인화 될 가능성이 높기 때문에 성능이 개선되기를 기대하기는 어렵다. 

여러번에 where절에 나눠 쓰는 것이 명시적이라면 블록으로 두는 편이 낫다.

 

반복

두 람다식에서 중복되는 람다식이 눈에 띈다. 이를 메서드로 분리하면 어떨까?

private static bool LowPaidSalaried(Employee e) =>
    e.MonthlySalary < 4000 && e.Classification ==
    EmployeeType.Salary;
    
var earlyFolks = from e in allEmployees
                 where LowPaidSalaried(e) &&
                         e.YearsOfService >= 20
                  select e;
                  
var earlyFolks = from e in allEmployees
                 where LowPaidSalaried(e) &&
                         e.YearsOfService < 20
                  select e;

좀 단순해진 것 같지만, 분리된 메서드는 재사용될 가능성이 현저히 낮다.

 

동일하게 사용되는 로직을 분리하여 호출 체인의 앞쪽으로 이동시켜보면 해결될 것이다.

그렇다면 다음과 같이 재사용 가능한 빌딩 빌딩 블록을 만들어 볼 수 있다.

private static IQueryable<Employee> LowPaidSalariedFilter
    (this IQueryable<Employee> sequence) =>
      from s in sequence
      where s.Classification == EmployeeType.Salary &&
        s.MonthlySalary < 4000
      select s;
    
var salaried = allEmployees.LowPaidSalariedFilter();

var earlyFolks = salaried.Where(e => e.YearsOfService >= 20);
var newes = salaried.Where(e => e.YearsOfService < 20);

 

결론

재사용성을 위해 쿼리표현식과 람다가 관련된 경우에 한하여 빌딩 블록을 만들어 조합하는 것이 낫다.

 

 

 

댓글

Designed by JB FACTORY