refactorizacion

43 views
Skip to first unread message

miguel agustin cardamone

unread,
Oct 9, 2019, 8:18:35 AM10/9/19
to VA Smalltalk
Hola

tengo estos dos metodos de clase 

priceBeforeTax
| com p p2 |
com := self isBestSeller
ifTrue: [ price / 50 ]
ifFalse: [ price / 100 ].
p := self isBestSeller
ifTrue: [ price * 1.1 ]
ifFalse: [ price ].
p2 := p + com.
^ p2

-----------------------------------------------------------

price
| com p p2 p3 |
self isBestSeller
ifTrue: [ p := price * 1.1.
com := price / 50 ]
ifFalse: [ p := price.
com := price / 100 ].
p2 := p + (p * 21 / 100).
p3 := p2 + com.
^ p3

como se ve hay codigo repetido quiero si pueden me ayuden a refactorizar estos dos metodos 

gracias 

Ben van Dijk

unread,
Oct 11, 2019, 4:58:14 PM10/11/19
to VA Smalltalk
Hi Miquel,

In general you can move the repeating code to new methods.
For example:

basicPrice
  ^self isBestSeller
  ifTrue: [ price * 1.1 ]
  ifFalse: [ price ]

---------------------------

com
 ^self isBestSeller
  ifTrue: [ price / 50 ]
  ifFalse: [ price / 100 ]

-------------------------------

Then change your methods to call the new methods

priceBeforeTax
 | p2 |
 
 p2 := self basicPrice + self com.
 ^ p2

---------------------------------------------

price
 | p p2 p3 |
 p := self basicPrice.

 p2 := p + (p * 21 / 100).
 p3 := p2 + self com.
 ^ p3


Reply all
Reply to author
Forward
0 new messages