Re: An attack on a mint

Mark Seaborn <mrs-ChQETwBkFSdCgCasaBwmdgC/[email protected]>
Newsgroups gmane.comp.lang.e.general
Message-ID <[email protected]>
Bill Frantz <[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.