Re: An attack on a mint
Bill Frantz <[email protected]>
| Newsgroups | gmane.comp.lang.e.general |
|---|---|
| Message-ID | <r02010500-1049-80F65322EA4A11DCB7FC0030658F0F64@[192.168.1.5]> |
[email protected] (Tyler Close) on Tuesday, March 4, 2008 wrote: >Suppose a rewrite like: > > int take(final PurseX src) { > if (dead) { throw new NullPointerException(); } > if (src.dead) { throw new NullPointerException(); } > > // subtract the credits from the source > final int r = src.balance; > src.balance = 0; > > // add the credits to the destination > balance += r; > > return r; > } > >Is such a layout sufficient to discourage reversion to the previous state? I would instead write: int take(final PurseX src) { if (dead) { throw new NullPointerException(); } if (src.dead) { throw new NullPointerException(); } // The following code must work correctly when src==this. This bad code // destroys money when src==this: // balance += r; // src.balance = 0; final int r = src.balance; src.balance = 0; // add the credits to the destination balance += r; return r; } Cheers - Bill ------------------------------------------------------------------------- Bill Frantz | The first thing you need when | Periwinkle (408)356-8506 | using a perimeter defense is a | 16345 Englewood Ave www.pwpconsult.com | perimeter. | Los Gatos, CA 95032