Tell,Don’t Ask!
내가 가장 많이 본 위반 원칙은'명령, 묻지 말라(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
좋은 대상방향 설계는 대상에게 무엇을 하는지 알려주는 것이지 대상의 상태를 조회한 다음에 상태에 따라 동작을 취하는 것이 아니다.데이터와 이 데이터에 의존하는 동작은 모두 같은 대상에 속해야 한다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.