@anotherusername said:
Assuming that it was properly written (with the variable definition outside the scope of the for loop), it would only trigger an exception if a list that contained multiple selected items blew up when VerifyMultiSelect() was called. According to the docs, VerifyMultiSelect does not validate the list, it just verifies that it can have multiple items selected. If multiple items are selected, there's no reason VerifyMultiSelect should ever blow up.
Like I said before, the only imaginable use of this code would be as part of a unit test, where the goal was to ensure that the code didn't ever allow a single-selection list to somehow end up having more than 1 item selected.
Again, you seem to tied to the idea that ONLY a single-selection list can have VerifyMultiSelect() be false. And that's not true, at all.
Yes, it would be nice to have a comprehensive set of unit tests that would invalidate the need for this check. But isn't it better not to assume that future unit tests will cover EVERY unique test case and scenario; and instead make sure that if the code does get broken, it's easier to figure out where and why it happened?
Which is better, not having the check there and leaving the possibility of it failing in the future if someone fucks up, or having the check there and having the fuckup not be some mysterious black-box that takes hours or days to figure out?
The reason I'm so passionate about this is that right now in the codebase I work on we have a lot of code that when it fails just returns a nullpointer exception. We COULD be handling the NPEs and I keep trying to make the argument that we should, but the team lead disagrees. Which means that every couple of weeks we get a screenshot from a mystified user that just says 'Null Pointer Exception at line X" and we have to spend hours just to figure out that the user put the expiration date of the policy before the effective date or something else similar.
My stance is, if you KNOW that a condition is likely to break your code, generate an exception for it. The guy maintaining in a couple of years will be much happier for it.