1
\$\begingroup\$

I have the following condition statement in Groovy:

if ( condition_1 ) {
    //some actions
} else if ( condition_2 ) {
    //some another actions
} else {
    assert false, 'Assertion description'
}

Code Narc gives me a warning about using boolean false constant in the assertion. I'm not sure if it is real issue, so please share your thoughts about it.

I thought about using exceptions in this code instead of assert false but it looks too heavy (exception handling is too heavy I think) for my particular case.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Throwing an assert error is probably heavier than throwing an Exception (as Groovy will parse the assert inputs to give you a pretty output string)

And unless you are going to be running this hundreds of times per second, I wouldn't worry about it either way...

A way of using assert that gets round the warning (and provides a nice error message) might be to do:

assert condition_1 || condition_2, 'Expected at least condition_1 or condition_2 to be true'
\$\endgroup\$
2
  • \$\begingroup\$ Good! It is exactly what I want! Probably condition_1 && condition_2 should be condition_1 || condition_2 \$\endgroup\$
    – zubactik
    Jul 30, 2013 at 11:15
  • \$\begingroup\$ Whoops...yeah, updated :-) \$\endgroup\$
    – tim_yates
    Jul 30, 2013 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.