E D R , A S I H C RSS

Tell Vs Ask


~cpp 
Alec Sharp, in the recent book Smalltalk by Example [SHARP], points up a very valuable lesson in few words: 
  
Procedural code gets information then makes decisions. Object-oriented code tells objects to do things. 
   --- Alec Sharp

then μ—μ„œ than 이 λ˜μ–΄μ•Ό ν•˜λŠ” 것이 μ•„λ‹κΉŒμš”? μ œκ°€ 원문을 λͺ»λ΄μ„œ ν•¨λΆ€λ‘œ κ³ μΉ˜κΈ°λŠ” νž˜λ“€κ΅°μš”.
λ§žλŠ” 지적인데. 원문도 then 이ꡰ. λ‚΄κ°€ 해석을 than 으둜 읽고 μ‹€μˆ˜ν–ˆλ„€. Thanks.~

μ•„λž™ μƒ€ν”„μ˜ "Smalltalk by Example" μ΄λΌλŠ” 졜근의 μ±…μ—μ„œλŠ” μ•„μ£Ό κ°’μ–΄μΉ˜μžˆλŠ” κ΅ν›ˆμ„ κ°•μ‘°ν•œλ‹€.

절차적인 μ½”λ“œμ—μ„œλŠ” μ •λ³΄λΌ μ–»κ³  λ‚œ λ’€ ν•  일을 κ²°μ •ν•œλ‹€. 개체 지ν–₯ μ½”λ“œμ—μ„œλŠ” κ°μ²΄μ—κ²Œ 행동을 ν•˜λ„λ‘ μ‹œν‚¨λ‹€.

~cpp 
That is, you should endeavor to tell objects what you want them to do; do not ask them questions about their state, 
make a decision, and then tell them what to do. 

μš”λŠ”, 당신은 κ°μ²΄λ“€λ‘œ ν•˜μ—¬κΈˆ 당신이 μ›ν•˜λŠ” 일을 ν•˜λ„λ‘ λ…Έλ ₯ν•΄μ•Ό ν•œλ‹€. κ°μ²΄λ“€μ˜ μƒνƒœμ— λŒ€ν•΄ κ°μ²΄λ“€μ—κ²Œ 묻지 말고, κ²°μ •ν•˜λΌ. 그리고 κ°μ²΄λ“€μ—κ²Œ μ›ν•˜λŠ” 일을 ν•˜λ„λ‘ ν•˜λΌ.

~cpp 
The problem is that, as the caller, you should not be making decisions based on the state of the called object 
that result in you then changing the state of the object. The logic you are implementing is probably the called object's 
responsibility, not yours. For you to make decisions outside the object violates its encapsulation. 
λ¬Έμ œμ μ€ caller λ‘œμ„œ, 당신은 called object 의 μƒνƒœμ— κΈ°λ°˜ν•œ 결정을 내리면 μ•ˆλœλ‹€λŠ” 것이닀.
당신이 κ΅¬ν˜„ν•˜λ €λŠ” logic 은 μ•„λ§ˆλ„ 호좜된 객체의 μ±…μž„μ΄μ§€, λ‹Ήμ‹ μ˜ μ±…μž„μ΄ μ•„λ‹ˆλ‹€. (μ—¬κΈ°μ„œ you λŠ” ν•΄λ‹Ή object λΌ μ΄μš©ν•˜λŠ” client. caller) 당신이 object 의 λ°”κΉμͺ½μ—μ„œ 결정을 λ‚΄λ¦¬λŠ” 것은 ν•΄λ‹Ή object 의 encapsulation 에 μœ„λ°˜λœλ‹€.

~cpp 
Sure, you may say, that's obvious. I'd never write code like that. Still, it's very easy to get lulled into 
examining some referenced 
object and then calling different methods based on the results. But that may not be the best way to go about doing it. Tell the object 
what you want. Let it figure out how to do it. Think declaratively instead of procedurally! 
μ•„λ§ˆ 당신은 μ΄λ ‡κ²Œ 말할지도 λͺ¨λ₯Έλ‹€. "λ‚˜λŠ” 그런 μ½”λ“œλΌ μž‘μ„±ν•œ 적이 μ—†μ–΄!" ν•˜μ§€λ§Œ, referenced object 의 값을 μ‘°μ‚¬ν•΄μ„œ κ·Έ 결과값에 따라 각각 λ‹€λ₯Έ λ©”μ†Œλ“œλΌ ν˜ΈμΆœν•˜λŠ” μ‹μœΌλ‘œ μ λ‹Ήνžˆ κ΅¬ν˜„ν•˜λ©° μ–Όλ¨Έλ¬΄λ¦¬λŠ” κ²½μš°λŠ” ν”ν•˜λ‹€.

