c# - Dollar Object intricacies -
i reading kent beck's book "test driven development: example." in book, there coding example:
public class dollar { public int _amount; public dollar(int amount) { _amount = amount; } public dollar times(int multiplier) { return new dollar(_amount *= multiplier); } } [testmethod] public void testmethod2() { dollar 5 = new dollar(5); dollar product = five.times(2); assert.areequal(10, product._amount); product = five.times(3); assert.areequal(15, product._amount); }
according kent, second dollar object: "product," necessary in order preserve original "five" dollar object; however, second assertion returns false because product._amount equal 30. have been unable find errata on text. how kent's code above need change in order have second assertion equal true or 15 == 15? book's example flawed? why product._amount in second assertion not equal 15?
as has been said in comments, this:
public dollar times(int multiplier) { return new dollar(_amount *= multiplier); }
should be:
public dollar times(int multiplier) { return new dollar(_amount * multiplier); }
in original form, *=
operator modifies _amount
variable of instance of dollar
class times
called on. removing =
changes operation reads value of _amount
variable , uses in calculation.
Comments
Post a Comment