[e-lang] An attack on a mint

Mark Seaborn mrs at mythic-beasts.com
Sun Mar 9 15:17:07 EDT 2008


Bill Frantz <frantz at pwpconsult.com> wrote:

> 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;
>        }

Wouldn't it be better to write a unit test to check that the behaviour
is correct for the src==this case?  Anyone reintroducing the bug would
discover their mistake when they re-run the test suite.  That way the
comment is nice to have for explaining the code, but you're not
relying on it to prevent this regression during maintenance.  It's not
always easy to preserve comments across refactoring anyway.

Regards,
Mark


More information about the e-lang mailing list