exam) μ•„λ§ˆ Refactoring 책에도 λ‚˜μ˜¬κ²ƒμ΄μ§€λ§Œ.
~cpp 
switch (person.gender) {
	case IS_MALE:
		____
	case IS_FEMALE:
		____
}
μ΄λŸ°μ‹μ˜ μ½”λ“œλ“€. 보톡 이런건 Polymorphism 으둜 ν•΄κ²°ν•΄μ•Ό ν•  λ¬Έμ œλ“€μ΄μ§€μš”.

ν•˜μ§€λ§Œ, μ΄λŠ” 쒋은 방법이 μ•„λ‹ˆλ‹€. 당신이 μ›ν•˜λŠ” 일에 λŒ€ν•΄μ„œ object μ—κ²Œ μ‹œμΌœλΌ. (즉, μ € ν–‰μœ„μ— λŒ€ν•œ 결정은 object λ‚΄μ—μ„œ ν•΄κ²°ν•˜κ²Œλ”) object 둜 ν•˜μ—¬κΈˆ μ–΄λ–»κ²Œ ν•΄μ•Ό 할지 ν•΄κ²°ν•˜λ„λ‘ ν•˜λΌ. μ ˆμ°¨μ μ΄λ €ν•˜κΈ° 보단, μ„œμˆ μ μ΄λ €κ³  ν•˜λΌ. (μ΄λŠ” OOP μ—μ„œ μ΄μ•ΌκΈ°ν•˜λ“, Object λ“€ κ°„μ˜ 행동듀에 λŒ€ν•΄μ„œ.)

~cpp 
It is easier to stay out of this trap if you start by designing classes based on their responsibilities, 
you can then progress naturally to specifying commands that the class may execute, as opposed to queries 
that inform you as to the state of the object. 
만일 당신이 각 ν΄λž˜μŠ€λ“€μ˜ μ±…μž„μ— κΈ°λ°˜ν•œ λ””μžμΈμœΌλ‘œ μ‹œμž‘ν–ˆλ‹€λ©΄ μ΄λŸ¬ν•œ 함정에 νƒˆμΆœν•˜λŠ” 일은 쉽닀.
(ResponsibilityDrivenDesign) κ·ΈλŸ¬ν•œ 경우, 당신은 λ‹Ήμ‹ μ—κ²Œ 객체의 μƒνƒœλΌ μ•Œλ¦¬λ„λ‘ μ§ˆμ˜λ¬Έμ„ μž‘μ„±ν•˜λŠ” λŒ€μ‹  (주둜 getter 듀에 ν•΄λ‹Ήλ˜λ¦¬λΌ 생각), class 듀이 μ‹€ν–‰ν•  수 μžˆλŠ” command 듀을 μžμ—°μŠ€λŸ½κ²Œ λ°œμ „μ‹œμΌœ λ‚˜κ°ˆ 것이닀.

λ§Œμ•½ κ·Έλ“€μ˜ μ±…μž„(의λΈ)에 κΈ°λ°˜ν•΄μ„œ 클래슀 μ„κ³„λΌ μ‹œμž‘ν•œλ‹€λ©΄ μ΄λŸ°ν•œ λ«μ—μ„œ λ²—μ–΄λ‚˜κΈ°μ— 더 쉽닀.

Reference - Smalltalk By Example 쀑 'Tell, Don't Ask' (http://www.iam.unibe.ch/~ducasse/WebPages/FreeBooks.html 에 κ³΅κ°œλ˜μ–΄μžˆλ‹€.)

See also κ°œμ²΄μ§€ν–₯vsμ ˆμ°¨μ§€ν–₯ - μ§€κΈˆ μ—¬κΈ° μ„œλ²„κ°€ 죽은것 같은데 μ„œλ²„ μ‚΄μ•„λ‚˜λ©΄ νŽ˜μ΄μ§€ λœ°κ»λ‹ˆλ‹€...;; - μž„μΈνƒ
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:11
Processing time 0.0171 sec