If a switch block has no default, adding an assert default is considered appropriate.
But how can this be true. I know that some of you will say that this example is just to prove the question wrong, but I actually had this code in my program when I was developing a game. It was a quiz based game which used to come on TV in USA as "Who wants to be a Billionaire" and in India as "Kaun Banega Crorepati", on the basis of the current question number, I had to print dots on the money tree on the question which were already answered. So I had this code
If I add assert as default here, my code will always generate an AssertionError. Then I would have to add the dafault as the first case or add break after the last case. But why would I do that??? My code is running fine in this condition. So adding assert is increasing complexity and I would consider it inappropriate....
Default case is used with assertion in the following scenario.
In a test suppose max number of questions are 10. Suppose in some exceptional scenario if input questionNumber > 10 then to know why none of question was selected we can use assertions (using simple assertions we get question number passed to switch statement also).
Assertions are used during testing.These statements are not executed during normal execution of program, as assert are disabled by default.Using assert in switch statement means that as per assumptions your are not suppose to reach the default.assertions are used to check your assumptions or the logic you have applied to your code.They are executed when they are enabled.
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad: