refactorizacion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

refactorizacion

miguel agustin cardamone
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 

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/va-smalltalk/a13309b7-1e54-4e99-b473-d79346f65891%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: refactorizacion

Ben van Dijk
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


--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/va-smalltalk/b69bc4e5-e8f4-4e0e-b0bb-f5bd14ac0a81%40googlegroups.com.