Am 28.02.2015 um 20:08 schrieb Patrick Schaaf:


Am 28.02.2015 19:32 schrieb "Crypto Compress" <cryptocompr...@googlemail.com <mailto:cryptocompr...@googlemail.com>>:
>
> class BankAccount {
>     function Add($amount) {
>         assert($amount > 0);
>         // ... code block ...
>     }
> }
>
> Now the programmer implementing "code block" to gracefully handle $amount > 0 has a problem. There is no way to (Unit)test failing path on development machine. Code after assertion is never called for failing contitions. What to do?

Put the assert AFTER handling the always-to-be-done validation, of course. Doesn't make sense otherwise.

Patrick



if ($amount > 0) { ...code block... }
// else {
throw new WrongAmountEx();
// }

Frankly i can't see any point in using assertion after checked expression thus duplicate the expression. It's simple a bug. As situation is clearly wrong, throw/exit/die and change code flow as desired. This is not the "conditionally unreachable code" case i argue about.

Reply via email to