Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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'
share|improve this answer
    
Good! It is exactly what I want! Probably condition_1 && condition_2 should be condition_1 || condition_2 – zubactik Jul 30 '13 at 11:15
    
Whoops...yeah, updated :-) – tim_yates Jul 30 '13 at 11:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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