Tell,Don’t Ask!

21075 단어

내가 가장 많이 본 위반 원칙은'명령, 묻지 말라(Tell, Don't Ask)'원칙이었다.이 원칙은 한 대상이 다른 대상에게 무엇을 해야 하는지를 명령하는 것이지 다른 대상의 상태를 조회해서 무엇을 해야 하는지를 결정하는 것이 아니라는 것이다. (다른 대상의 상태를 조회해서 무엇을 해야 하는지를 결정하는 것은 '기능 질투 (Feature Envy)' 라고도 부른다.
예:
       if (person.getAddress().getCountry() == "Australia") { 
               // , Person , Person 
              //address, address country
       } 
        :
       if(person.livesIn("Australia")){
           //operation
       }

Tell, Don't ask에 대한 기사의 예:

EXAMPLE 1


 Not so good:
 <% if current_user.admin? %>  <%= current_user.admin_welcome_message %>  <% else %> <%= current_user.user_welcome_message %>  <% end %>

Better:
 <%= current_user.welcome_message %>

EXAMPLE 2


Not so good:
 def check_for_overheating(system_monitor) if system_monitor.temperature > 100 system_monitor.sound_alarms end  end

Better:
 system_monitor.check_for_overheating  class SystemMonitor def check_for_overheating if temperature > 100 sound_alarms end end  end

     
   
좋은 대상방향 설계는 대상에게 무엇을 하는지 알려주는 것이지 대상의 상태를 조회한 다음에 상태에 따라 동작을 취하는 것이 아니다.데이터와 이 데이터에 의존하는 동작은 모두 같은 대상에 속해야 한다.

좋은 웹페이지 즐겨찾기