R
Razvan
Hi !
Take a look at the following code:
public class AssertTest
{
private y;
private void doSomething(int i)
{
assert (i < 100) : i = i % 100;
y = i;
}
}
The question was whether the above code makes proper use of
assertions. I answered 'yes', but the right answer was 'no' because of
the fact that the assertion has side effects. Indeed the variable 'i'
is modified inside the assert but why should this be inappropriate ? I
mean the program will end ANYWAY when the assertion is fired, so why
care if 'i' is modified or not before that ?
And what if this is what the developer really wanted to do: display
modulo of 'i' when i was bigger than 100 then end the application ?
Maybe the creator of the test assumes that the Error AssertionError
is caught somewhere ? (but to catch an AssertionError is a stupid
thing to do anyway)
Regards,
Razvan
Take a look at the following code:
public class AssertTest
{
private y;
private void doSomething(int i)
{
assert (i < 100) : i = i % 100;
y = i;
}
}
The question was whether the above code makes proper use of
assertions. I answered 'yes', but the right answer was 'no' because of
the fact that the assertion has side effects. Indeed the variable 'i'
is modified inside the assert but why should this be inappropriate ? I
mean the program will end ANYWAY when the assertion is fired, so why
care if 'i' is modified or not before that ?
And what if this is what the developer really wanted to do: display
modulo of 'i' when i was bigger than 100 then end the application ?
Maybe the creator of the test assumes that the Error AssertionError
is caught somewhere ? (but to catch an AssertionError is a stupid
thing to do anyway)
Regards,
Razvan