3 ms·
For me this is also a point about writing code that can easily be mentally translated to human language. It might seem simple enough, but this bug shows that
by tpoacher 4y ago
For me this is also a point about writing code that can easily be mentally translated to human language.
It might seem simple enough, but this bug shows that
if (Current < Withdrawal): Current -= Withdrawal
requires a few mental steps in working memory that can easily get missed
Whereas
isValidTransaction = Current - Withdrawal >= 0
if (isValidTransaction): NewBalance = Current - Withdrawal
reads like English, and is hard to get wrong in my view.
In the first case, the reader is expected to do mental math (albeit easy) in order to verify correctness. Whereas in the second, they're only expected to read a logical statement and confirm that the logic checks out.
- tpoacher 4y agoIncrement this counter by one (by upvoting this comment) if you thought I was exaggerating, but hadn't immediately noticed the same bug as the one OP reported in my top example :)